使用ZXing.Net.Mobile result.text作为WebView

时间:2017-01-07 10:49:20

标签: c# mobile webview uwp zxing

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

我使用了记录良好的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" />

然后我测试了基本功能,导航到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);
}

然而,这些代码行会遇到错误。

有人可以帮助我调整代码以使其正常工作吗?谢谢!

2 个答案:

答案 0 :(得分:1)

Navigate包裹在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));
});

答案 1 :(得分:1)

实际上它更方便了。以下完美的作品:

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