这段用于在 Xamarin.Forms 应用中显示 QR 码的代码适用于 iOS,但不适用于 Android:
let barCode = ZXingBarcodeImageView(HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
BarcodeFormat = ZXing.BarcodeFormat.QR_CODE,
BarcodeValue = foo)
barCode.BarcodeOptions.Width <- 500
barCode.BarcodeOptions.Height <- 500
mainLayout.Children.Add(barCode)
日志中没有错误,也没有抛出异常。尝试了许多高度和宽度以及不同的 LayoutOptions 但无济于事。我该如何调试这个?
最佳答案
幸运的是,我只需在自己的 Xamarin.Forms 项目中使用 ZXing.Net.Mobile
即可。我设法使用下一个 C# 代码显示 iOS 和 Android 的 QRCode:
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;
}
请注意,有一个已知的issue在此库中,您必须显式设置 WidthRequest 和 HeightRequest。
P.S.:或多或少已经讨论过相同的问题here也是如此。
关于xamarin - ZXingBarcodeImageView(QR 代码)在 iOS 上显示,但在 Android 上不显示(Xamarin.Forms),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49105986/