[英]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.