使用 ZXing.Net.Mobile result.text 作为 WebView 的输入 [英] Use ZXing.Net.Mobile result.text as input for WebView
问题描述
我目前尝试为 Windows 10 创建一个条形码扫描仪 UWP 应用程序.目的是我想使用扫描的条形码作为网络搜索的输入.
i currently try to create a Barcode scanner UWP App for Windows 10. The purpose is that i want to use the scanned Barcode as input for a web search.
我使用了有据可查的 ZXing.Net.Mobile 包,我已经得到了在应用程序中运行的扫描仪.扫描仪启动,条码被扫描,结果显示在消息框中.我在 MainPage.xaml.cs 中使用了以下代码行:
I used the well documented ZXing.Net.Mobile package and I already got the scanner running in the app. The scanner starts, the Barcode is scanned and the result is displayed in a message box. I used the following line of Code in the MainPage.xaml.cs:
MobileBarcodeScanner scanner;
public MainPage()
{
//Create a new instance of our scanner
scanner = new MobileBarcodeScanner(this.Dispatcher);
scanner.RootFrame = this.Frame;
this.Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
//Tell our scanner to use the default overlay
scanner.UseCustomOverlay = false;
//We can customize the top and bottom text of our default overlay
scanner.TopText = "Hold camera up to barcode";
scanner.BottomText = "Camera will automatically scan barcode\r\n\r\n" +
"Press the 'Back' button to Cancel";
//Start scanning
scanner.Scan().ContinueWith(t =>
{
if (t.Result != null)
HandleScanResult(t.Result);
});
}
async void HandleScanResult(ZXing.Result result)
{
string msg = "";
if (result != null && !string.IsNullOrEmpty(result.Text))
msg = "Found Barcode: " + result.Text;
else
msg = "Scanning Canceled!";
await MessageBox(msg);
}
async Task MessageBox(string text)
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
var dialog = new MessageDialog(text);
await dialog.ShowAsync();
});
}
但是,我不希望在消息框中显示扫描结果.我想用它进行网络搜索.因此,我在 MainPage.xaml 页面中创建了一个名为 SearchURI 的 WebView:
However, I do not want the scannaing result to be displayed in a message box. I want to use it for a web search. So, I created a WebView named SearchURI in the MainPage.xaml page:
<WebView Name="SearchURI" />
然后我测试了基本功能,导航到 Google fo 实例,它运行良好:
Then I tested the Basic function, navigating to Google fo instance, and it worked fine:
public MainPage()
{
SearchURI.Navigate(new Uri("https://www.google.com"));
}
然后我尝试调整 HandleScanResult 以将 result.text 添加到预定义的 Uri 并在 WebViewSearchURI"中打开组合的 Uri,同时我尝试保留消息框以应对扫描被取消的情况.
Then I tried to adjust HandleScanResult to add result.text to a predefined Uri and open the combined Uri in the WebView "SearchURI", while i tried to Keep the message box for the case that the scan is canceled.
async void HandleScanResult(ZXing.Result result)
{
string msg = "";
if (result != null && !string.IsNullOrEmpty(result.Text))
SearchURI.Navigate(new Uri(
"https://www.google.com/?gfe_rd=cr&ei=ccRwWIS7D8ao8weY9b_ADA#q=" + result.Text));
else
msg = "Scanning Canceled!";
await MessageBox(msg);
}
然而,这些代码行遇到了错误.
However those lines of Code run into Errors.
有人可以帮我调整代码以使其正常工作吗?谢谢!
Can someone help me to adjust the Code to get it working? Thanks!
推荐答案
实际上更容易.以下完美工作:
Actually it way way easier. The folowing perfectly works:
private async void ScanButton_Click(object sender, RoutedEventArgs e)
{
var scanner = new MobileBarcodeScanner(this.Dispatcher);
scanner.UseCustomOverlay = false;
scanner.TopText = "Halte die Kamera vor den Barcode";
scanner.BottomText = "Kamera scannt den Barcode automatisch\r\n\rDrücke 'zurück' zum abbrechen";
var result = await scanner.Scan();
SearchURI.Navigate(new Uri("https://www.google.com/?gfe_rd=cr&ei=ccRwWIS7D8ao8weY9b_ADA#q=" + result.Text));
}
这篇关于使用 ZXing.Net.Mobile result.text 作为 WebView 的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!