Search code examples
prismdryioc

Unregister view-Viewmodel Prism xamarin dryioc


I have a situation where I need to Unregister a ViewModel and rerregister it .

The reason is that at times I want to inject a "fakeservice" rather than the "real one".

So if I press the "offline" button I need to unregister the viewModels and re-register them so that the Fakeservices are used.

How can i unregister a view-viewmodel using prism and dryioc

I usuall Register like this:

  protected override void RegisterTypes(Prism.Ioc.IContainerRegistry containerRegistry)
{
    containerRegistry.RegisterForNavigation<MyPage,MyPageViewModel>();
}

How do I unregister the above?

thanks


Solution

  • When you register your View and ViewModel with that extension, it is worth noting that the ViewModel itself is not registered with the container, it is only registered with the ViewModelLocationProvider which provides the ViewModel type to resolve for a given View.

    It is also worth noting here that ViewModels are resolved with a Transient Lifetime which means that each time it is resolved you get a new instance, so all you would really need to do is Navigate away and then back again.