1
votes

I have the following code that finds out if the status of the posted request (to an external api) was successful, and if so, it should navigate to the Interface.xaml page I have created in the windows phone app. Not sure which class deals with navigating between xaml pages

public bool UsernameAndPassword(string username, string password)
{
    data = "grant_type=" + GRANTTYPE + "&username=" + username + "&password=" + password + "&client_id=" + CLIENTID + "&redirect_uri=" + REDIRECTURI + "&client_secret=" + CLIENTSECRET;
    return true;
}

public bool Authenticate()
{
    // form the URI
    UriBuilder fullUri = new UriBuilder(urlPath);
    fullUri.Query = data;

    // initialize a new WebRequest
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fullUri.Uri);
    request.Method = "POST";

    // set up the state object for the async request
    DataUpdateState dataState = new DataUpdateState();
    dataState.AsyncRequest = request;

    // start the asynchronous request
    request.BeginGetResponse(new AsyncCallback(HandleResponse),
        dataState);

    return true;
}

private void HandleResponse(IAsyncResult asyncResult)
{
    // get the state information
    DataUpdateState dataState = (DataUpdateState)asyncResult.AsyncState;
    HttpWebRequest dataRequest = (HttpWebRequest)dataState.AsyncRequest;

    // end the async request
    dataState.AsyncResponse = (HttpWebResponse)dataRequest.EndGetResponse(asyncResult);
    if (dataState.AsyncResponse.StatusCode.ToString() == "OK")
    {
        // What needs to go here to navigate to another xaml page?
        // something like this? - NavigationService.Navigate(new Uri("Interface.xaml", UriKind.Relative));    
    }

}

public class DataUpdateState
{
    public HttpWebRequest AsyncRequest { get; set; }
    public HttpWebResponse AsyncResponse { get; set; }
}

The problem can be found within the HandleResponse method in the if statement.

// EDIT::

I have now extended the PhoneApplicationPage class which has given me access to NavigationService ... However, when I now execute my program, when I navigate to the new page using:

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

it throws a runtime error:

An exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll but was not handled in user code

If there is a handler for this exception, the program may be safely continued.

any ideas?

2

2 Answers

1
votes
(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/UserDetail.xaml", UriKind.Relative));

Try this

0
votes

In a Windows Phone project I worked on, we set up a method on the base view model that would publish an event and get handled like this in App.xaml.cs:

Messenger.Default.Register<NavigateMessage>(this, (navigateMessage) =>
{
  Deployment.Current.Dispatcher.BeginInvoke(() =>
  {
    ApplicationParameter = navigateMessage.Parameter;
    RootFrame.Navigate(navigateMessage.Destination);
  });
});

The destination is the view's URI. We stored the navigation parameter in a common area since you are only able to pass query parameters around as strings natively.

App.xaml.cs contains the RootFrame element that we use for navigation. Thus, I think the key piece you are looking for is RootFrame.Navigate.