What are the cases that a BindableProperty's propertyChanged is called multiple times?

mshwfmshwf EGMember ✭✭✭
edited December 2017 in Xamarin.Forms

I have a custom control that has an ItemsSource property, that I set in XAML like so:

<Controls:RadioButtonsGroup Grid.Column="1" ItemsSource="{Binding lstGender}" Orientation="Horizontal" SelectedItem="{Binding SelectedGender}" HorizontalOptions="Fill"/>

backing property:

public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(nameof(ItemsSource), typeof(IEnumerable<object>), typeof(RadioButtonsGroup), propertyChanged: OnItemsSourceChanged);

the problem is that the OnItemsSourceChanged method is called multiple times when I go back from the page and enter it again.

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I don't see that in any of my apps. My first guess is its something you're doing in code.
    Maybe disposing of the VM and making a new one every time? That would be a new source

    Or a dozen other things.

  • DanielGlosDanielGlos USMember ✭✭

    Hey, any update on this? I'm facing exactly the same issue and I'm starting to be desperate.

  • AnthonyRamirezAnthonyRamirez USUniversity ✭✭✭

    @DanielGlos

    I'm not sure about it getting called multiple times. A breakpoint in the propertyChanged method could help troubleshoot where it is getting called from.

    I typically add the following code to the top of my propertyChanged method, which would prevent unnecessary processing of the same or null value.

    if (oldvalue == newvalue ||
    newvalue == null)
    return;

  • DanielGlosDanielGlos USMember ✭✭

    I also use your checks for equal values or newValue == null but both conditions pass every time. When I look into call stack its called from Xamarin.Forms code

  • DanielGlosDanielGlos USMember ✭✭

    It looks like when i do Push it adds the event handler for propertyChanged again and agin but I dont know how.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @DanielGlos said:
    It looks like when i do Push it adds the event handler for propertyChanged again and agin but I dont know how.

    "do a push"... What does that mean?
    When you push a button on the app it adds a subscription to the handler? Well there's your problem. You probably have a lambda as the handler for the button, right? And you added a subscribe within that, right? So every time you click, you add another subscription.
    Lets see your code. Otherwise you're just saying "Me too, how do I fix it" and expect us to be telepathic.

Sign In or Register to comment.