简体   繁体   中英

pass photochosen image from one page to another C#

I know how to pass data through NavigationService.Navigate(new Uri("/Second.xaml?msg=mesage", UriKind.Relative));

The question is, how can I pass an image selected from the library to another page?

To select an image, I use the PhotoChooserTask and in the event where it is completed I have this:

 private void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        if (e.ChosenPhoto != null)
        {
            BitmapImage image = new BitmapImage();
            image.SetSource(e.ChosenPhoto);
            this.img.Source = image;
        }
    }

How can I send the chosen photo to another page?

You can only pass string in Query String. To pass an image source from one page to another PhoneApplicationservice is easiest way. Here is the simple example to pass image from one page to another.

 private void photoChooserTask_Completed(object sender, PhotoResult e)
        {
            if (e.ChosenPhoto != null)
            {
             **//Edits**
            if (PhoneApplicationService.Current.State.ContainsKey("Image"))
                    if (PhoneApplicationService.Current.State["Image"] != null)
                        PhoneApplicationService.Current.State.Remove("Image");
                BitmapImage image = new BitmapImage();
                image.SetSource(e.ChosenPhoto);
                this.img.Source = image;
     PhoneApplicationService.Current.State["Image"] = image;
            }
        }

//On second page
//I assume you want to image on page load
protected override void OnNavigatedTo(NavigationEventArgs e)
    {
      BitmapImage image = new BitmapImage();
     image  =(BitmapImage )PhoneApplicationService.Current.State["Image"]
     PhoneApplicationService.Current.State.Remove("Image");
     this.img.Source = image;
    }

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