【问题标题】:PhotoChooserTask + NavigationPhotoChooserTask + 导航
【发布时间】:2011-01-14 11:48:19
【问题描述】:

我为它们拍摄了两张图片并添加了事件 (MouseButtonDown)。 当第一张图片处理打开图库的事件时。第二个图像处理打开相机的事件。 当用户从图库中选择了他的图像时,我想导航到下一页。它的导航。但在完成导航过程之前,它会显示 MainPage 然后移至下一页。一旦用户从图库中选择图像,我不想显示 MainPage。 请帮忙。 提前致谢。

公共部分类 MainPage : PhoneApplicationPage

{
    PhotoChooserTask objPhotoChooser;
    CameraCaptureTask cameraCaptureTask;

    // Constructor
    public MainPage()
    {
        InitializeComponent();

        objPhotoChooser = new PhotoChooserTask();
        objPhotoChooser.Completed += new EventHandler<PhotoResult>(objPhotoChooser_Completed);

        cameraCaptureTask = new CameraCaptureTask();
        cameraCaptureTask.Completed += new EventHandler<PhotoResult>(objCameraCapture_Completed);         
    }

    void objPhotoChooser_Completed(object sender, PhotoResult e)
    {
        if (e != null && e.TaskResult == TaskResult.OK)
        {
            //Take JPEG stream and decode into a WriteableBitmap object                
            App.CapturedImage = PictureDecoder.DecodeJpeg(e.ChosenPhoto);

            //Delay navigation until the first navigated event
            NavigationService.Navigated += new NavigatedEventHandler(navigateCompleted);
        }
    }


    void navigateCompleted(object sender, EventArgs e)
    {
        //Do the delayed navigation from the main page
        this.NavigationService.Navigate(new Uri("/ImageViewer.xaml", UriKind.RelativeOrAbsolute));
        NavigationService.Navigated -= new NavigatedEventHandler(navigateCompleted);
    }


    void objCameraCapture_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            //Take JPEG stream and decode into a WriteableBitmap object                
            App.CapturedImage = PictureDecoder.DecodeJpeg(e.ChosenPhoto);

            //Delay navigation until the first navigated event
            NavigationService.Navigated += new NavigatedEventHandler(navigateCompleted);          
        }
    }


    protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
    {
        e.Cancel = true;
    }


    private void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        objPhotoChooser.Show();
    }


    private void image2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        cameraCaptureTask.Show();
    }

【问题讨论】:

    标签: windows-phone-7


    【解决方案1】:

    据我所知,当您使用其中一个选择器(例如照片库或相机)时,当您的应用程序激活时,它会将其带回您离开它的页面。我认为没有办法解决这个问题。您需要做的是在您的主页代码中捕获 Activating 事件并从那里导航到所需的页面。 现在我不完全确定如何将图像从 MainPage 传递到目标页面。导航服务中似乎没有用于存储此值的属性。但是您可以将其设置在应用程序范围的变量 ModelView 中,甚至可以将其存储在独立存储区域中。

    【讨论】:

      【解决方案2】:

      您可以通过导航到中间空白页面并让该中间页面启动任务来解决此问题。任务完成后,您可以照常导航到新页面,只有这个空白页面会在传输过程中显示。

      【讨论】:

        【解决方案3】:

        Chris 是正确的,一些任务将离开您的应用程序(有效地墓碑化它),并在用户从任务返回时重新激活您的应用程序。对于相机来说,这尤其困难,据我所知,没有简单的方法可以检测您何时从相机返回。此外,当连接到调试器或 Zune 软件时,相机也无法工作(至少在我的 HTC Surround 上是这样),这使得故障排除非常困难!

        在我的WP7 Barcode Scanning 应用程序中,我最终在PhoneApplicationService 类上使用了标志来帮助跟踪导航事件的来源。比如:

        PhoneApplicationService.Current.State["ReturnFromSampleChooser"] = true;

        然后,您可以在主页的 PhoneApplicationPage_LoadedOnNavigatedTo 方法中检查这些标志,并根据需要重定向到所需的页面。只需确保清除标志并注意不要在导航中造成任何循环,因为这可能会使您的应用无法通过认证(后退按钮必须始终正常工作)。

        有关如何使用摄像头和使用 PhoneApplicationService 设置/清除标志的示例,请查看Silverlight ZXing Barcode Library 的源代码。您可以下载完整源代码here 或浏览files online

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-02
          • 1970-01-01
          相关资源
          最近更新 更多