Manprit Singh Sahota
Manprit Singh Sahota

Reputation: 1339

Wpf Prism Dispose ViewModel after navigation

I am developing an app in which I have used Prism Framework. I have registered viewmodels in bootstrapper as below:

ViewModelLocationProvider.Register<LoginControl, LoginViewModel>();
ViewModelLocationProvider.Register<MainWindow, MainWindowViewModel>();
ViewModelLocationProvider.Register<CountryList, CountryViewModel>();

I have also register prism for Navigation as:

builder.RegisterTypeForNavigation<LoginControl>();
builder.RegisterTypeForNavigation<MainWindow>();
builder.RegisterTypeForNavigation<CountryList>();

Its working perfect but I found that the constructor for each viewmodel is called once while first time navigating to ViewModel. After that same object of viewmodel is served. But I want that after navigation, viewmodel should also dispose and new object is served every time.

I want to achieve some this similar to autofac:

builder.RegisterType<ModuleLoader>().InstancePerDependency()

But I dont found any method in ViewModelLocationProvider to achieve this. Is there any way to achieve this?

Update

Found that when I navigate from one view to another then the view is also not disposed. We I move to same View (Usercontrol in Region) then same object is served which is the real cause for same instance of ViewModel. Is there any way to dispose view after navigation in Prism?

Upvotes: 2

Views: 2250

Answers (1)

Manprit Singh Sahota
Manprit Singh Sahota

Reputation: 1339

Resolved the issue by implementing IRegionMemberLifetime interface to my usercontrol and adding below property

public bool KeepAlive
{
    get
    {
        return false;
    }
}

Upvotes: 1

Related Questions