定期自动更新MVVM中的数据

本文关键字:数据 MVVM 更新 | 更新日期: 2023-09-27 18:02:11

在我的模型和ViewModel的XAML视图(列表),我有一个字符串属性。数据在LoadData中从WebService检索。我不使用MVVMlight

模型:

public class LocationsModel : INotifyPropertyChanged
{
    private string _name;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            if (value != _name)
            {
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }
    }
    private string _id;
    public string ID
    {
        get
        {
            return _id;
        }
        set
        {
            if (value != _id)
            {
                _id = value;
                NotifyPropertyChanged("ID");
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

ViewModel:

public class MainViewSurroundingModel : INotifyPropertyChanged
{
    public MainViewSurroundingModel()
    {
        this.Items = new ObservableCollection<LocationsModel>();
    }
    public ObservableCollection<LocationsModel> Items { get; set; }
    private string _name = "";
    private string _id = "";
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            if (value != _name)
            {
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }
    }
    public string ID
    {
        get
        {
            return _id;
        }
        set
        {
            if (value != _id)
            {
                _id = value;
                NotifyPropertyChanged("ID");
            }
        }
    }
    public bool IsDataLoaded
    {
        get;
        private set;
    }
    public async Task<bool> LoadData()
    {
        // WEB-API CALL IS HERE...
        this.IsDataLoaded = true;
        return true;
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

我现在想通过代码周期性地更新Name属性(而不是从WebService重新检索数据)每5分钟。

我如何以及在哪里实现这一点?

定期自动更新MVVM中的数据

很抱歉,我的回答很模糊,我没有太多的时间,所以如果有人关心写更多关于我所说的,他们是受欢迎的。

如何

一种可能性是使用DispatcherTimer(更多关于它的例子在这里)并使用它的Tick事件。(在提供的链接中有一个教程)

现在关于在哪里创建定时器

这取决于你想改变什么,什么时候开始。

如果我想改变列表中所有的数据一些预定义的方式,我可以这样做:

 public class MainViewSurroundingModel : INotifyPropertyChanged
 {
      //...omitted the parts you have done, only writing the things I'd change...
      MainViewSurroundingModel()
      {
           ..if there was something leave it here..
           changeTimer = new DispatcherTimer...
           changeTimer.Tick += Tick;
           changeTimer.Interval = ....et cetera, setting the timer based on tutorial
      }
      public ObservableCollection<LocationsModel> items;
      public ObservableCollection<LocationsModel> Items 
      {
        get //edited so that I can write my own setter
        {
            return items;
        }
        set
        {
            if(value != items)
            {
               items = value;
               //NotifyPropertyChanged("Items"); //can be used here, not necessary
               changeTimer.Start();
            }
        }
      }
      private DispatcherTimer changeTimer;//based on the tutorial
      //based on the tutorial provided, create dispatcher timer and
      //tick definition somewhere around here
      private Tick(...)
      {
          //code that iterates through the list and updates it goes here!
      }
 }