How does xamarin forms controls receive propertychanged notification and render?

LeloLelo Member ✭✭✭

How does xamarin forms controls receive propertychanged notification and render?

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Nobody is going to be able to describe how all that low-level code interacts in a simple thread.
    And I suspect you're not really asking for a textbook on how all that works.
    So what is it you're trying to do and what information is it you're really in need of?

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai

    ViewModels generally implement the INotifyPropertyChanged interface, which means that the class fires a PropertyChanged event whenever one of its properties changes. The data binding mechanism in Xamarin.Forms attaches a handler to this PropertyChanged event so it can be notified when a property changes and keep the target updated with the new value.
    For more details you can refer https://lalorosas.com/blog/xamarin-forms-mvvm-inotifypropertychanged

  • LeloLelo Member ✭✭✭

    The background is that I would like to get MobX going for Xamarin.Forms. Here is a C# library for MobX. Basically you can mark properties and classes with [Observable] and you can then react on the changes of them anywhere in your code.

    Right now the UI side of MobX in the library only has Blazor. Meaning the ui component will rerender when the observable value changes - if it is implementing IObserverComponent. It is doing that through this class - by calling StateHasChanged() in ComponentBase.cs that triggers render.

    Basically I am looking for a equivalent method in Xamarin forms.

  • JohnHardmanJohnHardman GBUniversity mod
    edited June 5

    @Lelo

    I don't know MobX. From a very quick look, it sounds like Fody PropertyChanged. You might want to look at https://github.com/Fody/PropertyChanged

  • LeloLelo Member ✭✭✭

    Well when thinking of it again - it might not make such a big difference. The Fody PropertyChanged can suffice. But it would be a bit more developer friendly - and for people coming from mobx background especially to be able to implement the xamarin ui as observer and have them render when [observable] marked classed or properties change.

    The reactive parts of the library is actually usable as it is in it's current state, like this:
    (code behind:)

    Observe.Autorun(() => {
        button.BackgroundColor = viewModel.Title == "TitleChanged" ? Color.Green : Color.Blue;
        if (viewModel.IsBusy)
        {
            ShowAnimation();
        }
    });
    

    compared to:

    viewModel.PropertyChanged += (object sender, PropertyChangedEventArgs e) =>
    {
        if (e.PropertyName == "Title")
        {
            button.BackgroundColor = viewModel.Title == "TitleChanged" ? Color.Green : Color.Blue;
        }
        if (e.PropertyName == "IsBusy")
        {
            if (viewModel.IsBusy)
            {
                ShowAnimation();
            }
        }
    };
    

    Basically the change in property is automatically give to the area of the code that is using the value via Observe.autorun(()) => { }; or other Observe methods. So if the method given in autorun does not use any of the property values it won't run.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @Lelo said:
    compared to:

    viewModel.PropertyChanged += (object sender, PropertyChangedEventArgs e) =>
    {
        if (e.PropertyName == "Title")
        {
            button.BackgroundColor = viewModel.Title == "TitleChanged" ? Color.Green : Color.Blue;
        }
        if (e.PropertyName == "IsBusy")
        {
            if (viewModel.IsBusy)
            {
                ShowAnimation();
            }
        }
    };
    

    Well... You're comparing to a way that should never be done anyway. Nobody does that 1998 WinForms stuff these days - or for decades. You wouldn't even do this in C# at all.

    In today's world it would be all XAML - no C#. Just put a DataTrigger in the XAML for the color.

    We don't know what ShowAnimation does... But since its based on the bool property of the view model, I'm going to guess it means to show an ActivityIndicator or similar to show the user that a long running operation is happening. Again, that just binding from XAML.

    Please don't be mad when I make the observation that you're trying to solve a problem that exists out of a lack of understanding of how to do these things in a correct and modern way. Not understanding how to leverage good XAML practices leads to doing it in old-fashioned ways in C#... leads to way to fix it in C#... leads to patches on top of problems that were self created.

  • LeloLelo Member ✭✭✭

    @ClintStLaurent said:

    @Lelo said:
    compared to:

    viewModel.PropertyChanged += (object sender, PropertyChangedEventArgs e) =>
    {
        if (e.PropertyName == "Title")
        {
            button.BackgroundColor = viewModel.Title == "TitleChanged" ? Color.Green : Color.Blue;
        }
        if (e.PropertyName == "IsBusy")
        {
            if (viewModel.IsBusy)
            {
                ShowAnimation();
            }
        }
    };
    

    Well... You're comparing to a way that should never be done anyway. Nobody does that 1998 WinForms stuff these days - or for decades. You wouldn't even do this in C# at all.

    In today's world it would be all XAML - no C#. Just put a DataTrigger in the XAML for the color.

    We don't know what ShowAnimation does... But since its based on the bool property of the view model, I'm going to guess it means to show an ActivityIndicator or similar to show the user that a long running operation is happening. Again, that just binding from XAML.

    Please don't be mad when I make the observation that you're trying to solve a problem that exists out of a lack of understanding of how to do these things in a correct and modern way. Not understanding how to leverage good XAML practices leads to doing it in old-fashioned ways in C#... leads to way to fix it in C#... leads to patches on top of problems that were self created.

    I don't agree with you. In short by only trying to use xaml you loose ability in creating dynamic ui. And view specific code should not be put in the ViewModel. With animation I meant this which can't be done in xaml. I don't like the notion of introducing 4 different Triggers, BindingCondition, PropertyCondition, Setter - And then we have the different Binding paths, source x:refrence etc - just to do simple view logic.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @Lelo said:
    And view specific code should not be put in the ViewModel.

    Absolutely correct.

    In short by only trying to use xaml you loose ability in creating dynamic ui.

    That hasn't been my experience in 15 years of XAML.

    I don't agree with you.

    Ok.

Sign In or Register to comment.