xamu-infrastructure icon indicating copy to clipboard operation
xamu-infrastructure copied to clipboard

Proper way to Dispose ViewModel

Open soooraj opened this issue 6 years ago • 2 comments

@markjulmar : Could you please help me on below issue.

In my app I am using DependencyServiceExtension in the library to create my ViewModel. For example

BindingContext="{infrastructure:DependencyService Type={x:Type viewModels:HomeViewModel},
                                                      Scope=NewInstance}">

Due to some reason I don't want to keep the same instance of my view model through out the life time of the app. That is the reason I am using Scope as new Instance.

But now I noticed that the View Model that are created while navigation are not disposing at all. The events that I registered are firing for multiple times, based on the number of times I navigated to the page.

This is creating lot of issues in my application. Could you please let me know the best method to dispose this view models when the user navigates back (INavigationService.GoBack) method.

soooraj avatar Feb 22 '19 08:02 soooraj

@markjulmar : Ping

soooraj avatar Mar 15 '19 10:03 soooraj

I've found this to be a problem a times generally speaking where the View having a BindingContext of the ViewModel causes the View/ViewModel combo to leak. When the view is "done" try setting the BindingContext to null. This seems to break anything tied through bindings and allow the cleanup.

DennisWelu avatar Jul 22 '19 16:07 DennisWelu