WPF实现定时刷新UI界面的实例详解

WPF实现定时刷新UI界面的实例详解,第1张

概述WPF实现定时刷新UI界面的实例详解 这篇文章主要为大家详细介绍了WPF实现定时刷新UI界面功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了WPF定时刷新UI界面展示的具体代码,供大家参考,具体内容如下

代码:

using NHibernate.Criterion;using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.ComponentModel;using System.Data;using System.linq;using System.Text;using System.Threading;using System.windows;using System.windows.Controls;using System.windows.Data;using System.windows.documents;using System.windows.input;using System.windows.Media;using System.windows.Media.Imaging;using System.windows.Navigation;using System.windows.Shapes;using Visifire.Charts;namespace SunCreate.CombatPlatform.ClIEnt{  public partial class MainPage : UserControl  {    private System.Timers.Timer timerNotice = null;    public MainPage()    {      InitializeComponent();    }    private voID MainPage_Loaded(object sender, RoutedEventArgs e)    {      #region 通知公告      if (timerNotice == null)      {        BindNotice();        timerNotice = new System.Timers.Timer();        timerNotice.Elapsed += new System.Timers.ElapsedEventHandler((o, eea) =>        {          BindNotice();        });        timerNotice.Interval = 60 * 1000;        timerNotice.Start();      }      #endregion    }    private voID MainPage_SizeChanged(object sender, SizeChangedEventArgs e)    {    }    #region 绑定通知公告    private voID BindNotice()    {      System.Threading.Tasks.Task.Factory.StartNew(() =>      {        try        {          int total = 0;          TES_NOTICE info = new TES_NOTICE();          IList<TES_NOTICE> List = new List<TES_NOTICE>();          List = HI.Get<INoticeService>().GetListPage(null, DateTime.MinValue, DateTime.MinValue, 1, 50, ref total);          dispatcher.Invoke(new Action(() =>          {            noticeListVIEw.ItemsSource = List;          }));        }        catch        {        }      });    }    #endregion  }}

说明:在 System.Timers.Timer 的事件中使用 BackgroundWorker 是无效的,即如下代码不能正常刷新界面:

using NHibernate.Criterion;using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.ComponentModel;using System.Data;using System.linq;using System.Text;using System.Threading;using System.windows;using System.windows.Controls;using System.windows.Data;using System.windows.documents;using System.windows.input;using System.windows.Media;using System.windows.Media.Imaging;using System.windows.Navigation;using System.windows.Shapes;using Visifire.Charts;namespace SunCreate.CombatPlatform.ClIEnt{  public partial class MainPage : UserControl  {    private System.Timers.Timer timerNotice = null;    public MainPage()    {      InitializeComponent();    }    private voID MainPage_Loaded(object sender, RoutedEventArgs e)    {      #region 通知公告      if (timerNotice == null)      {        BindNotice();        timerNotice = new System.Timers.Timer();        timerNotice.Elapsed += new System.Timers.ElapsedEventHandler((o, eea) =>        {          BindNotice();        });        timerNotice.Interval = 60 * 1000;        timerNotice.Start();      }      #endregion    }    private voID MainPage_SizeChanged(object sender, SizeChangedEventArgs e)    {    }    #region 绑定通知公告    private voID BindNotice()    {      PT_USER_INFO user = new PT_USER_INFO();      IList<TES_COMBAT_TASK> taskList = new List<TES_COMBAT_TASK>();      BackgrounDWorker worker = new BackgrounDWorker();      worker.DoWork += (s, e) =>      {        user = HI.Get<Cache.ICacheService>().UserCache.GetCurrentUserInfo();        taskList = HI.Get<ITaskService>().GetCombatTaskByUserIDUnfinished(user.ID.ToString());      };      worker.RunWorkerCompleted += (s, e) =>      {        try        {          taskListVIEw.ItemsSource = taskList;        }        catch { }      };      worker.RunWorkerAsync();    }    #endregion  }}

也可以使用 dispatcherTimer 刷新界面,但耗时的 *** 作不能放在dispatcherTimer的事件中执行,否则界面会卡,那么耗时的定时 *** 作,比如查询数据库,需要再用一个 System.Timers.Timer,相对比较麻烦。 总结

以上是内存溢出为你收集整理的WPF实现定时刷新UI界面的实例详解全部内容,希望文章能够帮你解决WPF实现定时刷新UI界面的实例详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1211779.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存