如何重定向到另一个 XAML 页面 Windows Phone C#

本文关键字:页面 Windows Phone XAML 另一个 重定向 | 更新日期: 2023-09-27 18:31:20

我有以下代码,可以确定已发布的请求(对外部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.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.

有什么想法吗?

如何重定向到另一个 XAML 页面 Windows Phone C#

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

试试这个

在我参与的一个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 .