【问题标题】:ZXing Barcode ImageView not showing generated barcode Xamarin FormsZXing Barcode ImageView 不显示生成的条形码 Xamarin Forms
【发布时间】:2016-08-02 12:56:53
【问题描述】:

我目前正在使用 Xamarin Forms(共享)开发跨平台应用程序。我需要生成一个 EAN-13 条形码,我的代码在 Android 上运行良好,但在 iOS 上没有任何反应。我正在使用 ZXingBarcodeImageView。 这是我的代码。

public class CardPage : ContentPage
{
  ZXingBarcodeImageView barcode = new ZXingBarcodeImageView
  {
    HorizontalOptions = LayoutOptions.Center,
    VerticalOptions = LayoutOptions.Center,
  };

  barcode.BarcodeFormat = ZXing.BarcodeFormat.EAN_13;
  barcode.BarcodeOptions.Height = 25;
  barcode.BarcodeOptions.Width = 75;
  barcode.BarcodeValue = "2800100028014";

  Content = barcode;
}

编辑

好的,所以我编写了特定于平台的代码来处理这个问题,直到问题得到解决。所以现在我的代码是这样的。

#if __IOS__

  var writer = new ZXing.Mobile.BarcodeWriter
  {
    Format = ZXing.BarcodeFormat.EAN_13,
    Options = new ZXing.Common.EncodingOptions
    {
      Width = 75,
      Height = 25,
      Margin = 30
    }
  };

  var b = writer.Write("2800100028014");

  Image m = new Image
  {
    HorizontalOptions = LayoutOptions.FillAndExpand,
    VerticalOptions = LayoutOptions.FillAndExpand,
    Source = ImageSource.FromStream(() => b.AsPNG().AsStream())
  };

  Content = m;
#endif
#if __ANDROID__

  ZXingBarcodeImageView barcode = new ZXingBarcodeImageView
  {
    HorizontalOptions = LayoutOptions.FillAndExpand,
    VerticalOptions = LayoutOptions.FillAndExpand,
  };

  barcode.BarcodeFormat = ZXing.BarcodeFormat.EAN_13;
  barcode.BarcodeOptions.Height = 25;
  barcode.BarcodeOptions.Width = 75;
  barcode.BarcodeOptions.Margin = 20;
  barcode.BarcodeValue = "2800100028014";

  Content = barcode;
#endif

【问题讨论】:

    标签: c# xamarin xamarin.ios xamarin.forms zxing


    【解决方案1】:

    好的,所以这不是包裹中的问题。只需要像这样正确初始化它。

    在Android平台上,我的MainActivity.cs是这样的,需要初始化的是ZXing.Net:

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        global::Xamarin.Forms.Forms.Init (this, bundle);
        global::ZXing.Net.Mobile.Forms.Android.Platform.Init();
        LoadApplication (new Test.App ());
        this.ActionBar.SetIcon(Android.Resource.Color.Transparent);
    }
    

    在iOS平台上,我的AppDelegat.cs是这样的,需要初始化的是ZXing.Net:

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        global::Xamarin.Forms.Forms.Init ();
        global::ZXing.Net.Mobile.Forms.iOS.Platform.Init();
        LoadApplication (new Test.App ());
        return base.FinishedLaunching (app, options);
    }
    

    【讨论】:

    • global::ZXing.Net.Mobile.Forms.iOS.Platform.Init();给我错误。 Net.Mobile.....在ZXing中没有找到。
    • 你使用了什么 nugetpackage?我正在使用
    • 我安装了包 ZXing.Net.Mobile。版本=2.1.47
    • @DipakAkhade - 你是否构建了你的 iOS 版本?
    • 是的。它现在运作良好。我只是重新安装 Zxing.Net.Mobile 包。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2021-03-03
    • 2014-10-16
    • 1970-01-01
    • 2011-11-11
    • 2020-02-17
    • 1970-01-01
    • 2017-09-13
    • 2019-12-05
    相关资源
    最近更新 更多