如何重定向到另一个xaml页面windows phone c#

时间:2013-09-15 22:14:49

标签: c# xaml windows-phone-8

我有以下代码可以找出发布请求(到外部api)的状态是否成功,如果是,它应该导航到我在windows phone app中创建的Interface.xaml页面。不确定哪个类处理在xaml页面之间导航

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; }
}

问题可以在if语句的HandleResponse方法中找到。

//编辑::

我现在扩展了PhoneApplicationPage类,它让我可以访问NavigationService ...但是,当我现在执行我的程序时,当我使用以下命令导航到新页面时:

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

它会引发运行时错误:

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.

任何想法?

2 个答案:

答案 0 :(得分:1)

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

试试这个

答案 1 :(得分:0)

在我工作的Windows Phone项目中,我们在基本视图模型上设置了一个方法,该方法将发布一个事件并在App.xaml.cs中像这样处理:

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

目的地是视图的URI。我们将导航参数存储在公共区域中,因为您只能将查询参数本身作为字符串传递。

App.xaml.cs包含我们用于导航的RootFrame元素。因此,我认为您正在寻找的关键部分是RootFrame.Navigate