xamarin.formsapp-shell

Xamarin Forms App Shell OnPropertyChanged not updating for FlyoutHeader view


I am binding a text property from a viewmodel into a the FlyoutHeader view of the App Shell slide out menu

All the update events fire correctly when the app is initialised, however there is a page the user may go in order to update their information

The problem I have is when you navigate to this page and execute the update, the FlyoutHeader view isn't updating in response to the viewmodel

Is there something specific to the slide out menu that means once it is loaded it then it doesn't change in response to onpropertychanged ... ? Doesn't seem right

From AppShell xaml

<Shell.FlyoutHeader>
    <views:FlyoutHeader />
</Shell.FlyoutHeader>

From Flyoutheader code behind

    private LocationsViewModel _vm;

    public FlyoutHeader()
    {
        InitializeComponent();

        this.BindingContext = new LocationsViewModel();
    }

    protected override void OnBindingContextChanged()

    {

        _vm = BindingContext as LocationsViewModel;


        _vm.PropertyChanged += _vm_PropertyChanged;

        _vm.SetUserLocation();

    }

User navigates to 'change location' page via a button in FlyoutHeader view, called like so

            await Shell.Current.GoToAsync("changelocation");

In my debug log I can see that the binding object 'user' in the LocationsViewModel does trigger OnPropertyChanged in response to a change made on this page, but then going 'back' and opening the flyoutmenu nothing has changed and I can see the PropertyChanged didn't fire for that view?

I thought the whole point of OnPropertyChanged binding was even with the view loaded an update occurs to cause the view to respond?


Solution

  • The issue is basically because you are creating multiple instances of the same ViewModel, So the thing is you are trying to change a header that is bound to a property in your ViewModel, which is then bound to you View, so for your View to reflect the change that you made in your ViewModel it ought to have the instance of the ViewModel that is bound to it if that makes sense!

    Doing this will solve your issue

    Good luck!