DeviceInformation.FindAllAsync()什么都不做

时间:2019-06-18 12:34:00

标签: c# uwp barcode-scanner

我在UWP应用程序中已经使用相机BarcodeScanner(Microsoft的BarcodeScanner API)已有3周了。 它工作正常,但是有时条形码的预览窗口没有打开,并且BarcodeScanner没有打开。

当我弄清楚这一点时,我将我的应用程序置于调试模式,以查看问题出在哪里以及是否引发了某些异常。

这是我初始化BarcodeScanner的方式:

public async Task ClaimScannerAsync()
{
    string selector = Windows.Devices.PointOfService.BarcodeScanner.GetDeviceSelector();
    DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(selector);

    if (_scanner == null)
        _scanner = await Windows.Devices.PointOfService.BarcodeScanner.FromIdAsync(deviceCollection[0].Id);

    if (_scanner != null)
    {
        if (_claimedBarcodeScanner == null)
            _claimedBarcodeScanner = await _scanner.ClaimScannerAsync();

        if (_claimedBarcodeScanner != null)
        {
            _claimedBarcodeScanner.DataReceived += ClaimedBarcodeScanner_DataReceivedAsync;
            _claimedBarcodeScanner.ReleaseDeviceRequested += ClaimedBarcodeScanner_ReleaseDeviceRequested;
            _claimedBarcodeScanner.IsDecodeDataEnabled = true;
            _claimedBarcodeScanner.IsDisabledOnDataReceived = true;
            await _claimedBarcodeScanner.EnableAsync();
            //await _claimedBarcodeScanner.ShowVideoPreviewAsync();
            await _claimedBarcodeScanner.StartSoftwareTriggerAsync();
            await StartPreviewAsync();

            Debug.WriteLine("Barcode Scanner claimed");
        }
    }
}

...,问题似乎出在线路上:

DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(selector);

已致电:

internal async Task OnBarcodeExecuted(object dataContext)
{
    try
    {
        CurrentDataContext = dataContext;
        _scanner.Subscribe(OnBarcodeReceived);
        await _scanner.ClaimScannerAsync();
    }
    catch(Exception e)
    {
        Debug.WriteLine(e.Message);
    }

}

OnBarcodeReceived在我的ViewModel中:

private void OnBarcodeReceived(string barcode)
{
    var ignored = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        if(CurrentDataContext is IScannable)
        {
            IScannable item = (IScannable)CurrentDataContext;
            item.NumSerie = barcode;
        }
    });

}

代码只是停止并等待而不会引发错误。

所以如果有人可以向我解释,为什么这样做,为什么代码在一定时间后没有引发任何错误?

0 个答案:

没有答案