2

This piece of code for showing a QR code in a Xamarin.Forms app works in iOS but not on 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)

There's no error in the log, no exception thrown. Tried many heights and widths and different LayoutOptions to no avail. How can I debug this?

knocte
  • 16,941
  • 11
  • 79
  • 125
  • Did you initialise the library for Android? https://github.com/Redth/ZXing.Net.Mobile#android Here is a C# example that is working on iOS and Android https://stackoverflow.com/a/49000467/1970317 – EvZ Mar 05 '18 at 08:19
  • yes I initialized it, full patch here https://gist.github.com/knocte/cc638cb196a6938fb62856c7ab3d454a – knocte Mar 05 '18 at 08:46
  • @EvZ setting HeightRequest and WitdthRequest worked!!! – knocte Mar 05 '18 at 12:07
  • Glad to help, posted it as an answer. – EvZ Mar 05 '18 at 12:13

1 Answers1

3

Luckily, I just had to play around with ZXing.Net.Mobile in my own Xamarin.Forms project. Where I managed to display the QRCode for both iOS and Android with the next C# code:

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;
}

Please note that there is a know issue in this library, and you have to set explicitly the WidthRequest & HeightRequest.

P.S.: More or less the same issue have been discussed here as well.

EvZ
  • 11,889
  • 4
  • 38
  • 76