您需要做的是使用MVVM。
您将控件绑定到 ViewModel 上的公共属性。您的 VM 可以侦听串行端口、解析 xml 数据、更新其公共属性,然后使用INotifyPropertyChanged告诉 UI 更新其绑定。
我建议您使用这条路线,因为您可以批量通知,如果必须,请使用 Dispatcher 在 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 调用一次。但这种方法很臭。