开发者

PhotoChooserTask + Navigation

I taken two Images & added event (MouseButtonDown) for them. When first image handles event to open Gallery. Second image handles events for open camera. When user has choosed his image from the gallery, I want to navigate to next page. Its navigates. But before completing navigation process, it displays MainPage & then moves toward next page. I didnt want to display the MainPage once user chooses the image from the gallery. Plz help. Thanks in advance.

public partial class 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_Comple开发者_Go百科ted);         
    }

    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();
    }


To my knowledge when you use one of the choosers, like the Photo gallery or the camera, when your application is activating it will take it back to the page you left it. I don't think there is a way to get around this itself. What you would have to do is catch the Activating event in your main page code and Navigate to the desired page from there. Now I am not completely sure how you would pass the image from the MainPage to the target page. It does not look like there is a property in the Navigation service to store this value. But you could either set it in an application wide variable, ModelView or even store it in the Isolated Storage area.


You could work around this by navigating to an intermediate blank page and have that intermediate page launch the tasks. When the tasks are completed you can then navigate as normal to your new page and only this blank page will show in transit.


Chris is correct that some of the tasks will navigate away from your app (effectively tombstoning it) and will the re-activate your application when the user returns from the task. For the camera this is particularly difficult, as to my knowledge there is no simple way to detect when you are returning from the camera. Also the camera doesn't work when attached to the debugger or Zune software (at least this is true on my HTC Surround), which makes troubleshooting quite difficult!

In my WP7 Barcode Scanning application I ended up using flags on the PhoneApplicationService class to help track where the navigation events are coming from. Something like:

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

You can then check for these flags in the PhoneApplicationPage_Loaded or OnNavigatedTo method of your main page and redirect to the desired page as needed. Just make sure to clear the flag and be careful to not cause any loops in the navigation, as that might make your app fail certification (back button must ALWAYS work correctly).

For an example of how to use the camera and set/clear flags using PhoneApplicationService check out the source code for the Silverlight ZXing Barcode Library. You can download the full source here or browse the files online.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜