1

So I am trying to do a very simple thing in WP7 like:

A button in MainPage will launch camera, and when camera successfully take a picture, I want to pass the picture to SecondPage and launch it.

Here's my code:

Within the MainPage constructor, I initialize the camera task and set the delegate:

camTask.Completed += new EventHandler<PhotoResult>(camTask_Completed);

Then I implemented the camTask_Completed

 void camTask_Completed(object sender, PhotoResult e)
    {
        //throw new NotImplementedException();
        
        img = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
        NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative));
    }

The application will run without error until after I pressed "accept" after I took the picture.

The exception says:

Exception {"Navigation is not allowed when the task is not in the foreground."} System.Exception {System.InvalidOperationException}

Which I understand is that I shouldn't launch the SecondPage within the camTask_Completed method.

Then my question is: How to launch another page on the result of EventHandler?

Thanks

UPDATE: (For answer to this sub-question, please refer to this comment in this page)

I found another error right after I click the button(to launch the camera):

It throw an exception said:

"Type 'System.Windows.Media.Transform' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute."

Where could I serialize the Transform stuff?

I did some search on Google and found this:

Found the answer, the error actually suggests it too :)

[DataContract]

[KnownType(typeof(System.Windows.Media.MatrixTransform))]

Seemed it can resolve this problem, but where should I put these lines?

This is my code on MainPage to pass the image to SecondPage, img is a WriteableBitmap:

 protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);
        var brush = new ImageBrush();
        brush.ImageSource = img;
        PhoneApplicationService.Current.State["bkg"] = brush;
    }

Thanks again.

Community
  • 1
  • 1
dumbfingers
  • 7,001
  • 5
  • 54
  • 80

2 Answers2

1

Perhaps You should try to use the dispatcher:

activePage.Dispatcher.BeginInvoke(
     () => NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative)));

This works in MetroApps.

Yann Olaf
  • 597
  • 3
  • 12
  • thx for your quick response, the error is, I think, because of the `camTask_Completed` is a method launched from camera, thus the MainPage is not in foreground so cannot do dispatcher stuff. – dumbfingers Oct 17 '12 at 13:39
  • The problem is that only the foreground thread can access the UI. try this: void camTask_Completed(object sender, PhotoResult e) { //throw new NotImplementedException(); img = PictureDecoder.DecodeJpeg(e.ChosenPhoto); mainpage.Dispatcher.BeginInvoke( () => NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative))); } – Yann Olaf Oct 17 '12 at 13:41
  • thx mate, I tried ur method and also I found another error, please refer to my updated question, thanx. :) – dumbfingers Oct 17 '12 at 14:03
0

That's a known issue with the CameraCaptureTask.
Here is the workaround that I use:

    void OnCameraCaptureTaskCompleted(object sender, PhotoResult args)
    {
        //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("/Page1.xaml", UriKind.Relative));
        NavigationService.Navigated -= new NavigatedEventHandler(navigateCompleted);
    }
Olivier Payen
  • 15,198
  • 7
  • 41
  • 70
  • I think it is another problem. It seems like you are trying to serializing an object. Are you saving objects states? `SolidColorBrush` has a property named `Transform` of type `Transform`. If it's that, you should serialize the Color instead. – Olivier Payen Oct 17 '12 at 14:12
  • yeah, i'm trying to pass the camera photo to a canvas on the second page and set it as `canvas.background` – dumbfingers Oct 17 '12 at 14:13
  • 1
    In that case, you should save the resulting image into the IsolatedStorage and read it later on your 2nd page. See here: http://stackoverflow.com/questions/6373702/save-image-into-isolated-storage – Olivier Payen Oct 17 '12 at 14:15