I am trying to use the new Windows 8.1 Point of Service API for Barcode Scanners, and if I call GetDefaultAsync()
from any of the following locations, it returns null
.
App.OnLaunched
- The first page
.Loaded
- The first page
OnNavigatedTo
- The first page constructor
This doesn't seem to be an issue with DeviceCapabilities
or drivers, as it will work perfectly if I call it from:
- A button
Click
event handler - The first page's
OnGotFocus
- The first page's constructor if wrapped in:
this.Dispatcher.RunIdleAsync(e => { var res = await BarcodeScanner.GetDefaultAsync(); Assert(res != null); });
- Subsequent page's constructors
Which makes me suspect that you must have focus to access the POS devices, and the constructor et al. are being called prior to focus being received.
Question: Is there published guidance as to when you can call GetDefaultAsync()
?
Loosely following Microsoft Sample BarcodeScanner I had no difficulty connecting a barcode scanner in
OnNavigatedTo
though theBarcodeScanner.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.
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.