当 INotifyPropertyChanged - UWP c# 发生时调用一个函数

3
我将会把代码中的SelectedSchoolList这个可观察集合赋值的函数每分钟调用一次。我希望在这个可观察集合中有任何数据发生变化时,能够调用另一个函数(比如CallIfValueChanged();)。请问我该怎么实现?
我的代码:
  public static ObservableCollection<SelectedSchoolList> _SelectedSchoolList = new ObservableCollection<SelectedSchoolList>();

        DispatcherTimer dispatcherTimer;
        dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += dispatcherTimer_Tick;
        dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
        dispatcherTimer.Start();

        // callback runs on UI thread
       async void  dispatcherTimer_Tick(object sender, object e)
        {
        response_from_server = await CallWebService();
        if (!response_from_server.Equals("FAIL", StringComparison.CurrentCultureIgnoreCase))
        {
        parseJSONandAssignValuesToSelectedSchoolList (response_from_server);//this function assigns values to _SelectedSchoolList  
        }                                 
        }
      CallIfValueChanged();// I want to call this function here only if any data on '_SelectedSchoolList' is updated/changed

我的课程:

  class SelectedSchoolList : INotifyPropertyChanged
{
    public string SchoolName { get; set; }
    public string Score { get; set; }
    public ObservableCollection<SelectedStudentList> SelectedStudentArray { get; set; }
    public event PropertyChangedEventHandler PropertyChanged;
}
public class SelectedStudentList
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public string IndividualScore { get; set; }

}

2
"PropertyChanged" 是一个事件。您可以像处理其他事件一样处理它。 - jmcilhinney
请定义“如果任何数据发生更改”,并展示赋值代码。 - Dennis
请查看我编辑过的代码。 - user2431727
2个回答

1

首先,您需要在实现中修复一些错误。您没有正确实现INotifyPropertyChanged接口。

SchoolName属性为例,如果您使用新值设置它,则PropertyChanged事件根本不会触发,这意味着UI不会更新以反映更改。

class SelectedSchoolList : INotifyPropertyChanged
{
    public string SchoolName { get; set; }
    //... other code omitted
}

你应该在属性的setter中触发PropertyChanged事件。对于所有用于绑定的属性都应该这样做。 Q: 如果在此可观察集合中更改了任何数据,我想调用另一个函数(例如:CallIfValueChanged();),该怎么办? 你可以注册一个事件处理程序到_SelectedSchoolListCollectionChanged事件中,该事件会被触发

当添加、删除、更改、移动或整个列表刷新时。

在事件处理程序中调用你的方法。

如何在我的代码下编写CollectionChanged事件?我的类和代码在两个不同的页面上。 - user2431727
你不需要更改 My class 中的代码,只需在 My code 中添加此行:_SelectedSchoolList.CollectionChanged += CallIfValueChanged; - kennyzx
它显示:"无法隐式将类型 void 转换为 System.Collections.Specialized.NotifyCollectionChangedEventHandler"。 - user2431727

0
尝试为每个公共属性添加私有字段,将NotifyPropertyChanged方法添加到您的类中。然后更改setter和getter。例如:
class SelectedSchoolList : INotifyPropertyChanged
{
   private string _schoolName;
   private string _score;

   public string SchoolName 
   { 
       get => _schoolName; 
       set { _schoolName = value; NotifyPropertyChanged();}
   }
   public string Score 
   { 
       get => _score; 
       set { _score = value; NotifyPropertyChanged();}
   }
   public ObservableCollection<SelectedStudentList> SelectedStudentArray{ get; set;}

   public event PropertyChangedEventHandler PropertyChanged;

   private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
   {
       PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
   }
 }

最后,在_SelectedSchoolListCollectionChanged事件中添加一个方法:
_SelectedSchoolList.CollectionChanged += (sender, e) => { CallIfValueChanged(); };

这个(sender,e)参数应该是什么? - user2431727
这是一个 lambda 表达式。阅读更多:Lambda表达式(sender,e) 是一个没有名称的函数,具有两个参数:(object sender, NotifyCollectionChangedEventArgs e){ CallIfValueChanged(); } 是该函数的主体,此函数将返回 void - Linh Nguyen
您可以通过 object sender 来获取它:var collection = (sender as ObserverbalCollection<SelectedSchoolList>);sender 是属性发生变化的对象。 - Linh Nguyen
私有的 void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { CallIfValueChanged(); } 当数据值改变时,此函数未触发。 - user2431727
是的,控制流程进入NotifyPropertyChanged函数。但是不会进入写在我的主页上的OnCollectionChanged函数。 - user2431727
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接