2

I need to call an xaml file from a click event and I am using c# for my development. I have created the Xaml file and done with the design part in it, now I need to call this xaml file from my application. I tried the following

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

but it gives me the following error,

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

What's going wrong with this? I am calling this xaml file in between one of my functions where I need to show this screen designated with .xaml.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254

1 Answers1

3

Seems like you're trying to call the Navigate method from a background thread. Call it from the UI thread instead, like this:

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

Edit from comments:

Dispatcher.BeginInvoke(() =>
{
   if(MessageBox.Show("message", "title", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
   {
       NavigationService.Navigate(new Uri("/xxx.xaml", UriKind.Relative));
   }
});
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • yes actually all the contents tat start my application runs in background this was the thing were my application starts void worker_DoWork(object sender, DoWorkEventArgs e) { Deployment.Current.Dispatcher.BeginInvoke(() => { // some dialog box handelled // where when i click on ok button of this message box i need to show the screen xxx.xaml. } and this worker_dowork as been called to a bacground and run async till i get that message box. now wat i need is when i click on the ok button in the message box it should navigate to that xxx.xaml thanks. – user1665577 Sep 12 '12 at 12:26
  • I've updated the code, but not sure if that's what you mean. Are you still getting the exception when you use the NavigationService within a dispatcher block? – keyboardP Sep 12 '12 at 12:42
  • no i am getting different error saying tat, 1)An object reference is required for the non-static field, method, or property 'System.Windows.Navigation.NavigationService.Navigate(System.Uri)' and 2)Cannot convert lambda expression to type 'System.Delegate' because it is not a delegate type – user1665577 Sep 12 '12 at 12:59
  • so i tried adding the foolowing code insted of tat as suggested in forum thread whi i saw, (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("xxx.xaml", UriKind.Relative)); for this i am getting null reference exception – user1665577 Sep 12 '12 at 13:04
  • You might be getting that error because it's your first page. Follow the method in this blog post http://blogs.msdn.com/b/ptorr/archive/2010/08/28/redirecting-an-initial-navigation.aspx – keyboardP Sep 12 '12 at 15:43
  • Hi as said my prob is, i have an asyc thread running to load the data to my main page. Now the problem is i need to call another screen where i need to get some information from the user. once the content is loaded i need to navigate to another screen, but inside thread i couldn't call the xaml file which i need, so is there any way to call the file once the thread is stopped ?. here is the code which we used, public override void PrepareView(IDictionary page) { base.PrepareViewModel(page); LoginSuccess(null); Messages(); fillContent(page); } – user1665577 Sep 14 '12 at 05:08
  • fillcontent(page); is the last function called in my thread. i need to call another screen once this thread is completed. so how can i over come this problem in windows ? Thanks – user1665577 Sep 14 '12 at 05:09