C# 使用Xamarin.Forms和Zxing生成二维码
我在网上看到了很多关于这方面的文章(老帖子),但对我来说似乎什么都不管用。 我正在尝试从字符串中生成二维码,并将其显示在应用程序中 这是我一开始所拥有的C# 使用Xamarin.Forms和Zxing生成二维码,c#,xamarin,xamarin.ios,xamarin.forms,qr-code,C#,Xamarin,Xamarin.ios,Xamarin.forms,Qr Code,我在网上看到了很多关于这方面的文章(老帖子),但对我来说似乎什么都不管用。 我正在尝试从字符串中生成二维码,并将其显示在应用程序中 这是我一开始所拥有的 qrCode = new ZXingBarcodeImageView { BarcodeFormat = BarcodeFormat.QR_CODE, BarcodeOptions = new QrCodeEncodingOptions { Height = 50, Width =
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(最新)这似乎是一个已知的版本。幸运的是,有一个解决方法,要设置
高度请求
&宽度请求
,下面是一个工作代码示例:
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;
}
将应用程序代理添加到此行 ZXing.Net.Mobile.Forms.iOS.Platform.Init() 加载应用程序之前(新应用程序())
准备好了…您找到解决方案了吗?如果没有,我可以帮你开始悬赏