WPF:通过调度程序更新多个控件 [英] wpf: update multiple controls via dispatcher

查看:97
本文介绍了WPF:通过调度程序更新多个控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SerialPort类中的事件监听器从串行端口读取数据。在我的事件处理程序中,我需要通过串行端口上的xml数据更新窗口中的许多(30-40)控件。

I'm reading data from a serial port using an event listener from the SerialPort class. In my event handler, I need to update many (30-40) controls in my window with xml data coming over the serial port.

我知道我必须使用myControl .Dispatcher.Invoke()进行更新,因为它位于不同的线程上,但是有一种方法可以一起更新许多控件,而不是对每个控件进行单独的Invoke调用(例如,myCon1.Dispatcher.Invoke(),myCon2.Dispatcher .invoke()等)?

I know that I must use myControl.Dispatcher.Invoke() to update it since it's on a different thread, but is there a way to update lots of controls together, rather than doing a separate Invoke call for each (i.e. myCon1.Dispatcher.Invoke(), myCon2.Dispatcher.Invoke(), etc)?

我正在寻找类似在容器上调用Invoke并分别更新每个子控件的东西,但我似乎无法找出如何做到这一点。

I'm looking for something like calling Invoke on the container, and updating each child control individually, but I can't seem to work out how to accomplish this.

谢谢!

推荐答案

您需要做的是< a href = http://msdn.microsoft.com/en-us/magazine/dd419663.aspx rel = nofollow noreferrer> MVVM 。

您将控件绑定到ViewModel上的公共属性。您的VM可以侦听串行端口,解析xml数据,更新其公共属性,然后使用 INotifyPropertyChanged 来通知UI更新其绑定。

You bind your controls to public properties on a ViewModel. Your VM can listen to the serial port, parse out the xml data, update its public properties, and then use INotifyPropertyChanged to tell the UI to update its bindings.

我建议您使用此路由,因为您可以批量处理通知,并且,如果需要,可以使用Dispatcher在UI线程上调用事件。

I'd suggest this route as you can batch notifications and, if you have to, use the Dispatcher to invoke the event on the UI thread.

UI:

<Window ...>
  <Window.DataContext>
    <me:SerialWindowViewModel />
  </Window.DataContext>
  <Grid>
    <TextBlock Text="{Binding LatestXml}/>
  </Grid>
</Window>

SerialWindowViewModel:

SerialWindowViewModel:

public class SerialWindowViewModel : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;

  public string LatestXml {get;set;}

  private SerialWatcher _serialWatcher;

  public SerialWindowViewModel()
  {
    _serialWatcher = new SerialWatcher();
    _serialWatcher.Incoming += IncomingData;
  }

  private void IncomingData(object sender, DataEventArgs args)
  {
    LatestXml = args.Data;
    OnPropertyChanged(new PropertyChangedEventArgs("LatestXml"));
  }

  OnPropertyChanged(PropertyChangedEventArgs args)
  {
    // tired of writing code; make this threadsafe and
    // ensure it fires on the UI thread or that it doesn't matter
    PropertyChanged(this, args);
  }
}

而且,如果那是您不可接受的(并且您希望像Winforms应用程序一样对WPF进行编程),则可以使用Dispatcher.CurrentDispatcher调用一次,同时手动更新表单上的所有控件。但是这种方法很臭。

And, if that isn't acceptable to you (and you want to program WPF like its a winforms app) you can use Dispatcher.CurrentDispatcher to Invoke once while you manually update all controls on your form. But that method stinks.

这篇关于WPF:通过调度程序更新多个控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆