我正在使用 SerialPort 类中的事件侦听器从串行端口读取数据。在我的事件处理程序中,我需要使用通过串行端口传入的 xml 数据来更新窗口中的许多 (30-40) 控件。
我知道我必须使用 myControl.Dispatcher.Invoke() 来更新它,因为它位于不同的线程上,但是有没有办法一起更新大量控件,而不是为每个控件进行单独的 Invoke 调用(即 myCon1.Dispatcher. Invoke()、myCon2.Dispatcher.Invoke() 等)?
我正在寻找类似在容器上调用 Invoke 并单独更新每个子控件的方法,但我似乎无法弄清楚如何完成此操作。
Thanks!
你需要做的是使用MVVM http://msdn.microsoft.com/en-us/magazine/dd419663.aspx.
您将控件绑定到 ViewModel 上的公共属性。您的VM可以侦听串行端口,解析出xml数据,更新其公共属性,然后使用INotifyPropertyChanged http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx告诉 UI 更新其绑定。
我建议您采用此路线,因为您可以批量通知,并且如果必须的话,可以使用 Dispatcher 在 UI 线程上调用事件。
UI:
<Window ...>
<Window.DataContext>
<me:SerialWindowViewModel />
</Window.DataContext>
<Grid>
<TextBlock Text="{Binding LatestXml}/>
</Grid>
</Window>
串行窗口视图模型:
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 调用一次。但这个方法很臭。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)