Xamarin.Forms MessagingCenter to update Pages BindingContext and PopAsync

PatrickJelittoPatrickJelitto Member ✭✭✭
edited June 2018 in Xamarin.Forms

Hi Guys,

i have the following situation:

      private async void UpdateKundenDatenInOverviewPage(KundenDatenPageModel e)
                        {
                            initializerModel.KundenDaten = e;
                
                            BindingContext = initializerModel;
                
                            await Navigation.PopAsync();
                        }

This Method gets invoked by a Message my Page subscribes from. The initializerModel is a class member and gets initialized once in the constructor of the page with default values. After the message was sent i want to update the KundenDaten Property of the initializerModel in order to bind the new data to the page again. But it wont work.

            private async void UpdateKundenDatenInOverviewPage(KundenDatenPageModel e)
            {
                OverviewPageModel initializerModel = new OverviewPageModel();
                initializerModel .KundenDaten = e;
                initializerModel .FahrzeugZustand = fahrzeugZustand;

                BindingContext = initializerModel;

                await Navigation.PopAsync();
            }

This code works. So if i create a new instance of initializerModel (in this case its a OverviewPageModel) and set it as the pages BindingContext the data will appear after i pop the page the message was sent from from the navigationstack.

Can u guys help me?
Thank you in advance, Gelatto

Answers

  • dkarzondkarzon Member ✭✭

    What are you binding it to that isn't working?
    Does your ViewModel class implement INotifyPropertyChanged? And is the PropertyChanged event being fired for that property?

  • PatrickJelittoPatrickJelitto Member ✭✭✭

    @dkarzon my viewmodel doesnt implement inotifypropertychanged, i'll look it up and implement it. do you know any good sites with explaination? thanks

  • dkarzondkarzon Member ✭✭

    @Gelatto I can't post links at the moment but I would recommend having a look at PropertyChanged.Fody it will wire up a lot of the code for you so you can just write regular properties.

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    This guide shows the basics of setting up a View Model to implement INotifyPropertyChanged:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/data-bindings-to-mvvm#a-simple-viewmodel

    There is one event in the interface:

    public event PropertyChangedEventHandler PropertyChanged;
    

    See the code samples in the above linked section of the guide on how to implement and use INotifyPropertyChanged

Sign In or Register to comment.