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.
Perhaps You should try to use the dispatcher:
This works in MetroApps.