0

我导航到一个新页面,并在该页面上设置DataContext页面加载事件。

public MyPage()
{
  this.InitializeComponent();
  this.Loaded += MyPage_Loaded;

} 
void MyPage_Loaded(object sender, RoutedEventArgs e)
{
  this.DataContext= myModel;

}

在设置数据上下文时,很多时候我得到System.AccessViolation异常并且应用程序退出。

可能是什么问题呢?

编辑

我试过以下

1) 设置NavigationCacheModeDisabled

2) 将页面设置为基本页面,即删除继承自LayoutAwarePage

我仍然得到那个例外

4

1 回答 1

0

您必须确保 UI 绑定到的数据仅由 UI 线程更改。你可以像

    public static async Task RunOnUIThread(DispatchedHandler action)
    {
        await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, action);
    }
于 2014-12-31T15:35:04.177 回答