0

我正在尝试使用 ZXing 在 Xamarin 的屏幕上呈现一个简单的条形码。我已将 ZXing 包添加到我的解决方案中,并具有以下代码:

        BarcodeWriterPixelData writer = new BarcodeWriterPixelData
        {
            Format = BarcodeFormat.CODE_39 ,
            Options = new ZXing.Common.EncodingOptions{
                Height = 80,
                Width = 30,
                Margin = 0
            }
        };
        var data = writer.Write("18898798790");
        IDBarCode.Source = ImageSource.FromStream(() => new MemoryStream(data.Pixels));

但是,我的代码运行时没有在屏幕上呈现任何内容。我一直在寻找某种我可以在没有任何成功的情况下使用的样本。

4

2 回答 2

1

不要忘记在您的平台项目中初始化 ZXing 库,否则它将显示为空。您可以通过添加:

// On iOS in your AppDelegate.cs in the FinishedLaunching method
ZXing.Net.Mobile.Forms.iOS.Platform.Init();

// On Android in the MainActivity.cs in the OnCreate method
ZXing.Net.Mobile.Forms.Android.Platform.Init();
于 2018-08-08T17:47:14.090 回答
0

在尝试了很多不同的东西但无济于事并联系了 ZXing 的开发团队后,我放弃了并使用了不同的方法。

如果使用普通图像,我使用了 ZXing 组件,现在一切正常。这是代码:

    private ZXingBarcodeImageView barcode=null;
    public MainPage()
    {
        InitializeComponent();
    }

    void Handle_Clicked(object sender, System.EventArgs e)
    {
        if (barcode==null){
            barcode = new ZXingBarcodeImageView
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand,
            };
            barcode.BarcodeFormat = ZXing.BarcodeFormat.CODE_128;
            barcode.BarcodeOptions.Width = 500;
            barcode.BarcodeOptions.Height = 100;
            barcode.BarcodeValue = contentEntry.Text.Trim();
            barResult.Content = barcode;
        }else{
            barcode.BarcodeValue = contentEntry.Text.Trim();
        }

    }

barResult 只是一个简单的 ContentView,其中没有任何内容。

        <ContentView x:Name="barResult">                
        </ContentView>
于 2018-08-09T20:47:14.453 回答