带有 WPF 应用程序的条码扫描器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38193313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:51:57  来源:igfitidea点击:

Barcode scanner with a WPF application

c#wpfbarcode-scanner

提问by Fivestar

I have a barcode scanner (bluetooth) connected to my computer to use for scanning some barcodes. The scanner acts exactly like a keyboard does and returns whatever it scans. In my WPF application I have some textboxes for the user to manually enter in a product number, revision number, bin number, and lot number.

我有一个条码扫描器(蓝牙)连接到我的电脑,用于扫描一些条码。扫描仪的作用与键盘完全一样,并返回它扫描的任何内容。在我的 WPF 应用程序中,我有一些文本框供用户手动输入产品编号、修订号、箱号和批号。

I would like the user to be able to instead scan the QR/Bar? code which has all that information in it at any time. So I have a few issues:

我希望用户能够改为扫描 QR/Bar?随时包含所有信息的代码。所以我有几个问题:

  1. Is it possible to scan a barcode with your focus on anything in the app and NOT write it out like a keyboard? Example, I have a random textbox highlighted right now but I go and scan a code - I dont want the code to fill in this random textbox - I would want something like all the scanned text goes into a variable.

  2. If step 1 is not possible. The way I currently have it set up is you need to click into a specific textbox. Which on TextChanged will parse it and try to figure out where the pieces need to go. But it will trigger when each character is added to the box. I have about ~30 characters per code so this will slow it down tremendously. I tried to see what other event might work but I don't see any other solution to that issue? Is there some other event that I'm just missing?

  1. 是否可以将注意力集中在应用程序中的任何内容上扫描条形码而不是像键盘一样写出来?例如,我现在有一个随机文本框突出显示,但我去扫描一个代码 - 我不希望代码填充这个随机文本框 - 我希望所有扫描的文本都进入一个变量。

  2. 如果第 1 步不可能。我目前设置它的方式是您需要单击特定的文本框。TextChanged 上的哪个将解析它并尝试找出需要去哪里。但是当每个字符添加到框中时它会触发。每个代码我有大约 30 个字符,所以这会大大减慢它的速度。我试图查看哪些其他事件可能起作用,但我没有看到该问题的任何其他解决方案?还有其他一些我错过的活动吗?

Thank you

谢谢

回答by Jeremy Thompson

I dont want the barcode to fill random textboxes - I want all the scanned QR/barcode into a variable.

我不希望条形码填充随机文本框 - 我希望所有扫描的 QR/条形码都变成一个变量。

private string barCode = string.Empty; //TO DO use a StringBuilder instead
private void Window_Loaded(System.Object sender, System.Windows.RoutedEventArgs e)
{
    MainWindow.PreviewKeyDown += labelBarCode_PreviewKeyDown;
}

private void labelBarCode_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if ((44 == e.Key)) e.Handled = true;
    barCode += e.Key;
    //look for a terminator char (different barcode scanners output different 
    //control characters like tab and line feeds), a barcode char length and other criteria 
    //like human typing speed &/or a lookup to confirm the scanned input is a barcode, eg.
    if (barCode.Length == 7) {
       var foundItem = DoLookUp(barCode);
       barCode = string.Empty;
    }
}


See update, as at March 2019:https://stackoverflow.com/a/55411255/495455

查看更新,截至 2019 年 3 月:https : //stackoverflow.com/a/55411255/495455