c# - ZXing Barcode ImageView不显示生成的条形码Xamarin Forms

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

我目前正在使用 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

最佳答案

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

在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);
}

关于c# - ZXing Barcode ImageView不显示生成的条形码Xamarin Forms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36564354/

相关文章:

c# - Objective C 中的 Xamarin Studio 类 HttpWebRequest 替代品是什么?

xamarin.ios - Xamarin MVVMCross 替代方案

ios - 在哪里可以找到 xamarin ios 设备日志?

c# - C#:使用无参数在功能上处于条件状态时?

c# - 如何为类型创建扩展方法

c# - DateTime代表C#中的多少位

c# - 使用 Objective Sharpie 绑定(bind) CocoaPods 库

xamarin - 'Microsoft.EntityFrameworkCore.Query.ResultOperators.Internal.TrackingExpressionNode' 的类型初始值设定项引发异常

ios - OpenGL ES 2.0/iOS : Skewed drawing of RGBA4 texture

c# - 为什么此代码段将答案显示为 6?