1

我有以下代码可以确定发布的请求(到外部 api)的状态是否成功,如果是,它应该导航到我在 windows phone 应用程序中创建的 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.

有任何想法吗?

4

2 回答 2

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

尝试这个

于 2013-10-22T11:42:37.297 回答
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.

于 2013-09-15T22:24:12.673 回答