0

使用 Photochooser Task 必须加载图像并立即将其传递到另一个页面。但是在实现以下代码时显示空白:

private void LoadPicture_Click(object sender, RoutedEventArgs e)
{
    PhotoChooserTask photoChooserTask;
    photoChooserTask = new PhotoChooserTask();
    photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
    photoChooserTask.Show();

    NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
}

void photoChooserTask_Completed(object sender, PhotoResult e)
{
    if (e.TaskResult == TaskResult.OK)
    {
        System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
        bmp.SetSource(e.ChosenPhoto);

        Page1 p1 = new Page1();
        p1.encodeImg.Source = bmp;
    }
    else
    {
        MessageBox.Show("Image Loading Failed.");
    }
}

请建议解决上述问题。

谢谢!

4

3 回答 3

1

你解决了吗?如果你没有,你可以使用这样的东西。在您的 photoChooseTask 处理程序中保存 bitmapImage

 PhoneApplicationService.Current.State["yourparam"] = bmp;

然后在你的 Page1 你得到 bitmapImage

BitmapImage bitmapGet = PhoneApplicationService.Current.State["yourparam"] as BitmapImage;

这是你应该如何使用它。

void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
            bmp.SetSource(e.ChosenPhoto);
            //save the bitmapImage 
            PhoneApplicationService.Current.State["yourparam"] = bmp;


        }
        else
        {
            MessageBox.Show("Image Loading Failed.");
        }


        NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
    }

您的第 1 页

      protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
      {
           //get the bitmapImage
           BitmapImage bitmapGet = PhoneApplicationService.Current.State["yourparam"] as BitmapImage;

           //set the bitmpaImage 
           img.Source = bitmapGet;

           base.OnNavigatedTo(e);
      }

更多关于 PhoneApplicationService.Current.State :)

于 2013-10-17T05:32:57.807 回答
0

实现它的另一种方法是先将图像保存在isolateStorage 中,然后将文件路径作为字符串参数传递给您的page1。

page1 然后可以在需要的任何时候加载图像。

于 2014-04-08T00:35:27.713 回答
0

导航必须在完成事件后完成,photochooser.show() 抑制主应用程序线程,因此您只能在获取图像流后传递它。因此,将导航语句转移到完成的事件处理程序并使用isolatedstoragesettings.applicationsettings 来存储图像并将其返回到第二页。

于 2013-10-17T07:47:49.767 回答