2016-07-22 265 views
0

我正在Visual Studio上爲UWP,Android和IOS在Xamarin Forms上開發移動應用程序。ZXing.Mobile.MobileBarcodeScanner.Cancel()不起作用

我目前正在測試我的電腦(Windows 10)和我的手機(也是Windows 10)上的應用程序。

我正在使用Zxing MobileBarcodeScanner掃描多個條形碼。

當我按下後退按鈕時,我打電話給MobileBarcodeScanner.Cancel()。

它唯一能做的就是關閉相機。它並沒有破壞MobileBarcodeScanner的用戶界面,我沒有找到任何解決方案。

任何人都可以幫助我或建議解決方案嗎?

此外,取消按鈕和Flash按鈕不會顯示在掃描儀UI中。

代碼:

private void showScanner() 
{ 
    var scanner = new MobileBarcodeScanner(App.coreDispatcher) 
    { 
     UseCustomOverlay = false, 
     TopText = "Hold camera up to barcode to scan", 
     BottomText = "Barcode will automatically scan", 
     CancelButtonText = "Done", 
     FlashButtonText = "Flash" 
    } 

    var opt = new MobileBarcodeScanningOptions { DelayBetweenContinuousScans = 3000 }; 

    scanner.ScanContinuously(opt, HandleScanResult); 
} 

protected override bool OnBackButtonPressed() 
{ 
    scanner.Cancel(); 
} 

private void HandleScanResult(ZXing.Result result) 
{ 
    string msg; 
    if (result != null && !string.IsNullOrEmpty(result.Text)) // Success 
    { 
     msg = result.Text; 
    } 
    else // Canceled 
    { 
     msg = "Scanning Canceled!"; 
    } 
} 

回答

0

,它所做的唯一一件事就是關閉相機。它不會破壞MobileBarcodeScanner的用戶界面,我也沒有找到任何解決方案

您的代碼片段中存在一個問題。在showScanner()方法中,您已定義scanner變量,但在OnBackButtonPressed()方法中,看起來您已使用全局變量,也稱爲scanner

也許下面的方式應該是正確的:

MobileBarcodeScanner scanner; 

private void showScanner(){ 
    scanner = new MobileBarcodeScanner(App.coreDispatcher) //Here, remove var 
    { 
     ...... 
    } 

} 
protected override bool OnBackButtonPressed() 
{ 
    scanner.Cancel(); 
} 

如果你有檢查Xamarin.Forms sample

它使用ZXingScannerPage託管您的佈局和處理一些邏輯,包括取消和ToggleTorch等,見here

此外,取消按鈕和Flash按鈕不會顯示在掃描儀UI中。

請使用自定義背景畫面,只需設置MobileBarcodeScanner.UseCustomOverlay屬性爲true並檢查示例代碼here

而且還ZXing.Net.Mobile Getting Started

+0

我現在用的是MobileBarcodeScanner同時掃描多個條碼時間。 ZXingScannerPage只支持每次掃描一次。也沒有辦法讓ZXingScannerPage來處理MobileBarcodeScanner。正如我在這裏看到的https://github.com/Redth/ZXing.Net.Mobile/issues/366/這是UWP的一個常見問題 – Panayiotis

+0

我的代碼也是基於這個例子https://forums.xamarin.com/討論/ 49643 /求助斑馬線,移動scancontinuously – Panayiotis