简体   繁体   中英

WP7 Passing data between pages

The basic navigation functionality of my app consists of selecting an item from a listbox, determine what item was selected and get its id value. Pass the id value to the next page and add it to an api url for use in a webclient to pull down new data. Here's what I have now.

Determine item selected and navigate to secondary page.

    public void genreSelectedHandler(object sender, RoutedEventArgs e)
    {
        ResultGenre data = (sender as TextBlock).DataContext as ResultGenre;
        ListBoxItem pressedItem = this.listGenres.ItemContainerGenerator.ContainerFromItem(data) as ListBoxItem;
        if (pressedItem != null)
        {
            string genreID = "http://api.beatport.com/catalog/3/beatport/genre?id=" + data.id;

            this.NavigationService.Navigate(new Uri("/Pages/GenrePage.xaml?genreID=" + genreID, UriKind.Relative));
        }
    }

On the secondary page i can use OnNavigatedTo to get the url from the previous page, but the part i'm stuck on is plugging that url into the webclient.

    public GenrePage()
    {
        InitializeComponent();

        // WebClient jsonRelease
        WebClient jsonGenres = new WebClient();
        Uri apiGenre = new Uri("URL from previous page goes here");
        jsonGenres.DownloadStringCompleted += new DownloadStringCompletedEventHandler(jsonGenres_GetDataCompleted);
        jsonGenres.DownloadStringAsync(apiGenre);

    }

    // Textblock data from main page
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        string genreID = "";
        if (NavigationContext.QueryString.TryGetValue("genreID", out genreID))

        base.OnNavigatedTo(e);
    }

Seems like the only thing i can do is add the url to an existing control on the secondary page. Is there a better way to accomplish what I'm trying to do? I've read a little bit on MVVM framework. Would that be a better way to handle this? If so how would that work? Thanks for the help.

Does this part of the Uri "http://api.beatport.com/catalog/3/beatport/genre?id=" ever change? If it doesn't, instead of passing the whole thing to GenrePage consider only passing data.id in the query string. Then create the Uri on the destination page.

If you must do it the way you've shown in your snippet, do the following:

string genreID = @"http://api.beatport.com/catalog/3/beatport/genre?id=" + data.id;
this.NavigationService.Navigate( new Uri( "/Pages/GenrePage.xaml?genreID=" + 
  Uri.EscapeDataString( genreID ), UriKind.Relative ) );

In GenrePage.xaml.cs

public partial class GenrePage : PhoneApplicationPage
{
  public GenrePage()
  {
    InitializeComponent();
  }

  protected override void OnNavigatedTo( NavigationEventArgs e )
  {
    base.OnNavigatedTo( e );

    string genreID = String.Empty;
    if( NavigationContext.QueryString.TryGetValue( "genreID", out genreID ) ) {
      LaunchWebClient( genreID );
    }
  }

  private void LaunchWebClient( string genreID )
  {
    WebClient jsonGenres = new WebClient();
    Uri apiGenre = new Uri( genreID );

    jsonGenres.DownloadStringCompleted += new 
      DownloadStringCompletedEventHandler( jsonGenres_GetDataCompleted );
    jsonGenres.DownloadStringAsync( apiGenre );
  }
}

Uri.EscapeDataString will insert the appropriate escape characters into the query string. The query string is automatically unescaped on the destination page, so you don't need to do anything special there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM