将 int id 从一个页面发送到另一个通用 windows 应用程序

send an int id from one page to another universal windows app

我必须将从 page1.xaml 的休息 Web 服务获得的 ID 发送到 page2.xaml 我试过这段代码:

Page1.xaml.cs:

private async void Grid1_Tapped(object sender, TappedRoutedEventArgs e)
        {
            UriString2 = "URL";

            int x= rootObject.posts[0].id;
            string uri = string.Format("/Page2.xaml?x={1}",x);

            //adding Navigation Statement
            this.Frame.Navigate(typeof(Page2), new Uri(uri, UriKind.Relative));

        }

Page2.xaml.cs:

 private async void Page_Loaded(object sender, RoutedEventArgs e)
           {
            int xx = Convert.ToInt32(NavigationContext.QueryString["x"]);
        }

此代码无效,我有一个错误 "NavigationContext doesn't exist in this context",想念任何东西:(

感谢帮助

对于 UWP 中的导航,您可以使用 Frame.Navigate(typeof(TargetPage)); 方法从当前页面导航到目标页面。

你可以看我另一个问题的回答:Windows Phone 8.1 - Page Navigation

而在TargetPage 中,您可以使用NavigationParameter 属性 来获取导航参数。

private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
    var param = e.NavigationParameter;
}

在你的情况下,代码应该是:

来源页面:

Frame.Navigate(typeof(SecondPage), rootObject.posts[0].id);

目标页面:

private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
    string idStr = e.NavigationParameter as string;
    int id = int.Parse(idStr);
}