ZXingBarcodeImageView(QR 码)显示在 iOS 但不显示在 Android (Xamarin.Forms)
ZXingBarcodeImageView (QR code) showing on iOS but not on Android (Xamarin.Forms)
这段用于在 Xamarin.Forms 应用程序中显示二维码的代码在 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 的二维码:
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.Forms 应用程序中显示二维码的代码在 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 的二维码:
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。