0

To provide some context, I'm writing a Xamarin.Forms application and utilizing data binding with INotifyPropertyChanged. Currently I have an inventory counter displayed on a button. The text on this button displays the bounded "Count" variable (e.g Current Inventory: 35). When I press the button, I push a screen onto the navigation stack which allows me to edit this "Count" variable. I use the class implementation like this

 public class UserInventory : INotifyPropertyChanged
{
    private int count = 0;

    // Declare the event
    public event PropertyChangedEventHandler PropertyChanged;

    public int Count
    {
        get => Preferences.Get(nameof(Count),0);
        set
        {

            if (count == value || value <1)
                return;
            Preferences.Set(nameof(Count), value);
            //count = value;
            //Application.Current.Properties["Count"] = count;
            OnPropertyChanged(nameof(Count));
            //OnPropertyChanged(nameof(displayName));
        }
    }



    public UserInventory()
    {

    }


    void OnPropertyChanged(string count)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(count));
    }
}

I add this class in Xaml according to the tutorial on the Xamarin <ContentPage.BindingContext> <local:UserInventory /> </ContentPage.BindingContext> So the variables are bounded correctly and I have no issues seeing updates on the current page or when I push new pages. The issue is when I swipe back on iOS the previous screen with the button "Current Inventory: 35" does not update to reflect the new changes. If I push that screen the changes are reflected.

Is there anyway to ensure the bounded data is updated when you go back (PopAsync()) ?

Joshua Wilkinson
  • 133
  • 2
  • 10
  • if both pages are using the same VM instance then this should just work automatically. However, the fact that you're using Preferences as a backing store might introduce a wrinkle. I'd suggest trying to switch to a local field to see if that changes the behavior. – Jason May 23 '20 at 17:42
  • How do you edit the Count property, could you show more code?And if you can't find the issue, you could try to use [MessagingCenter](https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center) to pass the value. – Leo Zhu May 25 '20 at 03:00

1 Answers1

0

Try overriding page's OnAppearing() method and call OnPropertyChanged from there.

Assuming 'UserInventory' the binded VM.....

public partial class Page1:ContentPage
{
  public Page1()
  {
    InitializeComponent();
    VM = (UserInventory)BindingContext;
  }

  public UserInventory VM { get; }

  protected override void OnAppearing()
  {
    VM.Notify();
    base.OnAppearing();
  }
}

.

public class UserInventory: INotifyPropertyChanged
{
  ........

  public void Notify()
  {
    OnPropertyChanged(nameof(Count));
  }
}
user10398433
  • 426
  • 3
  • 10
  • Hi unfortunately this returns "VM is not an instance of an Object" when it calls VM.Notify(); – Joshua Wilkinson Jun 01 '20 at 14:11
  • Hi, What do you mean by returns? Is VM.Notify() throwing an exception, because VM is null? Can you check if you are properly binding the xaml page to the view model. – user10398433 Jun 01 '20 at 19:37