0

In my Xamarin.Forms application, I have 1 page and am setting the view model in the xaml as such:

<ContentPage.BindingContext>
    <vm:MainViewModel />
</ContentPage.BindingContext>

The problem is, that when I put the app in the background and then open it again, I have 2 instances of MainViewModel. I can see with the debugger that a new one is created, but also code from the first one (before I put it in the background) is still being called (from event handlers).

How can I fix this? Is it possible to retain and use the same view model? Or am I forced to have a new one every time I close and reopen the app? If so, how do I make sure the old one is released from memory?

I'm running the app on an Android device.

Drake
  • 2,679
  • 4
  • 45
  • 88

1 Answers1

2

This sounds like a regression bug, since it was reported/fixed multiple times.

I have a similar thread here on SO : Xamarin.Forms ViewModelLocator get called twice

Don't set the BindingContext in XAML unless it is a one page app, instead consider using an IoC container.

EvZ
  • 11,889
  • 4
  • 38
  • 76