When is it allowable to call `BarcodeScanner.GetDefaultAsync()`?

这一生的挚爱 提交于 2019-12-02 07:00:52

Loosely following Microsoft Sample BarcodeScanner I had no difficulty connecting a barcode scanner in OnNavigatedTo though the BarcodeScanner.GetDefaultAsync() call is a bit nested.

I claimed the scanner in the OnNavigatedTo because the point of this particular page is to scan barcodes if the scanner is not found/claimed for some reason I want an error upfront I do not want the page to look and feel functional if it is not and I do not want to force the user to attempt a scan before they find out that the barcodescanner is not working.

I cannot tell you why calling in different locations was not working in your particular case without seeing more of your code, but i suggest trying the following.

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        EnableScanner();
    }

private async void EnableScanner()
    {
        if (await CreateDefaultScannerObject())
        {
            // after successful creation, claim the scanner for exclusive use and enable it so that data reveived events are received.
            if (await ClaimScanner())
            {
                Task<bool> AsyncSuccess = EnableClaimedScanner();
                bool x = await AsyncSuccess;
                if (x)
                {
                    HookUpEventsClaimedScanner();
                }
            }
        }
    }
        private async Task<bool> CreateDefaultScannerObject()
    {
        if (scanner == null)
        {
            UpdateOutput("Creating Barcode Scanner object.");
            scanner = await BarcodeScanner.GetDefaultAsync();

            if (scanner != null)
            {
                UpdateOutput("Default Barcode Scanner created.");
                UpdateOutput("Device Id is:" + scanner.DeviceId);
            }
            else
            {
                UpdateOutput("Barcode Scanner not found. Please connect a Barcode Scanner.");
                return false;
            }
        }
        return true;
    }

    private async Task<bool> EnableClaimedScanner()
    {
        bool result = false;
        try
        {
            await claimedScanner.EnableAsync();
            if (claimedScanner.IsEnabled)
            {
                claimedScanner.IsDecodeDataEnabled = true;
                UpdateOutput("ClaimedScanner is now Enabled.");
                result = true;
            }
            else
                UpdateOutput("ClaimedScanner wasn't Enabled.");
        }
        catch (Exception ex)
        {
            UpdateOutput( ex.Message);
        }
        return result;
    }

    public void HookUpEventsClaimedScanner()
    {
        claimedScanner.DataReceived += ScannerDataReceived;
        claimedScanner.ReleaseDeviceRequested += ScannerReleaseRequest;
    }

EDIT: I realize this question was over a year old but i found it in research for my own windows 8.1 embedded barcode scanner so I wanted to ensure it was not leading anyone else down the wrong path thinking GetDefaultAsync would not work in certain call situations.

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