【问题标题】:Generate barcode using Zxing in xamarin.forms在 xamarin.forms 中使用 Zxing 生成条形码
【发布时间】:2018-06-29 16:20:44
【问题描述】:

我正在尝试使用我的 xaml 中的绑定在我的应用中显示条形码。我的问题是如何转换要在 xaml 图像源中使用的条形码。我曾尝试使用字节属性,但出现此编译错误:“无法将 Zxing 条形码图像视图隐式转换为'字节'”。任何有关如何实现这一目标的指导将不胜感激,谢谢。

Cards.cs

 public class Cards
    {
        public int CustomerID { get; set; }
        public int DiscountLevelID { get; set; }
        public string DiscountLevel { get; set; }
        public double DiscountLevelAmount { get; set; }
        public bool StoreCustomerGiftCard { get; set; }
        public bool StoreCustomerLoyalty { get; set; }
        public int LoyaltyLevelID { get; set; }
        public string LoyaltyLevel { get; set; }
        public double LoyaltyLevelRatio { get; set; }
        public double Balance { get; set; }
        public int StoreNumber { get; set; }
        public string CardNumber { get; set; }
        public bool IsError { get; set; }
        public object ErrorMessage { get; set; }
        public string CompanyName { get; set; }
        public string CustomerLogo { get; set; }
        public byte BarCode { get; set; }

    }

Cards.xaml.cs

ZXingBarcodeImageView barcode;

 barcode = new ZXingBarcodeImageView { HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand };
                        barcode.BarcodeFormat = ZXing.BarcodeFormat.QR_CODE;
                        barcode.BarcodeOptions.Width = 300;
                        barcode.BarcodeOptions.Height = 300;
                        barcode.BarcodeOptions.Margin = 10;
                        barcode.BarcodeValue = i.CardNumber;

                        i.BarCode = barcode;

Cards.xaml

<Image Source="{Binding BarCode}" />

【问题讨论】:

    标签: xamarin xamarin.forms zxing.net


    【解决方案1】:

    ZXingBarcodeImageView 直接继承自Image,因此您可以将其用作Image 的替代品,而不是作为Image 的来源

    xmlns:zx="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"
    xmlns:zxcm="clr-namespace:ZXing.Common;assembly=zxing.portable"
    
    <zx:ZXingBarcodeImageView
        BarcodeFormat="QR_CODE"
        BarcodeValue="{Binding CardNumber}"
        HorizontalOptions="FillAndExpand"
        VerticalOptions="FillAndExpand">
        <zx:ZXingBarcodeImageView.BarcodeOptions>
          <zxcm:EncodingOptions Width="300" Height="300" />
        </zx:ZXingBarcodeImageView.BarcodeOptions>
    </zx:ZXingBarcodeImageView>
    

    【讨论】:

    • 谢谢。我已经试过你说的,条形码仍然没有显示:(
    • 我收到以下异常:System.Reflection.TargetInvocationException: 异常已被调用的目标抛出。
    • 我将此添加到我的 MainActivity.cs,global::ZXing.Net.Mobile.Forms.Android.Platform.Init(); .知道为什么它不起作用
    • 你需要查看 InnerException 才能找到根本原因
    • @JanineAlexander 修复了吗?我也有类似的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 2011-09-16
    • 2016-08-02
    • 2021-03-03
    • 1970-01-01
    相关资源
    最近更新 更多