【发布时间】:2012-03-29 18:42:02
【问题描述】:
我正在为 Windows 8 编写一个 Windows Store App 玩具应用程序。
它只有一个带有TextBlock 的xaml 页面。该页面的 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))
真正的问题是我正在更新类 MyTimer 的属性,而不是 GUI 本身, 我想不通,但我认为解决方案应该使用 this one 之类的东西。
【问题讨论】:
标签: c# xaml windows-runtime windows-store-apps