私はWindows8用のWindowsストアアプリのおもちゃのアプリケーションを書いています。これには、が付いたxamlページが1つだけありますTextBlock
。このページには、次のようなクラスMyTimerがありますDataContext
。
this.DataContext = new MyTimer();
MyTimer
を実装INotifyPropertyChanged
し、プロパティの更新はTime
タイマーで行われます。
public MyTimer(){
TimerElapsedHandler f = new TimerElapsedHandler(NotifyTimeChanged);
TimeSpan period = new TimeSpan(0, 0, 1);
ThreadPoolTimer.CreatePeriodicTimer(f, period);
}
と
private void NotifyTimeChanged(){
if (this.PropertyChanged != null){
this.PropertyChanged(this, new PropertyChangedEventArgs("Time"));
}
}
時間にTextBlock
データバインディングがあります
<TextBlock Text="{Binding Time}" />
アプリケーションを実行すると、次の例外が発生します。
System.Runtime.InteropServices.COMException was unhandled by user code
メッセージ付き
The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
本当の問題は、GUI自体ではなく、クラスMyTimerのプロパティを更新していることです。それを理解することはできませんが、ソリューションではこのようなものを使用する必要があると思います。