什么时候可以调用`BarcodeScanner.GetDefaultAsync()`? [英] When is it allowable to call `BarcodeScanner.GetDefaultAsync()`?
问题描述
我正在尝试使用新的 Windows 8.1条形码扫描仪的服务点API ,如果我致电
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
- 第一页
.Loaded
- 第一页
OnNavigatedTo
- 首页构造器
App.OnLaunched
- The first page
.Loaded
- The first page
OnNavigatedTo
- The first page constructor
这似乎不是DeviceCapabilities
或驱动程序的问题,因为如果我从以下位置调用它,它将完全正常工作:
This doesn't seem to be an issue with DeviceCapabilities
or drivers, as it will work perfectly if I call it from:
- 按钮
Click
事件处理程序 - 第一页的
OnGotFocus
- 第一页的构造函数(如果包含在其中):
this.Dispatcher.RunIdleAsync(e => { var res = await BarcodeScanner.GetDefaultAsync(); Assert(res != null); });
- 后续页面的构造函数
- 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
这让我怀疑您必须集中精力访问POS设备,而构造函数等.在获得关注之前被调用.
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.
问题:是否存在有关何时您可以致电GetDefaultAsync()
的指南?
Question: Is there published guidance as to when you can call GetDefaultAsync()
?
推荐答案
严格遵循 Microsoft Sample BarcodeScanner 尽管BarcodeScanner.GetDefaultAsync()
调用有点嵌套,但在OnNavigatedTo
中连接条形码扫描器没有困难.
Loosely following Microsoft Sample BarcodeScanner I had no difficulty connecting a barcode scanner in OnNavigatedTo
though the BarcodeScanner.GetDefaultAsync()
call is a bit nested.
我在OnNavigatedTo
中声明了扫描仪,因为如果由于某种原因未找到/声明扫描仪,则此特定页面的目的是扫描条形码.如果不是,并且我不想强迫用户在发现条形码扫描仪不起作用之前尝试进行扫描.
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;
}
我意识到这个问题已经存在了一年多,但是我在我自己的Windows 8.1嵌入式条形码扫描仪的研究中发现了这个问题,因此我想确保它不会导致其他人误以为GetDefaultAsync
无法正常工作在某些通话情况下.
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.
这篇关于什么时候可以调用`BarcodeScanner.GetDefaultAsync()`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!