Hi,
I'm using the MVVM pattern with XamarinForms, so I create a model, assign it to the Xamarin Page (the page is a subclass of ContentPage) and because of the bindings declared in XAML everything gets registered beautifully.
However, if I can push and popping the same view on/off a NavigationPage (say 10 times) and then change a visual element (i.e. by typing in a bound text field), I get a console output for all the old bindings as well as the current binding. I have a base class for my MVVM models that includes the logging...
public void PropChanged(String property) {
if (PropertyChanged != null)
{
Console.WriteLine(this.GetType().ToString()+" PropChanged:" + property);
PropertyChanged(this,new PropertyChangedEventArgs(property));
}
}
The only way I have so far been able to resolve the issue is to modify the Page to include an override OnDissapearing method, I tried to call UnapplyBindings() but that didn't help, however, setting the BindingContext to null has done the trick...
protected override void OnDisappearing()
{
base.OnDisappearing();
// Only this seems to remove the bindings created from XAML from the ViewModel
this.BindingContext = null; // <--- YAY!!! Bindings now removed
this.ListDetailViewModel.Dispose(); // <-- Making sure the view model also tidies up
this.ListDetailViewModel = null;
}
My question is, shouldn't the ContentPage OnDisappearing be removing all the bindings anyway?
Although what I'm doing works, is this the right approach?
Given that ContentPage.UnapplyBindings doesn't remove the bindings, but setting ContentPage.BindingContext=null does, what the heck does UnapplyBindings actually do then?
You might have noticed that in addition, I delegate through to my view model to call Dispose which has some clean up code, is this 'okay', or should I simply have a method I call something like CleanUp - I'm not sure when the Dispose is 'meant' to be called, but it would seem it doesn't unless I manually invoke it.
Thanks.