首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用zxing扫描条形码

使用zxing扫描条形码
EN

Stack Overflow用户
提问于 2020-12-29 03:18:24
回答 1查看 101关注 0票数 0

<zxing:ZXingScannerView IsScanning="True" OnScanResult="OnScanResult"/>我在xaml文件上有这个,但是我想使用mvvm,所以我在视图模型中有onscanresult平均处理器。我怎样才能用它来代替代码背后的那个呢?

代码语言:javascript
复制
public void OnScanResult(ZXing.Result result)
{
    Device.BeginInvokeOnMainThread(() =>
    {
        ScanResult = result.Text + " (type: " + result.BarcodeFormat + ")";
    });
}
EN

回答 1

Stack Overflow用户

发布于 2020-12-29 15:26:40

如果你不想使用某个行为,只需创建一个新版本的ContentPage来创建一个事件来执行命令:

代码语言:javascript
复制
    public class ZxingContentPage : ZXingScannerPage
{
    public static readonly BindableProperty ScanResultCommandProperty =
        BindableProperty.Create(
            nameof(ScanResultCommand),
            typeof(ICommand),
            typeof(ZxingContentPage),
            default(ICommand)
            );

    public ICommand ScanResultCommand
    {
        get { return (ICommand)GetValue(ScanResultCommandProperty); }
        set { SetValue(ScanResultCommandProperty, value); }
    }

    public ZxingContentPage(MobileBarcodeScanningOptions options) : base(options)
    {
        OnScanResult += ZxingContentPage_OnScanResult;
    }

    private void ZxingContentPage_OnScanResult(ZXing.Result result)
    {
        if (ScanResultCommand?.CanExecute(result) == true)
        {
            ScanResultCommand.Execute(result);
        }
    }
}

然后像下面这样使用它:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8" ?>
<views:ZxingContentPage
  x:Class="NameSpace.Zxing.ZxingMobileScannerView"
  xmlns="http://xamarin.com/schemas/2014/forms"
  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  xmlns:views="clr-namespace:QRST.Views.Generic"
  IsScanning="True"
  ScanResultCommand="{Binding ZxingScanResultCommand}" />

祝好运

如果有疑问,请随时返回。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65482878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档