WPF刷新界面之坎坷路

   项目需要一个硬件检测功能,需要用到界面刷新,刚开始想用个定时器,对检测过的硬设定时添加后刷新界面。

但是很遗憾,定时器并不能进行刷新。后台检测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;
        }

    }
效果如下:

QQ截图20130726082335

posted @ 2013-07-26 08:24  幕三少  阅读(9517)  评论(13编辑  收藏  举报