繁体   English   中英

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

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

我目前正在尝试为Windows 10创建一个条形码扫描仪UWP App。其目的是我想使用扫描的条形码作为Web搜索的输入。

我使用了详细记录的ZXing.Net.Mobile软件包,并且已经在应用程序中运行了扫描仪。 扫描仪启动,扫描条形码,结果显示在消息框中。 我在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:

<WebView Name="SearchURI" />

然后,我测试了Basic函数,导航到Google fo实例,并且工作正常:

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

然后,我尝试调整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);
}

但是,这些代码行会遇到错误。

有人可以帮助我调整《准则》以使其正常工作吗? 谢谢!

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

实际上,这很容易。 以下完美的工作方式:

    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