简体   繁体   中英

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.

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

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" />

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

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!

Wrap the Navigate in Dispatcher

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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