DeviceInformation.FindAllAsync() doing nothing

混江龙づ霸主 提交于 2019-12-24 06:36:02

问题


I've been using a camera BarcodeScanner (Microsoft's BarcodeScanner API) for 3 weeks now in my UWP application. It's working well but sometimes, the preview window of the barcode is not opening and the BarcodeScanner is not turning on.

When I figured this out, I took my app in debug mode to see where was the problem and if they were some exceptions thrown.

Here's how I'm initializing the 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");
        }
    }
}

... and the problem seems to come from the line :

DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(selector);

Called in :

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

}

OnBarcodeReceived in my 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;
        }
    });

}

where the code is just stopping and waiting without throwing an error.

So if someone could explain to me, why it's doing that and why the code doesn't throw any error after a certain amount of time ?

来源:https://stackoverflow.com/questions/56649293/deviceinformation-findallasync-doing-nothing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!