あなたがする必要があるのは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);
}
}
そして、それが受け入れられない場合 (そして、Winforms アプリのように WPF をプログラムしたい場合) は、Dispatcher.CurrentDispatcher を使用して、フォーム上のすべてのコントロールを手動で更新するときに 1 回呼び出すことができます。しかし、その方法は臭いです。