简体   繁体   中英

WP7 passing multiple pieces of data between pages

In my app I am building my own media player. When a user selects a song to play want to be able to pass the link to the sample media and the metadata associated with it (Artist, Track, Album Art, etc.) The part I'm stuck on is how to group all the data and pass it to the media player page. Here's what i have so far.

Determine what item was selected and add data to query string.

UPDATED

    public void musicSampleSelectedHandler(object sender, RoutedEventArgs e)
    {
        Track selected = (sender as Image).DataContext as Track;
        ListBoxItem pressedItem = this.listReleaseMain.ItemContainerGenerator.ContainerFromItem(selected) as ListBoxItem;
        if (pressedItem != null)
        {
            string _rT = selected.title;
            string _rN = selected.release.name;
            //string _rA = selected.artists; ????
            string _rI = selected.images.large.url;
            string _rS = selected.sampleUrl;

            this.NavigationService.Navigate(new Uri("/Pages/MediaPage.xaml?releaseName=" + _rN + "&releaseTrack=" + _rT + "&releaseImage=" + _rI
                + "&releaseSample=" + _rS, UriKind.Relative));
        }
    }

OnNavigatedTo method to pull data out of the query string

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

        string releaseName = String.Empty;
        string releaseImg = String.Empty;
        string releaseUrl = String.Empty;

        if (NavigationContext.QueryString.TryGetValue("releaseUrl", out releaseUrl))
        {
            sampleMedia.Source = new Uri(releaseUrl, UriKind.Absolute);
        }
    }

I'm not sure if I can use the query sting to pass multiple pieces to the media player, or if I to do something different to pass the data to the other page. All of my data comes from the web using a webclient. Thanks for the help.

QueryString is just a Dictionary of the parameters passed in via the Uri. The Uri uses the standard syntax of passing in parameters delimited by & . So in your example, if you had something like:

this.NavigationService.Navigate(new Uri("/Pages/MediaPage.xaml?releaseUrl=" + releaseUrl + "&releaseImg=" + releaseImg , UriKind.Relative)); 

then you can then parse this out using something like:

if (NavigationContext.QueryString.TryGetValue("releaseUrl", out releaseUrl))   
{   
    sampleMedia.Source = new Uri(releaseUrl, UriKind.Absolute);   
} 

if (NavigationContext.QueryString.TryGetValue("releaseImg", out releaseImg))   
{   
    // do something with releaseImg
} 

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