如何在ViewModel中每隔一段时间运行一次Event
文章目录
有一种场景,每隔一段时间就要执行一次动作,比如从服务器上拉取内容。碰巧使用的又是C#+WPF的MVVM模式,需要在ViewModel里执行这种操作。该如何进行呢?
可以用DispatcherTimer,比如这样:
//The Constructure
public myViewModel
{
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(5); //Every 5 seconds do something
timer.Tick += Tick_Event;
timer.Start();
}
void Tick_Event(object sender, EventArgs e)
{
//Do something you want
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论