ScannerPage.xaml.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using Comal.Classes;
  2. using InABox.Core;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Xamarin.Essentials;
  9. using Xamarin.Forms;
  10. using ZXing;
  11. using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
  12. using XF.Material.Forms.UI.Dialogs;
  13. using InABox.Mobile;
  14. using LogType = InABox.Core.LogType;
  15. namespace PRS.Mobile
  16. {
  17. public class ScannerPageItemScannedArgs : EventArgs
  18. {
  19. public String Text { get; set; }
  20. public ScannerPageItemScannedArgs(String text)
  21. {
  22. Text = text;
  23. }
  24. }
  25. public partial class ScannerPage
  26. {
  27. bool bProcessing = false;
  28. public Func<ScannerPageItemScannedArgs, Task> ItemScanned;
  29. public ScannerPage()
  30. {
  31. InitializeComponent();
  32. var options = new ZXing.Mobile.MobileBarcodeScanningOptions()
  33. {
  34. PossibleFormats = new List<ZXing.BarcodeFormat>() { ZXing.BarcodeFormat.QR_CODE },
  35. AutoRotate = false,
  36. TryInverted = true,
  37. TryHarder = true,
  38. };
  39. _scanView.Options = options;
  40. _scanView.IsAnalyzing = false;
  41. _scanView.IsScanning = true;
  42. }
  43. protected override void OnAppearing()
  44. {
  45. base.OnAppearing();
  46. _scanView.IsAnalyzing = true;
  47. }
  48. protected override void OnDisappearing()
  49. {
  50. _scanView.IsAnalyzing = false;
  51. base.OnDisappearing();
  52. }
  53. public async void _scanView_OnScanResult(ZXing.Result result)
  54. {
  55. if (bProcessing)
  56. return;
  57. bProcessing = true;
  58. Vibration.Vibrate();
  59. if (ItemScanned != null)
  60. await ItemScanned?.Invoke(new ScannerPageItemScannedArgs(result.Text))!;
  61. bProcessing = false;
  62. }
  63. }
  64. }