C# 在 WindowsPhone 中将图像从一个页面传递到另一个 xaml 页面

本文关键字:一个 页面 xaml 另一个 WindowsPhone 图像 | 更新日期: 2023-09-27 18:35:44

使用照片选择器任务,图像必须加载并立即传递到另一个页面。但在实现以下代码时显示空白:

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.");
    }
}

请在解决上述问题时提出建议。

谢谢!

C# 在 WindowsPhone 中将图像从一个页面传递到另一个 xaml 页面

你解决了吗? 如果你还没有,你可以使用这样的东西。 在你的照片选择任务处理程序保存位图图像

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

然后在您的 Page1 中,您将获得位图图像

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 :)

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

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

然后,Page1 可以随时加载图像。