【问题标题】:C# Passing image on select from one page to another xaml page in WindowsPhoneC#在WindowsPhone中将图像从一个页面传递到另一个xaml页面
【发布时间】:2013-10-16 19:45:41
【问题描述】:

使用 Photochooser 任务必须加载图像并立即将其传递到另一个页面。但执行以下代码时显示空白:

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

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

谢谢!

【问题讨论】:

  • 您是否尝试通过 ctor?新页面1(bmp);
  • page1Page1.xaml 真的是同一个页面吗?这可能是两个不同的实例。
  • 是的,它们是一样的。我打错了。但他们都是一样的。就是 Page1 p1 = new Page1();

标签: c# .net xaml windows-phone-7


【解决方案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);
      }

More about PhoneApplicationService.Current.State :)

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      page1 然后可以随时加载图像。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多