C# 捕获QR码时屏幕挂起

C# 捕获QR码时屏幕挂起,c#,windows-phone-8,windows-phone,qr-code,C#,Windows Phone 8,Windows Phone,Qr Code,我正在开发一个需要Qr码扫描的Windows Phone 8应用程序,我正在使用Zxing库扫描Qr码 在这个应用程序中,扫描完成后,我需要返回到上一页 我正在使用下面的代码 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { _phoneCamera = new PhotoCamera(); _phoneCamera.Initialized += ca

我正在开发一个需要Qr码扫描的Windows Phone 8应用程序,我正在使用Zxing库扫描Qr码

在这个应用程序中,扫描完成后,我需要返回到上一页

我正在使用下面的代码

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{

    _phoneCamera = new PhotoCamera();
    _phoneCamera.Initialized += cam_Initialized;
    _phoneCamera.AutoFocusCompleted += _phoneCamera_AutoFocusCompleted;

    CameraButtons.ShutterKeyHalfPressed += CameraButtons_ShutterKeyHalfPressed;

    viewfinderBrush.SetSource(_phoneCamera);

    _scanTimer = new DispatcherTimer();
    _scanTimer.Interval = TimeSpan.FromMilliseconds(1500);
    _scanTimer.Tick += (o, arg) => ScanForBarcode();

    base.OnNavigatedTo(e);
}

protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
{
    _scanTimer.Stop();
    if (cameraInit)
    {
        Dispatcher.BeginInvoke(() =>
           {                       
               if (_phoneCamera != null)
               {
                   _phoneCamera.CancelFocus();
                   _phoneCamera.Dispose();                        
                   _phoneCamera.Initialized -= cam_Initialized;
                   _phoneCamera = null;
                   cameraInit = false;
               }
           });
    }
}

void cam_Initialized(object sender, Microsoft.Devices.CameraOperationCompletedEventArgs e)
{

    if (e.Succeeded)
    {
        cameraInit = true;
        this.Dispatcher.BeginInvoke(() =>
        {                 
           _phoneCamera.FlashMode = FlashMode.Auto;
            _previewBuffer = new WriteableBitmap((int)_phoneCamera.PreviewResolution.Width, (int)_phoneCamera.PreviewResolution.Height);
            _barcodeReader = new BarcodeReader();

          (int)_phoneCamera.PreviewResolution.Width, (int)_phoneCamera.PreviewResolution.Height, 0, 100);

            var supportedBarcodeFormats = new List<BarcodeFormat>();
            supportedBarcodeFormats.Add(BarcodeFormat.QR_CODE);
            supportedBarcodeFormats.Add(BarcodeFormat.DATA_MATRIX);
            _barcodeReader.Options.PossibleFormats = supportedBarcodeFormats;

            _barcodeReader.Options.TryHarder = true;               
            _barcodeReader.ResultFound += _bcReader_ResultFound;
            _scanTimer.Start();
        });
    }
    else
    {
        Dispatcher.BeginInvoke(() =>
        {
            MessageBox.Show("Unable to initialize the camera");
        });
    }

}


void _bcReader_ResultFound(Result obj)
{
    Dispatcher.BeginInvoke(() =>
    {
        VibrateController.Default.Start(TimeSpan.FromMilliseconds(100));
        a = obj.Text;
        _scanTimer.Stop();
        NavigationService.GoBack(); //here I am going back to previos page
    });
}

private void ScanForBarcode()
{                    
    _phoneCamera.GetPreviewBufferArgb32(_previewBuffer.Pixels);
    _previewBuffer.Invalidate();
    _barcodeReader.Decode(_previewBuffer);         
}
受保护的覆盖无效OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
_phoneCamera=新的光电摄像机();
_phoneCamera.Initialized+=cam_Initialized;
_phoneCamera.AutoFocusCompleted+=\u phoneCamera\u AutoFocusCompleted;
CameraButtons.ShutterKeyHalfPressed+=CameraButtons\u ShutterKeyHalfPressed;
viewfinderBrush.SetSource(_phoneCamera);
_scanTimer=新的调度程序();
_scanTimer.Interval=TimeSpan.From毫秒(1500);
_scanTimer.Tick+=(o,arg)=>ScanForBarcode();
基地。导航到(e);
}
受保护的覆盖无效OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
{
_scanTimer.Stop();
如果(cameraInit)
{
Dispatcher.BeginInvoke(()=>
{                       
如果(_phoneCamera!=null)
{
_取消焦点();
_phoneCamera.Dispose();
_phoneCamera.Initialized-=cam_Initialized;
_phoneCamera=null;
cameraInit=false;
}
});
}
}
已初始化无效cam_(对象发送器,Microsoft.Devices.CameraOperationCompletedEventArgs e)
{
如果(如成功)
{
cameraInit=true;
this.Dispatcher.BeginInvoke(()=>
{                 
_phoneCamera.FlashMode=FlashMode.Auto;
_previewBuffer=新的WriteableBitmap((int)\u phoneCamera.PreviewResolution.Width,(int)\u phoneCamera.PreviewResolution.Height);
_条形码阅读器=新条形码阅读器();
(int)_phoneCamera.PreviewResolution.Width,(int)_phoneCamera.PreviewResolution.Height,0,100);
var supportedBarcodeFormats=新列表();
supportedBarcodeFormats.Add(BarcodeFormat.QR_代码);
supportedBarcodeFormats.Add(BarcodeFormat.DATA_矩阵);
_barcodeReader.Options.PossibleFormats=支持的条形码格式;
_barcodeReader.Options.TryHarder=true;
_条形码阅读器.ResultFound+=\u bcReader\u ResultFound;
_scanTimer.Start();
});
}
其他的
{
Dispatcher.BeginInvoke(()=>
{
MessageBox.Show(“无法初始化摄像头”);
});
}
}
无效_bcReader_ResultFound(结果对象)
{
Dispatcher.BeginInvoke(()=>
{
VibrationController.Default.Start(TimeSpan.From毫秒(100));
a=对象文本;
_scanTimer.Stop();
NavigationService.GoBack();//这里我将返回到previos页面
});
}
专用void ScanForBarcode()
{                    
_GetPreviewBufferArgb32(_previewBuffer.Pixels);
_previewBuffer.Invalidate();
_条形码阅读器.解码(_previewBuffer);
}
此代码工作正常,但有时在捕获QR代码时会挂起应用程序

编辑

在没有调试模式的情况下运行应用程序时会出现此问题。 当应用程序没有响应时,经过一段时间后,它会崩溃,但不会给出任何错误消息


所以请帮我解决这个问题。提前感谢。

我不确定,但是否可能是您试图扫描UI线程上的二维码,从而导致挂起。尝试在不同的线程上执行


Windows Phone操作系统不喜欢无响应的UI线程,可能会导致应用程序意外关闭。

定义“挂起”。应用程序是否在一段时间内没有响应,或者是否崩溃?是否有相关的错误消息?它将变得无响应,并在一段时间后崩溃,但不会显示任何错误消息。我现在编辑了我的问题…@user2428823你解决了吗?