本文实例为大家分享了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界面
# C#中WPF使用多线程调用窗体组件的方法
# C# WPF开源UI控件库MaterialDesign介绍
# WPF自定义MenuItem样式的实现方法
# 在WPF中使用多线程更新UI
# 绑定
# 放在
# 事件中
# 可以使用
# 再用
# 大家分享
# 不能正常
# 具体内容
# 大家多多
# partial
# public
# Client
# private
# UserControl
# MainPage
# Charts
# Visifire
# Shapes
# CombatPlatform
# SunCreate
相关文章:
南京做网站制作公司,南京哈发网络有限公司,公司怎么样,做网页美工DIV+CSS待遇怎么样?
如何用wdcp快速搭建高效网站?
如何在云虚拟主机上快速搭建个人网站?
北京企业网站设计制作公司,北京铁路集团官方网站?
制作无缝贴图网站有哪些,3dmax无缝贴图怎么调?
西安制作网站公司有哪些,西安货运司机用的最多的app或者网站是什么?
定制建站如何定义?其核心优势是什么?
高防服务器租用如何选择配置与防御等级?
建站之星代理如何获取技术支持?
建站之星免费版是否永久可用?
安徽网站建设与外贸建站服务专业定制方案
如何用美橙互联一键搭建多站合一网站?
如何在沈阳梯子盘古建站优化SEO排名与功能模块?
大连企业网站制作公司,大连2025企业社保缴费网上缴费流程?
建站三合一如何选?哪家性价比更高?
seo网站制作优化,网站SEO优化步骤有哪些?
如何基于云服务器快速搭建个人网站?
如何高效完成自助建站业务培训?
python的本地网站制作,如何创建本地站点?
儿童网站界面设计图片,中国少年儿童教育网站-怎么去注册?
阿里云网站搭建费用解析:服务器价格与建站成本优化指南
网站制作公司广州有几家,广州尚艺美发学校网站是多少?
建站之星导航配置指南:自助建站与SEO优化全解析
大连网站制作费用,大连新青年网站,五年四班里的视频怎样下载啊?
如何通过商城免费建站系统源码自定义网站主题?
简单实现Android文件上传
如何在橙子建站中快速调整背景颜色?
枣阳网站制作,阳新火车站打的到仙岛湖多少钱?
建站主机SSH密钥生成步骤及常见问题解答?
如何通过VPS建站实现广告与增值服务盈利?
广州建站公司哪家好?十大优质服务商推荐
网站制作软件免费下载安装,有哪些免费下载的软件网站?
东莞专业制作网站的公司,东莞大学生网的网址是什么?
济南网站建设制作公司,室内设计网站一般都有哪些功能?
建站VPS配置与SEO优化指南:关键词排名提升策略
建站之星安装路径如何正确选择及配置?
宝塔新建站点为何无法访问?如何排查?
如何获取开源自助建站系统免费下载链接?
娃派WAP自助建站:免费模板+移动优化,快速打造专业网站
建站之星上传入口如何快速找到?
制作宣传网站的软件,小红书可以宣传网站吗?
文字头像制作网站推荐软件,醒图能自动配文字吗?
小说建站VPS选用指南:性能对比、配置优化与建站方案解析
建站主机选哪家性价比最高?
浅析上传头像示例及其注意事项
c# await 一个已经完成的Task会发生什么
专业网站建设制作报价,网页设计制作要考什么证?
如何通过多用户协作模板快速搭建高效企业网站?
网站制作模板下载什么软件,ppt模板免费下载网站?
如何选择高效响应式自助建站源码系统?
*请认真填写需求信息,我们会在24小时内与您取得联系。