【问题标题】:Opening new page with data from selected item (JSON)使用来自所选项目 (JSON) 的数据打开新页面
【发布时间】:2014-11-13 02:13:25
【问题描述】:

我正在尝试使用 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));
    }

【问题讨论】:

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


    【解决方案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);
        }
    

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

    【讨论】:

      猜你喜欢
      • 2020-02-10
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      • 1970-01-01
      • 2015-08-31
      • 2020-12-28
      • 2011-04-23
      相关资源
      最近更新 更多