xamarin 使用MVVM进行qr/条形码扫描?

5f0d552i  于 2022-12-07  发布在  其他
关注(0)|答案(4)|浏览(257)

我新的xamarin,我试图使一个按钮,打开一个扫描仪的形式,扫描qr/条形码,这是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;
        }
    }```
iecba09b

iecba09b1#

You can use the code-behind the view for the actions. And use the VM for other properties
XAML:

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

Code behind:

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

62lalag42#

尝试不使用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">

在按钮的Clicked事件上:

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.", "ΟΚ");
        }
    }
nmpmafwu

nmpmafwu3#

更新:我试过这个,看起来扫描命令起作用了,但是程序后来停止了。

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"/>
xvw2m8pv

xvw2m8pv4#

您可以在github上给予我的示例应用程序
xam-barcode-scanner
它可以在两个平台上工作,并且不需要ZXing库

相关问题