2014-01-08 47 views
0

當我呼叫等待Windows Phone 8相機應用程序中的PhotoCaptureDevice.OpenAsync(..)時,會引發InvalidOperationException異常。WP6中的PhotoCaptureDevice中的InvalidOperationException

System.InvalidOperationException:由於對象當前狀態爲 ,操作無效。在在 JPMC System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務 任務)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務 任務)在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()。 Controls.CaptureTaskLauncher.d_6.MoveNext()---棧底從以前的位置在那裏引發異常--- 跟蹤在 System.Runtime.CompilerServices.AsyncMethodBuilderCore.b_0(對象 狀態)}

但還有一件事是我第一次運行相機測試時它會完美工作,即PhotoCaptureDevice初始化但是當我從另一個頁面返回到相機測試時,它會給出上述錯誤。 那麼,任何人都可以幫我弄清楚這個問題?

感謝

回答

0

您是否嘗試過離開頁面前釋放資源,使用PhotoCaptureDevice.Close()和Dispose()方法?

+0

是的,我試過了,但沒有use.after,也引發異常。 – user3146084

+0

然後你將需要顯示更多的代碼(或者我應該說 - 至少是一些代碼),以顯示如何初始化PhotoCaptureDevice等。 –

+0

我已經添加了上面的代碼。 – user3146084

0
private static PhotoCaptureDevice d = null; 
     public static async Task<bool> InitializeCamera(CameraSensorLocation sensorLocation) 
     { 
      bool catchValue = true; 
      try 
      { 
       Windows.Foundation.Size initialResolution = new Windows.Foundation.Size(640, 480); 
       Windows.Foundation.Size previewResolution = new Windows.Foundation.Size(640, 480); 
       Windows.Foundation.Size captureResolution = new Windows.Foundation.Size(640, 480); 
       d = await PhotoCaptureDevice.OpenAsync(sensorLocation, initialResolution); 
       await d.SetPreviewResolutionAsync(previewResolution); 
       await d.SetCaptureResolutionAsync(captureResolution);    
       d.SetProperty(KnownCameraGeneralProperties.PlayShutterSoundOnCapture, true); 
       MemoryStream stream = new MemoryStream();     
       CameraCaptureSequence sequence = d.CreateCaptureSequence(1); 
       sequence.Frames[0].CaptureStream = stream.AsOutputStream(); 
       await d.PrepareCaptureSequenceAsync(sequence);     
       await sequence.StartCaptureAsync();        
       if (stream.Length > 0) 
       { 
        d.Dispose(); 
        d = null; 
        stream.Dispose(); 
        stream = null; 
        return true; 
       } 
       else 
       { 
        d.Dispose(); 
        d = null; 
        stream.Dispose(); 
        stream = null; 
       } 
       return false; 
      } 
      catch (Exception ex) 
      { 
       catchValue = false; 
      } 
      if (!catchValue) 
      { 
       Windows.Foundation.Size initialResolution = new Windows.Foundation.Size(640, 480); 
       Windows.Foundation.Size previewResolution = new Windows.Foundation.Size(640, 480); 
       Windows.Foundation.Size captureResolution = new Windows.Foundation.Size(640, 480); 

       d = await PhotoCaptureDevice.OpenAsync(sensorLocation,initialResolution); 

       await d.SetPreviewResolutionAsync(previewResolution); 
       await d.SetCaptureResolutionAsync(captureResolution); 

       d.SetProperty(KnownCameraGeneralProperties.PlayShutterSoundOnCapture, true); 
       MemoryStream stream = new MemoryStream(); 

       CameraCaptureSequence sequence = d.CreateCaptureSequence(1); 
       sequence.Frames[0].CaptureStream = stream.AsOutputStream(); 
       await d.PrepareCaptureSequenceAsync(sequence); 

       await sequence.StartCaptureAsync(); 

       if (stream.Length > 0) 
       { 
        d.Dispose(); 
        d = null; 
        stream.Dispose(); 
        stream = null; 
        return true; 
       } 
       else 
       { 
        d.Dispose(); 
        d = null; 
        stream.Dispose(); 
        stream = null; 
       } 
       return false; 
      } 
      return false; 

     } 
相關問題