C# &引用;InvalidOperationException:在意外时间调用了方法;调用MediaCapture.StartPreviewSync时
我正在尝试在Windows 8.1通用应用程序中启动和停止摄像头的MediaCapture预览。在OnNavigatedTo方法中,我有以下代码:C# &引用;InvalidOperationException:在意外时间调用了方法;调用MediaCapture.StartPreviewSync时,c#,wpf,windows-phone-8.1,win-universal-app,C#,Wpf,Windows Phone 8.1,Win Universal App,我正在尝试在Windows 8.1通用应用程序中启动和停止摄像头的MediaCapture预览。在OnNavigatedTo方法中,我有以下代码: protected override async void OnNavigatedTo(NavigationEventArgs e) { var scanItemsParameter = e.Parameter as ScanItemParameter; if (scanItemsParameter != null) {
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;
}
}
在我第一次打开页面时,调用正常工作,但是如果我离开页面并返回,我会得到一个InvalidOperationException。我错过了什么
请注意,我使用的是MVVM light,如果这有什么不同的话
提前感谢您的帮助…我可以通过在离开页面之前处理
MediaCapture
元素,并在返回页面时重新实例化它来解决此问题
在OnNavigatingFrom
方法中,我添加了\u mediaCapture.Dispose()
和\u 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;
}
然后,就在\u mediaCapture.InitializeAsync()
调用导航到之前,我实例化了一个新的:
//...
_mediaCapture = new MediaCapture();
//...
你是怎么离开的?我怀疑的问题是,这些事件不会在每次你“离开应用程序”时调用。例如,如果您使用的是suspension manager,则在挂起应用程序(点击开始按钮、更改应用程序等)时将调用NavigateFrom事件(调试时也不会发生这种情况),但在返回应用程序时将不会调用NavigateTo。@Romasz我使用的是NavigationService.NavigateTo()
,和NavigationService.GoBack()
方法来更改页面。当我使用后者时,OnNavigatingFrom
方法中的代码被成功调用。至少在我第二次访问页面时,OnNavigatedTo
代码肯定会被调用。它始终在startPreviewSync
方法上中断。我认为当应用程序被暂停或按下home按钮时,它也会做同样的事情。我还没有开始研究这些问题。您是否设置了NavigationCacheMode=NavigationCacheMode.Required您的页面的代码>如果没有,那么一旦您从导航返回,您的MediaCapture可能为空。我已经测试了样品,如果您还有其他问题,请给我一个提示。另外,使用OnNavigatedTo和From(仅限)也不是一个好主意,您应该慢慢开始研究这些问题;)@谢谢你的帮助。我将尝试使用NavigationCacheMode。今晚需要。在我脑子里,我想我没有用这个。“使用OnNavigatedTo和From(仅)不是一个好主意”是什么意思?这是我用这些技术做的第一个项目,所以我当然可以使用你能提供的所有建议。我还发现,在开发Windows Phone 8.1通用应用程序时,很难获得好的文档。至于一个坏主意——看一看(你也会发现一些关于这个问题的链接)。你也可能是窗口的东西。在某些情况下可能更好的时事。
//...
_mediaCapture = new MediaCapture();
//...