1

I have a windows phone app with a login process, that login process accesses an external API. The controller for the login button originally ran some code that instantly navigated to the dashboard page:

    private void LogInButton_Click(object sender, RoutedEventArgs e)
    {
        ...

        App.RootFrame.Navigate(new Uri("/Interface.xaml", UriKind.RelativeOrAbsolute));
    }

This works fine!

Later on, I thought it best to implement the actual connection to the api, check if the user details are correct and on that, redirect to the dashboard. for brevity I have taken out the api parts, but let's say this function gets passed all over the place as an Action delegate before being called in it's rightful place, the controller..

    ...
    // This method is also located in the controller class, but it is called by another class
    public void LoadDashboard( DataUpdateState data )
    {
        //data.AsyncResponse
        App.RootFrame.Navigate(new Uri("/Interface.xaml", UriKind.RelativeOrAbsolute));
    }

The thing is, the navigation method now no longer works, it fires a debug break on RootFrame_NavigationFailed.

what am I not understanding here?

Is there a way of finding out just why it loaded the navigation failed method in the App class

1 Answer 1

2

You can get more detail in the NavigationFailedEventArgs of the navigation failed event (in the Exception parameter).

The most probable cause is that you are try to call Navigate from a non ui thread. If that the case just use a dispatcher to dispatch it on the ui thread:

Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
        App.RootFrame.Navigate(new Uri("/Interface.xaml", UriKind.RelativeOrAbsolute));

            });
5
  • Wow, that worked. Thanks. So basically, you can't just say... change page, you have to make sure that a new thread is ready and geared to do something for you?
    – Jimmyt1988
    Sep 18, 2013 at 22:59
  • no you just have to make sure that you are calling from the ui thread and not from a backgroud thread (it's exactly like you cannot change any ui component property from a background thread) Sep 18, 2013 at 23:11
  • the only thing that Deployment.Current.Dispatcher.BeginInvoke do is send the request back to the ui thread not starting anything Sep 18, 2013 at 23:12
  • What probably happened insyou used a HttpWebRequest and the callback of a HttpWebRequest is on another thread (it's done so that you don't block the ui and the ui stay responsive) Sep 18, 2013 at 23:19
  • nice one, makes sense
    – Jimmyt1988
    Sep 18, 2013 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.