"InvalidOperationException: A method was called at an unexpected time" 调用时 MediaCapture.StartPreviewAsync

"InvalidOperationException: A method was called at an unexpected time" when calling MediaCapture.StartPreviewAsync

我正在尝试在 Windows 8.1 通用应用程序中启动和停止相机的 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 light,如果这有什么不同...

在此先感谢您的帮助...

我能够通过在离开页面之前处理 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();
//...