WPF刷新界面之坎坷路
来源:互联网 发布:网络直播模式 编辑:程序博客网 时间:2024/04/30 05:11
项目需要一个硬件检测功能,需要用到界面刷新,刚开始想用个定时器,对检测过的硬设定时添加后刷新界面。
但是很遗憾,定时器并不能进行刷新。后台检测List数据里面已经添加了很多了很多数据了,就是不能显示到界面
然后百度一下“WPF刷新界面”找了好几篇文章,大致都是如下代码:
public class UIHelper : Application { //刷新界面 private static DispatcherOperationCallback exitFrameCallback = new DispatcherOperationCallback(ExitFrame); public static void DoEvents() { DispatcherFrame nestedFrame = new DispatcherFrame(); DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke( DispatcherPriority.Background, exitFrameCallback, nestedFrame); Dispatcher.PushFrame(nestedFrame); if (exitOperation.Status != DispatcherOperationStatus.Completed) { exitOperation.Abort(); } } private static object ExitFrame(object state) { DispatcherFrame frame = state as DispatcherFrame; frame.Continue = false; return null; } }
我把代码手敲下来,(注意不是复制,应为我不是太懂,这里也劝大家不要轻易复制,因为复制就算解决问题
了下次你还是不知道怎么回事)。在我添加数据后面调用UIHelper .DoEvents(),但是还是没有反应。依然不
刷新,我就郁闷了,别人可以解决为啥到我这就不能用了呢,请教各位大神,这个怎么用啊?有什么前提条件
吗?请我告诉我……
然后继续找啊找啊,在CSDN上找到了类似的问题。原来list<T>没有数据更新的功能,这里面需要用
ObservableCollection<T> 类 或 BindingList<T> 类 代替 List 类,看ObservableCollection<T>在帮助文档里的说明,
这个提供自动更新数据的接口,可以自动向控件发送更新消息,果断一实验。OK成功显示。
public partial class Window2 : Window { DispatcherTimer _mainTimer; public Window2() { InitializeComponent(); _mainTimer = new DispatcherTimer(); _mainTimer.Interval = TimeSpan.FromSeconds(1); _mainTimer.Tick += new EventHandler(_mainTimer_Tick); _mainTimer.IsEnabled = true; } void _mainTimer_Tick(object sender, EventArgs e) { if (progressBar1.Value == progressBar1.Maximum) progressBar1.Value = 0; progressBar1.Value++; DeviceCheckInfo device = new DeviceCheckInfo(); device.CheckResult = true; device.Name = "发卡器" + progressBar1.Value; device.CheckContent = "打卡短短"; Dispatcher.BeginInvoke(new Action(() => { if (list != null) list.Add(device); lbtest.ItemsSource = list; // UIHelper.DoEvents(); })); } ObservableCollection<DeviceCheckInfo> list; private void Window_Loaded(object sender, RoutedEventArgs e) { list = new ObservableCollection<DeviceCheckInfo>(){ new DeviceCheckInfo {Name="三合一读卡器",CheckContent="duankou",CheckResult=true }, new DeviceCheckInfo {Name="发卡器",CheckContent="tongdao",CheckResult=false }, new DeviceCheckInfo {Name="打印机",CheckContent="youzhi" ,CheckResult=true} }; lbtest.ItemsSource = list; } private void button1_Click(object sender, RoutedEventArgs e) { DeviceCheckInfo device = new DeviceCheckInfo(); device.CheckResult = true; device.Name = "发卡器" + progressBar1.Value; device.CheckContent = "打卡短短"; list.Add(device); lbtest.ItemsSource = list; } }
效果如下:
<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- WPF刷新界面之坎坷路
- WPF界面刷新
- WPF强制刷新界面
- 坎坷之路
- opencv坎坷之路
- C游戏之路坎坷
- JAVA坎坷求职之路
- 封神之路注定坎坷
- CSS的坎坷之路
- WPF多线程刷新界面控件1
- WPF 开启多线程刷新UI 界面
- 艰辛坎坷的360崛起之路
- 回顾MySpace架构的坎坷之路
- 回顾MySpace架构的坎坷之路
- 回顾MySpace架构的坎坷之路
- 坎坷的组建RAID之路
- 回顾MySpace架构的坎坷之路
- 回顾MySpace架构的坎坷之路
- JS解析JSON
- mysql中datetime类型字段怎么取出来?
- struts 2的文件下载
- 黑马程序员----【张孝祥7K面试题】二银行业务调度系统视频学习笔记
- HDU 1166 敌兵布阵
- WPF刷新界面之坎坷路
- oracle的外部表
- win7玩游戏全屏
- 虚拟机VMware中Linux下安装VMwareTools
- android自定义导航、可滚动(添加子导航、栏目)
- Unrecognized Windows Sockets error: 0: JVM_Bind
- C++ 虚析构函数
- oracle的sql_trace
- NGUI的UITable