public CodeScannerPage() { InitializeComponent(); // 取得這個頁面的檢視模型(ViewModel)物件 foo本頁面的檢視模型 = this.BindingContext as CodeScannerPageViewModel; #region 指定要掃描的條碼類型 var fooMobileBarcodeScanningOptions = new MobileBarcodeScanningOptions(); fooMobileBarcodeScanningOptions.PossibleFormats = new List <ZXing.BarcodeFormat>() { BarcodeFormat.QR_CODE, BarcodeFormat.CODE_128, BarcodeFormat.CODE_39, BarcodeFormat.CODABAR }; #endregion #region 建立條碼掃描控制項 zxing = new ZXingScannerView { HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand, AutomationId = "zxingScannerView", }; #region 設定完成條碼掃描後,要進行處理的工作 zxing.OnScanResult += (result) => Device.BeginInvokeOnMainThread(async() => { // 停止分析掃描條碼工作 zxing.IsAnalyzing = false; // 將掃描結果回傳到首頁 foo本頁面的檢視模型._eventAggregator.GetEvent <ScanResultEvent>().Publish(result.Text); // 回到上一頁 await foo本頁面的檢視模型._navigationService.GoBackAsync(); }); #endregion #endregion #region 建立條碼掃描遮罩 overlay = new ZXingDefaultOverlay { ShowFlashButton = zxing.HasTorch, }; // 當按下 Flash 按鈕,就會顯示燈光,幫助更順利讀取條碼 overlay.FlashButtonClicked += (sender, e) => { zxing.IsTorchOn = !zxing.IsTorchOn; }; #endregion // 將條碼掃描與分析控制項,加入到頁面上 ScannerGrid.Children.Add(zxing); // 將遮罩加入到頁面上 ScannerGrid.Children.Add(overlay); }
public CodeScannerPage() { InitializeComponent(); BindingContext = new CodeScannerPageViewModel(); }