2017-01-07 53 views
1

我當前嘗試爲Windows 10創建條碼掃描器UWP App。目的是爲了使用掃描的條碼作爲Web搜索的輸入。使用ZXing.Net.Mobile result.text作爲WebView的輸入

我使用了有據可查的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\nPress 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(); 
     }); 
    } 

不過,我不希望被顯示在消息框中的scannaing結果。我想用它來進行網絡搜索。所以,我創建了一個在MainPage.xaml頁面命名SearchURI的WebView:

<WebView Name="SearchURI" /> 

然後,我測試的基本功能,導航到谷歌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); 

    } 

但是,這些代碼行會出現錯誤。

有人可以幫助我調整代碼,讓它工作嗎?謝謝!

+0

你有什麼類型的錯誤?崩潰? –

回答

1

包裹NavigateDispatcher

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

其實它的方式比較容易的方式。以下完美的作品:

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