使用所选项目(JSON)中的数据打开新页面

时间:2014-11-12 15:29:48

标签: c# json xaml windows-phone-8.1

我正在尝试使用JSON创建一个简单的Windows Phone 8.1应用程序。 我的问题是,在点击ListView中的项目后,我可以打开第二页,其中包含与所选项目相关的数据吗?

public MainPage()
    {

        this.InitializeComponent();

        this.NavigationCacheMode = NavigationCacheMode.Required;



        var client = new HttpClient();
        var task = client.GetAsync("json")
          .ContinueWith((taskwithresponse) =>
          {
              var response = taskwithresponse.Result;
              var jsonString = response.Content.ReadAsStringAsync();
              jsonString.Wait();
              dynamic content = JsonConvert.DeserializeObject<RootObject>(jsonString.Result);
              Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(() =>
              {
                  List.ItemsSource = content.value.items;


              })).AsTask().Wait(); 
          });



    }


    private void listView_ItemClick(object sender, RoutedEventArgs e)
    {

        this.Frame.Navigate(typeof(SecondPage));
    }

1 个答案:

答案 0 :(得分:1)

You can pass parameters when navigating如下:

page.NavigationService.Navigate(new Uri("/Views/Page.xaml?parameter=test", UriKind.Relative));

// and ..

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    // NavigationEventArgs returns destination page
    Page destinationPage = e.Content as Page;
    if (destinationPage != null) {

        // Change property of destination page
        destinationPage.PublicProperty = "String or object..";
    }
}

这是我在项目中传递参数的方法:

    private void ItemView_ItemClick(object sender, ItemClickEventArgs e)
    {
        var albumId = ((Album)e.ClickedItem).Id;
        if (!Frame.Navigate(typeof(AlbumPage), albumId))
        {
            var resourceLoader = ResourceLoader.GetForCurrentView("Resources");
            throw new Exception(resourceLoader.GetString("NavigationFailedExceptionMessage"));
        }
    }

这就是我使用它的方式:

    private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
    {
        var artist = dbContext.GetWithChildren(new Artist { Id = (int)e.NavigationParameter } );

        this.GetArtistInfo(artist);
    }

我不知道这是否是最佳解决方案,但这对我有用。