Home > other >  Why the RefreshView not updated my child element when I refresh?
Why the RefreshView not updated my child element when I refresh?

Time:02-07

Here's my xaml file.

    <RefreshView  x:Name="myRefreshView" Refreshing="myRefreshView_RefreshingAsync" RefreshColor="#b52b2b">
          <ScrollView>
             <StackLayout>
                 <Label Text="{Binding firstName }" />
            </StackLayout>
         </ScrollView>
 </RefreshView>

Here's my .cs file which has a function

namespace Health.Views

    {
        public partial class LandingPage : ContentPage
        {
           
            public string firstName { set; get; }
    
            public LandingPage()
            {
            
                firstName = "Mary";
                this.BindingContext = this;
              
            }
    
    
             async void myRefreshView_RefreshingAsync(Object sender, System.EventArgs e)
             {
                 await Task.Delay(3000);
                 firstName = "John";
                 myRefreshView.IsRefreshing = false;
             }
         }
    }

The problem is when I refresh, the name is not change to "John". Not sure what else I have to add.

CodePudding user response:

You need to implement INotifyPropertyChanged.

public class LandingPage : ContentPage, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string _firstName;
    public string firstName { 
        get{return _firstName;}
        set
        {
            _firstName = value;
            OnPropertyChanged("firstName");
        } }


    protected void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }



}
  •  Tags:  
  • Related