zxing qr/barcode 使用 MVVM 扫描?

zxing qr/barcode scanning using MVVM?

我是 xamarin 的新手,我正在尝试制作一个按钮来打开一个扫描器表单,该表单扫描 qr/barcode 这是 MVVM 方法。我正在尝试获取结果并将其显示到标签中。这是我最好的客人,但它不起作用,希望有人能帮忙。

view:
<StackLayout>
 <Label Text="{Binding CodigoQr}"/>
 <zxing:ZXingScannerView x:Name="ucZXingScannerView" 
  IsScanning="True" 
  IsAnalyzing="True"
  Result="{Binding CodigoQr}"
  ScanResultCommand="{Binding ScanCommand }" />
 </StackLayout>

ViewModel:
public class BarcodeScanVM : BaseViewModel
    {
        private Result _codigoQr;
        public Result CodigoQr
        {
            get { return _codigoQr; }
            set
            {
                _codigoQr = value;
                OnPropertyChanged();
            }
        }
        public AsyncCommand ScanCommand { get; set; }
        public BarcodeScanVM()
        {
            ScanCommand = new AsyncCommand(OnScanResultCommand);
        }
        async Task OnScanResultCommand()
        {
            var text = CodigoQr;
        }
    }```

您可以使用代码隐藏视图来执行操作。并将 VM 用于其他属性

XAML:

 <zxing:ZXingScannerView
        IsAnalyzing="{Binding IsAnalyzing}"
        IsScanning="{Binding IsScanning}"
        OnScanResult="CameraScanner_OnScanResult" />

后面的代码:

private void CameraScanner_OnScanResult(ZXing.Result result)
        {   
             ((MyViewModel)BindingContext).OnScanComplete(result.Text);
        }

更新:我试过这个,扫描命令好像可以,但之后程序停止了。

ViewMode:
private Result bcScanResult;
    public Result BcScanResult
    {
        get => bcScanResult;
        set
        {

            if (value == bcScanResult)
                return;
            bcScanResult = value;
            OnPropertyChanged();
        }
    }

  public AsyncCommand BcScanCommand { get; }

  public CodeScanVM()
    {
        BcScanCommand = new AsyncCommand(BcScanCommand_Call);
    }

  async Task BcScanCommand_Call()
    {
        await App.Current.MainPage.DisplayAlert("Item", "Code Async 
        Command:" + Result, "OK");
        return;
    }

 View:
 <zxing:ZXingScannerView
            x:Name="ScanView"
            Result="{Binding BcScanResult}"
            ScanResultCommand="{Binding BcScanCommand }"
            IsScanning="True"
            WidthRequest="300"
            HeightRequest="300"/>

不使用 ScannerView 试试。在 XAML 中添加一个标签(我正在使用一个条目)和一个打开扫描仪的按钮:

<Button Text="QR Scan"
        TextColor="White"
        CornerRadius="30"
        Clicked="ButtonScan"/>

<Entry BackgroundColor="White"
       IsTextPredictionEnabled="False"
       TextTransform="Uppercase"
       FontSize="Body"
       TextChanged="Search"
       Placeholder="Search"
       TextColor="Black"
       PlaceholderColor="Black"
       x:Name="lblBarcode"
       Keyboard="Chat">

关于按钮的点击事件:

private async void ButtonScan(object sender, EventArgs e)
    {
        PermissionStatus granted = await Permissions.CheckStatusAsync<Permissions.Camera>();
        if (granted != PermissionStatus.Granted)
        {
            _ = await Permissions.RequestAsync<Permissions.Camera>();
        }
        if (granted == PermissionStatus.Granted)
        {
            try
            {
                MobileBarcodeScanner scanner = new MobileBarcodeScanner();
                ZXing.Result result = await scanner.Scan();
                if (result != null && result.Text != "")
                {
                    lblBarcode.Text = result.Text; // <--- This places the result of scanner at Entry/Label
                    scanner.Cancel(); // <--- This closes the scanner
                }
            }
            catch (Exception)
            {
                await DisplayAlert("Problem", "Something went wrong.", "ΟΚ");
            }
        }
        else
        {
            await DisplayAlert("Problem", "No permissions to use camera.", "ΟΚ");
        }
    }