question

mrizoiwe98 avatar image
0 Votes"
mrizoiwe98 asked ColeXia-MSFT commented

Updating content between tabs binded by Preference

I am using a tabbed app and I have a button on one tab, when the user clicks then it increments a Preference and I want to update its value on another tab. I tried to experiment with MVVM but I couldnt figure it out.


   public partial class Page1View: ContentPage
     {
         public Page1View()
         {
             InitializeComponent();
         }
    
         private void Button_Clicked(object sender, EventArgs e)
         {
             Preferences.Set("Total", Preferences.Get("Total", 0)+1);
         }
     }


 public class Page2ViewModule : INotifyPropertyChanged
     {
         public int Total
         {
             get => Preferences.Get(nameof(Total), 0);
             set
             {
                 if (Preferences.Get(nameof(Total),0) == value)
                     return;
                 Preferences.Set(nameof(Total), value);
                 OnPropertyChanged(nameof(Total));
             }
         }
         public event PropertyChangedEventHandler PropertyChanged;
         protected virtual void OnPropertyChanged(string propertyName)
         {
             var changed = PropertyChanged;
             if (changed != null)
             {
                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
             }
         }
     }




dotnet-xamarinforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ColeXia-MSFT avatar image
0 Votes"
ColeXia-MSFT answered ColeXia-MSFT commented

Hello,

Welcome to Microsoft Q&A!

@mrizoiwe98

You update the value with Preferences , but this will not trigger the change notify of Property Total in Page2ViewModule.

Total does not update , so the Label(or something else) does not update .

We should take a way to tell Total it should be changed after button clicking .

Here I suggest you use Messaging Center , I've mentioned it in this thread .

Here we just move the messaging center into Set method of the property .

set
{
     MessagingCenter.Subscribe<object>(this, "Hi", (o) => {              
               OnPropertyChanged(nameof(Total));
      });                        
}


If you have any question please let me know .


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I tried your solution but it didnt work unfortunately.. Here's what I done with MessagingCenter. On the button click event I sent a message like this:

 private void Button_Clicked(object sender, EventArgs e)
    
 {
      Preferences.Set("Total", Preferences.Get("Total", 0)+1);
     MessagingCenter.Send<Object>(this, "hi");
  }

In the Page2 view module I modified the set property so it subscribes to message:

 public int Total
         {
             get => Preferences.Get(nameof(Total), 0);
             set
             {
                 MessagingCenter.Subscribe<Object>(this, "hi", (o) =>
                 {
                     //Total = Preferences.Get(nameof(Total), 0);
                     OnPropertyChanged(nameof(Total));
                 });
             }
         }

But that still hasn't worked.





0 Votes 0 ·

I test without problem .
We need to set model.Total in second page to let code step into Setter method , so that messaging center would be triggered .

Page2

public Page2ViewModule model { get; set; }
  public Page2View()
        {
            InitializeComponent();
            model = new Page2ViewModule ();
            model.Total = Preferences.Get("Total", 0);   //add this line

            this.BindingContext = model;
        }

0 Votes 0 ·

Thank you that worked. But I'm not sure would this be the best way to do it? Or is there a more efficient way?

0 Votes 0 ·
Show more comments

I tried it with another type. This time with DateTime , by using
Preferences.set(“LastTime”, DateTime.Now)
But that didn’t work

0 Votes 0 ·