pass photochosen image from one page to another C#

青春壹個敷衍的年華 提交于 2019-12-22 12:21:04

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/22498022/pass-photochosen-image-from-one-page-to-another-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!