2015-03-03 70 views
1

我試圖在Windows 8.1通用應用程序中啓動和停止相機的MediaCapture預覽。在的OnNavigatedTo方法中,我有以下代碼:調用MediaCapture.StartPreviewAsync時發生「InvalidOperationException:方法在意外的時間被調用」

protected override async void OnNavigatedTo(NavigationEventArgs e) 
{ 
    var scanItemsParameter = e.Parameter as ScanItemParameter; 

    if (scanItemsParameter != null) 
    { 
     ((ScanItemViewModel)DataContext).ScanCallback = scanItemsParameter.ScanCallback; 
    } 

    try 
    { 
     HardwareButtons.CameraPressed += HardwareButtonsOnCameraPressed; 
     HardwareButtons.CameraHalfPressed += HardwareButtonsOnCameraHalfPressed; 
     DisplayInformation.GetForCurrentView().OrientationChanged += OnOrientationChanged; 

     if (!_mediaInitialized) 
     { 
      var cameras = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); 
      if (cameras.Count < 1) 
      { 
       return; 
      } 
      MediaCaptureInitializationSettings settings; 
      settings = new MediaCaptureInitializationSettings 
      { 
       VideoDeviceId = cameras[0].Id, 
       PhotoCaptureSource = PhotoCaptureSource.Photo 
      }; 

      await _mediaCapture.InitializeAsync(settings); 

      VideoCapture.Source = _mediaCapture; 

      _mediaInitialized = true; 
     } 

     SetOrientation(DisplayInformation.GetForCurrentView().CurrentOrientation); 

     await _mediaCapture.StartPreviewAsync(); 

     await _mediaCapture.VideoDeviceController.ExposureControl.SetAutoAsync(true); 

     if (_mediaCapture.VideoDeviceController.FocusControl.FocusChangedSupported) 
     { 
      var focusSettings = new FocusSettings(); 
      focusSettings.AutoFocusRange = AutoFocusRange.Normal; 
      focusSettings.Mode = FocusMode.Auto; 
      focusSettings.WaitForFocus = true; 
      focusSettings.DisableDriverFallback = false; 
      _mediaCapture.VideoDeviceController.FocusControl.Configure(focusSettings); 
     } 

     _mediaCapture.VideoDeviceController.FlashControl.Auto = true; 
    } 
    catch (Exception ex) 
    { 
     var ex2 = ex; 
     throw; 
    } 
} 

在OnNavigatingFrom方法,我清理了一些事件處理程序,並調用MediaCapture.StopPreviewAsync()

protected override async void OnNavigatingFrom(NavigatingCancelEventArgs e) 
{ 
    await _mediaCapture.StopPreviewAsync(); 
    HardwareButtons.CameraPressed -= HardwareButtonsOnCameraPressed; 
    HardwareButtons.CameraHalfPressed -= HardwareButtonsOnCameraHalfPressed; 
    DisplayInformation.GetForCurrentView().OrientationChanged -= OnOrientationChanged; 
} 

呼叫正常工作,我第一次開該頁面,但如果我離開頁面並返回,我會得到一個InvalidOperationException。我錯過了什麼?

作爲一個說明,我使用的MVVM光,如果說有什麼區別...

在此先感謝您的幫助......

+0

你怎麼離開?我懷疑的問題是,這些事件並不是每次你離開應用程序時都被調用。例如,如果您使用的是暫停管理器,則當您暫停應用時(命中開始按鈕,更改應用等),將調用NavigateFrom事件(這在調試時也不會發生),但是當您返回時不會調用NavigatedTo到應用程序。 – Romasz 2015-03-03 05:58:35

+0

@Romasz我使用'NavigationService.NavigateTo()'和'NavigationService.GoBack()'方法來改變頁面。當我使用後者時,'OnNavigatingFrom'方法中的代碼被成功調用。至少在我第二次訪問該頁面時,「OnNavigatedTo」代碼肯定會被調用。它一直在'StartPreviewAsync'方法中斷。我認爲它在應用程序暫停或主頁按鈕被按下時執行相同的操作。我還沒有開始研究這些問題。 – 2015-03-03 13:18:33

+0

您是否設置了您的頁面的「NavigationCacheMode = NavigationCacheMode.Required;」 - 如果您的MediaCapture不是導航可能返回* null *,那麼您是否有可能這樣做。我已經測試過樣本,並應該工作,如果你有進一步的問題給我一個跡象。還使用OnNavigatedTo和From(僅)不是一個好主意,你應該慢慢開始研究這些問題;) – Romasz 2015-03-03 17:00:35

回答

0

我能夠通過的配置來解決這個MediaCapture元素在導航離開頁面之前,並在返回頁面時重新實例化它。

OnNavigatingFrom方法,我添加_mediaCapture.Dispose();_mediaCapture = null;

protected override async void OnNavigatingFrom(NavigatingCancelEventArgs e) 
{ 
    if (_mediaCapture != null) 
    { 
     await _mediaCapture.StopPreviewAsync(); 
     _mediaCapture.Dispose(); 
     _mediaCapture = null; 
    } 
    HardwareButtons.CameraPressed -= HardwareButtonsOnCameraPressed; 
    HardwareButtons.CameraHalfPressed -= HardwareButtonsOnCameraHalfPressed; 
    DisplayInformation.GetForCurrentView().OrientationChanged -= OnOrientationChanged; 
} 

然後,只需在OnNavigatedTo_mediaCapture.InitializeAsync()電話之前,我實例化一個新問題:

//... 
_mediaCapture = new MediaCapture(); 
//... 
相關問題