相机初始化并按下后退按钮或页面导航时出现致命错误

时间:2013-08-22 08:41:45

标签: windows-phone-7 windows-phone-8 windows-phone

我正在使用带条形码扫描的WP7 / 8应用程序。处理相机时遇到问题。相机初始化时间太长,当相机仍在初始化时我按下后退按钮,我有一个致命的错误:

  

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

有人可以帮我解决这个错误吗?

我的代码:

 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);
        }
    }

2 个答案:

答案 0 :(得分:1)

在成功初始化之前请勿使用相机(您可以在相机的初始化事件中进行检查)。

此外,请将相机的任何用法包装在

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

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

至于

  

发生了'System.ObjectDisposedException'类型的异常   Microsoft.Devices.Camera.ni.dll并没有在a之前处理过   托管/本地边界WinRT信息:致命错误。处置   捕获设备。

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

答案 1 :(得分:0)

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

我想首发尝试...在OnNavigatedTo事件处理程序中捕获InitializeAndGo()。而且整个PhotoCameraOnInitialized事件处理程序也是。

干杯,