繁体   English   中英

进入EventHandler竞争时的页面?

[英]Navigate to a page when EventHandler compelete?

所以我正在尝试在WP7中做一个非常简单的事情,例如:

MainPage中的一个按钮将启动照相机,当照相机成功拍摄照片时,我要将照片传递给SecondPage并启动它。

这是我的代码:

在MainPage构造函数中,我初始化了camera任务并设置了委托:

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方法中启动SecondPage。

然后我的问题是:如何在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]

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

看来它可以解决此问题,但是我应该将这些行放在哪里?

这是我在MainPage上将图像传递给SecondPage的代码, imgWriteableBitmap

 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中有效。

这是CameraCaptureTask的已知问题。
这是我使用的解决方法:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM