あなたがする必要があるのは、MVVMです。
コントロールをViewModelのパブリックプロパティにバインドします。 VMはシリアルポートを監視し、xmlデータを解析し、パブリックプロパティを更新してから、INotifyPropertyChangedを使用してUIにバインディングを更新するよう指示できます。
バッチ通知と、必要な場合は、Dispatcherを使用してUIスレッドでイベントを呼び出すことができるので、このルートを提案したいと思います。
UI:
<Window ...>
<Window.DataContext>
<me:SerialWindowViewModel />
</Window.DataContext>
<Grid>
<TextBlock Text="{Binding LatestXml}/>
</Grid>
</Window>
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);
}
}
そして、それはあなたに受け入れられない(そしてあなたはそのAのWinFormsアプリのようWPFをプログラムする)場合は、あなたがディスパッチャを使用することができます。フォーム上のすべてのコントロールを手動で更新するときに、一度呼び出すCurrentDispatcher。しかし、その方法は悪臭を放つ。
ありがとうございました!これは私が実装できるもののようです! – Klay