mvx:Warning: Exception masked ArgumentException: An item with the same key has already been added
Asked Answered
A

1

0

I am trying to upgrade an existing Xamarin.Android app from mvvmcross 3.5 to mvvmcross 4.4.0. I have followed this documentation and the app now builds and starts up showing the splashscreen, but it doesn't navigate past the splash screen. When I call ShowViewModel<MainViewModel>() nothing happens. The last Application output I see is the output below. I have currently no idea where to look next. Anybody have an idea?

    mvx:Warning: 17,87 Exception masked ArgumentException: An item with the same key has already been added.
      at System.ThrowHelper.ThrowArgumentException (System.ExceptionResource resource) [0x0000b] in <5d9d5f6570654147b240b9398d8953cc>:0 
  at System.Collections.Generic.Dictionary`2[TKey,TValue].Insert (TKey key, TValue value, System.Boolean add) [0x0008e] in <5d9d5f6570654147b240b9398d8953cc>:0 
  at System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) [0x00000] in <5d9d5f6570654147b240b9398d8953cc>:0 
  at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] keySelector, System.Func`2[T,TResult] elementSelector, System.Collections.Generic.IEqualityComparer`1[T] comparer) [0x0005c] in <1a5e8b31a0e44a97a3b8f85920b2d059>:0 
  at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] keySelector, System.Func`2[T,TResult] elementSelector) [0x00000] in <1a5e8b31a0e44a97a3b8f85920b2d059>:0 
  at MvvmCross.Droid.Shared.Presenter.FragmentHostRegistrationSettings.InitializeIfNeeded () [0x00128] in <90641336d9534da69c6ac9e0919ad635>:0 
  at MvvmCross.Droid.Shared.Presenter.FragmentHostRegistrationSettings.IsTypeRegisteredAsFragment (System.Type viewModelType) [0x00000] in <90641336d9534da69c6ac9e0919ad635>:0 
  at MvvmCross.Droid.Shared.Presenter.MvxFragmentsPresenter.Show (MvvmCross.Core.ViewModels.MvxViewModelRequest request) [0x0000c] in <90641336d9534da69c6ac9e0919ad635>:0 
  at MvvmCross.Droid.Views.MvxAndroidViewDispatcher+<>c__DisplayClass2_0.<ShowViewModel>b__0 () [0x00000] in <f6aebf863dc84be7b380cfec8d459508>:0 
  at MvvmCross.Droid.Views.MvxAndroidMainThreadDispatcher.RequestMainThreadAction (System.Action action) [0x00019] in <f6aebf863dc84be7b380cfec8d459508>:0 
  at MvvmCross.Droid.Views.MvxAndroidViewDispatcher.ShowViewModel (MvvmCross.Core.ViewModels.MvxViewModelRequest request) [0x00014] in <f6aebf863dc84be7b380cfec8d459508>:0 
  at MvvmCross.Core.ViewModels.MvxNavigatingObject.ShowViewModelImpl (System.Type viewModelType, MvvmCross.Core.ViewModels.IMvxBundle parameterBundle, MvvmCross.Core.ViewModels.IMvxBundle presentationBundle, MvvmCross.Core.ViewModels.MvxRequestedBy requestedBy) [0x0002e] in <69bce0378e8e413982d3b552d7e387a8>:0 
  at MvvmCross.Core.ViewModels.MvxNavigatingObject.ShowViewModel (System.Type viewModelType, MvvmCross.Core.ViewModels.IMvxBundle parameterBundle, MvvmCross.Core.ViewModels.IMvxBundle presentationBundle, MvvmCross.Core.ViewModels.MvxRequestedBy requestedBy) [0x00000] in <69bce0378e8e413982d3b552d7e387a8>:0 
  at MvvmCross.Core.ViewModels.MvxNavigatingObject.ShowViewModel[TViewModel] (MvvmCross.Core.ViewModels.IMvxBundle parameterBundle, MvvmCross.Core.ViewModels.IMvxBundle presentationBundle, MvvmCross.Core.ViewModels.MvxRequestedBy requestedBy) [0x00000] in <69bce0378e8e413982d3b552d7e387a8>:0 
  at MvvmCross.Core.ViewModels.MvxAppStart`1[TViewModel].Start (System.Object hint) [0x00013] in <69bce0378e8e413982d3b552d7e387a8>:0 
  at MvvmCross.Droid.Views.MvxSplashScreenActivity.TriggerFirstNavigate () [0x00005] in <f6aebf863dc84be7b380cfec8d459508>:0 
  at MvvmCross.Droid.Views.MvxSplashScreenActivity.InitializationComplete () [0x00009] in <f6aebf863dc84be7b380cfec8d459508>:0 
  at MvvmCross.Droid.Platform.MvxAndroidSetupSingleton.<InitializeFromSplashScreen>b__7_1 () [0x0000a] in <f6aebf863dc84be7b380cfec8d459508>:0 
  at MvvmCross.Platform.Core.MvxMainThreadDispatcher.ExceptionMaskedAction (System.Action action) [0x00000] in D:\git\MvvmCross\MvvmCross\Platform\Platform\Core\MvxMainThreadDispatcher.cs:22 
Alodee answered 23/1, 2017 at 8:31 Comment(3)
Can you please provide some reproduction steps for this? The stack trace doesn't seem symbolicated so I can only guess which line would fail.Hesta
I think I have finally found the cause: the application uses code in the setup.cs like this: assemblies.Add (typeof(Android.Support.V4.Widget.DrawerLayout).Assembly); After removing this code the navigation seems to work again. But I am still investigating while typing this.Alodee
Did you find out what is causing this as I am getting the same problem. It would be a great help if you could post the solution or cause. ThanksTrinh
A
2

The problem was that before upgrading to MvvmCross 4 we had our own implementation of protected override IMvxAndroidViewPresenter CreateViewPresenter () in Setup.cs. After upgrading we changed this implementation to the (standard) implementation:

    protected override IMvxAndroidViewPresenter CreateViewPresenter()
    {
        var mvxFragmentsPresenter = new MvxFragmentsPresenter(AndroidViewAssemblies);
        Mvx.RegisterSingleton<IMvxAndroidViewPresenter>(mvxFragmentsPresenter);
        return mvxFragmentsPresenter;
    }

The standard implementation didn't work wel with our protected override IList<Assembly> AndroidViewAssemblies implementation.

Solution:

Removing assemblies.Add (typeof(...).Assembly); in protected override IList<Assembly> AndroidViewAssemblies { ... } solved our problem.

Alodee answered 23/2, 2017 at 14:36 Comment(2)
Thanks Niels. I had an identical situation with a custom presenter, I did the same and it solved the problem, although admittedly it is unclear why this would be.Trinh
Same here, the only thing I can think of is that the assemblies are added automatically now, hence the message "An item with the same key has already been added."Alodee

© 2022 - 2024 — McMap. All rights reserved.