调用MediaCapture.StartPreviewAsync时出现“InvalidOperationException:在意外时间调用方法”

时间:2015-03-03 04:44:07

标签: c# wpf windows-phone-8.1 win-universal-app

我正在尝试在Windows 8.1 Universal应用中启动和停止相机的MediaCapture预览。在OnNavigatedTo方法中,我有以下代码:

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灯,如果这有任何区别......

提前感谢您的帮助......

1 个答案:

答案 0 :(得分: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;
}

然后,就在_mediaCapture.InitializeAsync()中的OnNavigatedTo调用之前,我实例化一个新的:

//...
_mediaCapture = new MediaCapture();
//...