2013-03-10 97 views
2

我製作的應用程序可以捕捉照片,保存照片後會轉到另一個頁面,比如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)); 
} 

回答

3

你調用從後臺線程的GoTo方法。在導航時,您需要在前衛線上進行操作。

private void GoTo() 
{ 
    Dispatcher.BeginInvoke(() => 
    { 
     this.NavigationService.Navigate(new Uri("/page1.xaml", Urikind.Relative)); 
    }); 
} 
+0

是的,它的工作原理!感謝:D – wahyudierwin 2013-03-12 00:28:17

+1

幹得好,工作出色。 – 2013-10-10 12:09:26

相關問題