【问题标题】:Generate QR code with Xamarin.Forms and Zxing使用 Xamarin.Forms 和 Zxing 生成 QR 码
【发布时间】:2017-03-01 13:12:06
【问题描述】:

我在网上看到了很多关于此的内容(旧帖子),但似乎没有什么对我有用。 我正在尝试从字符串中生成二维码并将其显示在应用程序中。

这就是我一开始所拥有的

qrCode = new ZXingBarcodeImageView
{
    BarcodeFormat = BarcodeFormat.QR_CODE,
    BarcodeOptions = new QrCodeEncodingOptions
    {
        Height  = 50,
        Width   = 50
    },
    BarcodeValue = codeValue,
    VerticalOptions = LayoutOptions.CenterAndExpand,
    HorizontalOptions = LayoutOptions.CenterAndExpand
};

这适用于 Android,但在 IOS 设备上它根本不呈现。 所以在研究之后我试着这样做:

Image qrCode;

if (Device.OS == TargetPlatform.iOS)
{
    var writer = new BarcodeWriter
    {
        Format = BarcodeFormat.QR_CODE,
        Options = new ZXing.Common.EncodingOptions
        {
            Width = 50,
            Height = 50
        }
    };

    var b = writer.Write(codeValue);

    qrCode = new Image
    {
        Aspect = Aspect.AspectFill,
        VerticalOptions = LayoutOptions.CenterAndExpand,
        HorizontalOptions = LayoutOptions.CenterAndExpand,
        Source = ImageSource.FromStream(() =>
        {
            MemoryStream ms = new MemoryStream(b);
            ms.Position = 0;
            return ms;
        })
    };

}else{
    qrCode = new ZXingBarcodeImageView
    {
        BarcodeFormat = BarcodeFormat.QR_CODE,
        BarcodeOptions = new QrCodeEncodingOptions
        {
            Height  = 50,
            Width   = 50
        },
        BarcodeValue = codeValue,
        VerticalOptions = LayoutOptions.CenterAndExpand,
        HorizontalOptions = LayoutOptions.CenterAndExpand
    };
}

Content = new StackLayout
{
    Children = {
        header, lblExplenationText, qrCode
    },
    BackgroundColor = Color.White
};

但是仍然没有渲染任何东西。

ZXing.Mobile.Forms NuGet 包版本:2.1.47(最新)

【问题讨论】:

  • 您找到解决方案了吗?如果没有,我可以帮你开始赏金

标签: c# xamarin xamarin.ios xamarin.forms qr-code


【解决方案1】:

这似乎是一个已知的issue
幸运的是,有一个解决方法,设置一个HeightRequestWidthRequest,这是一个工作代码示例:

ZXingBarcodeImageView GenerateQR(string codeValue)
{
    var qrCode = new ZXingBarcodeImageView
    {
        BarcodeFormat = BarcodeFormat.QR_CODE,
        BarcodeOptions = new QrCodeEncodingOptions
        {
            Height = 350,
            Width = 350
        },
        BarcodeValue = codeValue,
        VerticalOptions = LayoutOptions.CenterAndExpand,
        HorizontalOptions = LayoutOptions.CenterAndExpand
    };
    // Workaround for iOS
    qrCode.WidthRequest = 350;
    qrCode.HeightRequest = 350;
    return qrCode;
}

【讨论】:

  • 二维码20秒后如何失效?
【解决方案2】:

在应用程序中添加委托这一行 ZXing.Net.Mobile.Forms.iOS.Platform.Init();

在LoadApplication(new App())之前;

准备好了……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多