MVVMLight ValueChanged event listener trigerred only for 3-4 times before stopping

117 Views Asked by At

I have set up a binding in my Xamarin.Android activity where I am binding to a boolean property in my viewmodel using WhenSourceChanges(). I’ve also tried ValueChanged event and in both cases the event listener fire the first 3 or 4 times and then it just stops working. The value had definitely changed, I alternate between true and false, and yet it just stops working after a while. Do you know why this might be?

this.SetBinding(() => this.Vm.ShowErrorMessage)
.WhenSourceChanges(() =>
{
if (this.Vm.ShowErrorMessage)
{
    this.ErrorMsg.Visibility = ViewStates.Visible;
}
else
{
    this.ErrorMsg.Visibility = ViewStates.Gone;
}
});

I am using MvvmLightLibs v5.3.0, developing in VS2015 on A Win10 machine, and testing on an Android v4.2 Samsung Galaxy device.

1

There are 1 best solutions below

2
On BEST ANSWER

Bindings are weak referenced, and will be garbage collected if you don’t save them. Please watch my presentation here.

In your case what you need to do is something like that:

private List<Binding> _bindings = new List<Binding>();

and then

_bindings.Add(this.SetBinding(…)); // (the rest remains the same).