简体   繁体   English

使用ZXing.Net.Mobile result.text作为WebView的输入

[英]Use ZXing.Net.Mobile result.text as input for WebView

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. 我目前正在尝试为Windows 10创建一个条形码扫描仪UWP App。其目的是我想使用扫描的条形码作为Web搜索的输入。

I used the well documented ZXing.Net.Mobile package and I already got the scanner running in the app. 我使用了详细记录的ZXing.Net.Mobile软件包,并且已经在应用程序中运行了扫描仪。 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: 我在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();
    });
}

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: 因此,我在MainPage.xaml页面中创建了一个名为SearchURI的WebView:

<WebView Name="SearchURI" />

Then I tested the Basic function, navigating to Google fo instance, and it worked fine: 然后,我测试了Basic函数,导航到Google fo实例,并且工作正常:

public MainPage()
{
    SearchURI.Navigate(new Uri("https://www.google.com")); 
}

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. 然后,我尝试调整HandleScanResult以将result.text添加到预定义的Uri,并在WebView“ SearchURI”中打开组合的Uri,同时尝试保留消息框以防扫描被取消。

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! 谢谢!

Wrap the Navigate in Dispatcher Dispatcher Navigate

await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
    SearchURI.Navigate(new Uri("https://www.google.com/?gfe_rd=cr&ei=ccRwWIS7D8ao8weY9b_ADA#q=" + result.Text));
});

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));            
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM