2

我需要从单击事件中调用一个 xaml 文件,并且我正在使用 c# 进行开发。我已经创建了 Xaml 文件并完成了其中的设计部分,现在我需要从我的应用程序中调用这个 xaml 文件。我尝试了以下

NavigationService.Navigate(new Uri("/xxx.xaml", UriKind.Relative));

但它给了我以下错误,

unauthorized access exception was unhandled. 
Invalid cross-thread access.

这有什么问题?我在我的一个函数之间调用这个 xaml 文件,我需要在其中显示用.xaml.

4

1 回答 1

3

好像您正在尝试Navigate从后台线程调用该方法。而是从 UI 线程调用它,如下所示:

Dispatcher.BeginInvoke(() =>
{
   NavigationService.Navigate(new Uri("/xxx.xaml", UriKind.Relative));
});  

从评论编辑:

Dispatcher.BeginInvoke(() =>
{
   if(MessageBox.Show("message", "title", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
   {
       NavigationService.Navigate(new Uri("/xxx.xaml", UriKind.Relative));
   }
});
于 2012-09-12T11:48:19.763 回答