1

写真をキャプチャできるアプリを作成し、写真を保存すると別のページに移動しますpage1.xamlが、エラーが発生します:|

エラーが発生An unhandled exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dllし、メッセージに「それはInvalid cross-thread access.何ですか?」と表示されます。私はWPアプリの開発に少し慣れていません。

これが私のコードです

void cam_CaptureImageAvailable(object sender, Microsoft.Devices.ContentReadyEventArgs e)
    {
        string fileName = savedCounter + ".jpg";

        try
        {   // Write message to the UI thread.
            Deployment.Current.Dispatcher.BeginInvoke(delegate()
            {
                txtDebug.Text = "Captured image available, saving picture.";
            });

            // Save picture to the library camera roll.
            library.SavePictureToCameraRoll(fileName, e.ImageStream);

            // Write message to the UI thread.
            Deployment.Current.Dispatcher.BeginInvoke(delegate()
            {
                txtDebug.Text = "Picture has been saved to camera roll.";

            });

            // Set the position of the stream back to start
            e.ImageStream.Seek(0, SeekOrigin.Begin);

            // Save picture as JPEG to isolated storage.
            using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
                {
                    // Initialize the buffer for 4KB disk pages.
                    byte[] readBuffer = new byte[4096];
                    int bytesRead = -1;

                    // Copy the image to isolated storage. 
                    while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
                    {
                        targetStream.Write(readBuffer, 0, bytesRead);
                    }
                }
            }

            // Write message to the UI thread.
            Deployment.Current.Dispatcher.BeginInvoke(delegate()
            {
                txtDebug.Text = "Picture has been saved to isolated storage.";

            });
        }
        finally
        {
            // Close image stream
            e.ImageStream.Close();

            GoTo();
        }
    }


private void GoTo()
{
     this.NavigationService.Navigate(new Uri("/page1.xaml", Urikind.Relative));
}
4

1 に答える 1

4

GoToバックグラウンドスレッドからメソッドを呼び出しています。ナビゲートするときは、フォアグラウンドスレッドで実行する必要があります。

private void GoTo()
{
    Dispatcher.BeginInvoke(() =>
    {
        this.NavigationService.Navigate(new Uri("/page1.xaml", Urikind.Relative));
    });
}
于 2013-03-10T17:30:04.047 に答える