PhotoChooserTask +导航

时间:2011-01-14 11:48:20

标签: windows-phone-7

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

公共部分类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();
    }

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

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

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

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