Forum Xamarin.Android

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

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.

Best Answer

  • Accepted Answer

    Hi,

    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 _bindings = new List();

    and then

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

    Take care
    Laurent

Answers

  • LaurentBugnionLaurentBugnion CHMember, Insider, University
    Accepted Answer

    Hi,

    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 _bindings = new List();

    and then

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

    Take care
    Laurent

  • HelenMamalakiHelenMamalaki GBUniversity ✭✭

    @Laurent Thank you! That seems to have been the problem.

Sign In or Register to comment.