所以我想在WP7中做一个非常简单的事情,例如:
主页中的一个按钮将启动相机,当摄像头成功拍照时,我想将图片传递给第二页并启动。
这是我的代码:
在主页构造函数中,我初始化相机任务并设置委托:
camTask.Completed += new EventHandler<PhotoResult>(camTask_Completed);
然后我实现了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));
}
直到我按"接受"之后,该应用程序将不会出错。拍照后。
例外说:
Exception {"Navigation is not allowed when the task is not in the foreground."} System.Exception {System.InvalidOperationException}
我知道我不应该在camTask_Completed
方法中启动第二页。
然后我的问题是:如何在EventHandler的结果上启动另一个页面?
谢谢
更新:(要回答此子问题,请参阅此页面中的此评论)
单击按钮(启动摄像机)后立即发现了另一个错误:
抛出一个例外说:
"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."
我可以在哪里序列化 Transform
东西?
我在Google上进行了一些搜索,并找到了以下内容:
找到答案,错误实际上也暗示了:)
[datacontract]
[newtype(typeof(system.windows.media.matrixtransform))]
似乎可以解决这个问题,但是我应该在哪里放这些行?
这是我在主页上的代码,将图像传递到第二页, img
是 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;
}
再次感谢。
也许您应该尝试使用调度程序:
activePage.Dispatcher.BeginInvoke(
() => NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative)));
这在Metroapps中起作用。
这是摄影库的已知问题。
这是我使用的解决方法:
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);
}