1

I'm trying to create a simple windows phone 8.1 application using JSON. And my question is how after clicking an item from the ListView can I open a second page with data related to the selected item?

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));
    }
Konrad
  • 15
  • 3

1 Answers1

1

You can pass parameters when navigating as follows:

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..";
    }
}

This is how I pass a parameter in my project:

    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"));
        }
    }

And this is how I use it:

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

        this.GetArtistInfo(artist);
    }

I don't know if it's the best solution, but this is what works for me.

Community
  • 1
  • 1
Benjamin Diele
  • 1,177
  • 1
  • 10
  • 26