1

だから私はWP7で次のような非常に単純なことをしようとしています:

MainPageのボタンでカメラが起動します。カメラが正常に写真を撮ったら、その写真をSecondPageに渡して起動します。

これが私のコードです:

MainPageコンストラクター内で、カメラタスクを初期化し、デリゲートを設定します。

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}

私が理解しているのは、メソッド内でSecondPageを起動するべきではないということ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ますか?

私はグーグルでいくつかの検索をして、これを見つけまし

答えが見つかりました、エラーは実際にもそれを示唆しています:)

[DataContract]

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

この問題は解決できるようですが、これらの行をどこに配置すればよいですか?

これは、画像をSecondPageに渡すためのMainPageのコード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;
    }

再度、感謝します。

4

2 に答える 2

1

おそらく、ディスパッチャを使用してみてください。

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

これはMetroAppsで機能します。

于 2012-10-17T13:36:38.547 に答える
0

これは 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);
    }
于 2012-10-17T13:50:55.603 に答える