0

我正在开发带有条码扫描的 WP7/8 应用程序。并且在处理相机方面有问题。相机初始化时间过长,当相机仍在初始化并且我按下返回按钮时,我遇到了一个致命错误:

Microsoft.Devices.Camera.ni.dll WinRT 信息中出现“System.ObjectDisposedException”类型的第一次机会异常:致命错误。处置捕获设备。

有人可以帮助我如何避免这个错误吗?

我的代码:

 protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        InitializeAndGo();

        base.OnNavigatedTo(e);
    }

    protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
    {
        disposeCamera();
    }

    private void PhotoCameraOnInitialized(object sender, CameraOperationCompletedEventArgs cameraOperationCompletedEventArgs)
    {
        _width = Convert.ToInt32(_photoCamera.PreviewResolution.Width);
        _height = Convert.ToInt32(_photoCamera.PreviewResolution.Height);

        _luminance = new PhotoCameraLuminanceSource(_width, _height);

        if (_photoCamera.IsFlashModeSupported(FlashMode.Auto))
        {
            _photoCamera.FlashMode = FlashMode.Off;
        }

        cameraInitialized = true;
        Dispatcher.BeginInvoke(() =>
        {
            FlashCheckbox.IsEnabled = true;
            if (_photoCamera.IsFlashModeSupported(FlashMode.Auto))
            {
                _photoCamera.FlashMode = FlashMode.Off;
            }
        });


        _photoCamera.Focus();
    }

     private void InitializeAndGo()
        {
             stopScan = false;
            _photoCamera = new PhotoCamera();
            _photoCamera.Initialized += PhotoCameraOnInitialized;
            _photoCamera.AutoFocusCompleted += PhotoCameraOnAutoFocusCompleted;
            viewfinderBrush.SetSource(_photoCamera);
            _previewTransform.Rotation = _photoCamera.Orientation;
            _results = new ObservableCollection<Result>();

            _barcodeReader = new BarcodeReader();
            _barcodeReader.TryHarder = true;
            _barcodeReader.AutoRotate = true;              
            _service = new MyMoviesDataService(ErrorDataService);

        }

    private void disposeCamera()
    {
        try
        {
            cameraInitialized = false;
            StopScan();
            _photoCamera.Initialized -= PhotoCameraOnInitialized;
            _photoCamera.AutoFocusCompleted -= PhotoCameraOnAutoFocusCompleted;
            _photoCamera.Dispose();
            _photoCamera = null;
        }
        catch (Exception ex)
        {
            App.ShowErrorToast(ex.Message);
        }
    }
4

2 回答 2

1

在成功初始化之前不要使用相机(您可以在相机的 Initialized 事件中检查这一点)。

此外,将相机的任何用法都包含在一个

try
{
    // camera code here
}
catch (ObjectDisposedException)
{
    // re-initialize the camera?
}

处理暂停等情况,这将自动处理相机。

至于

Microsoft.Devices.Camera.ni.dll 中出现“System.ObjectDisposedException”类型的异常,并且在托管/本机边界 WinRT 信息之前未处理:致命错误。处置捕获设备。

这是微软需要解决的问题;我的意思是,如果不允许传播到托管代码,您应该如何处理本机代码异常?

于 2014-03-07T09:23:47.390 回答
0

异常来自哪里(哪个代码行/块)?

首先,我会在 OnNavigatedTo 事件处理程序中尝试使用 InitializeAndGo()。并且在整个 PhotoCameraOnInitialized 事件处理程序上也是如此。

干杯,

于 2013-08-22T08:55:45.470 回答