I have implemented a Xamarin.Forms App focused on Android which should scan barcodes on different pages. (Followed this description ‚https://blog.xamarin.com/barcode-scanning-made-easy-with-zxing-net-for-xamarin-forms/‘ (Thanks! James Montemagno))
I initialize the scanner control (defined in xaml) in the constructor of the codebehind class:
<ContentPage.Content> <AbsoluteLayout> <AbsoluteLayout.Children> <z:ZXingScannerView x:Name="zScanner" AbsoluteLayout.LayoutBounds="0.5,0.5,1,1" AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" IsScanning="True" OnScanResult="ZScanner_OnOnScanResult" VerticalOptions="FillAndExpand" /> <z:ZXingDefaultOverlay x:Name="zOverlay" ShowFlashButton="False" AbsoluteLayout.LayoutBounds="0.5,0.5,1,1" AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" /> ….
For embedding into the own page layout I use the ZXingScannerView-Control.
public TicketScan()
{
InitializeComponent();
// https://components.xamarin.com/gettingstarted/zxing.net.mobile.forms
// https://blog.xamarin.com/barcode-scanning-made-easy-with-zxing-net-for-xamarin-forms/
var zXingOptions = new MobileBarcodeScanningOptions()
{
DelayBetweenContinuousScans = 1000, // msec
UseFrontCameraIfAvailable = false,
PossibleFormats = new List<BarcodeFormat>(new[]
{
BarcodeFormat.EAN_8,
BarcodeFormat.EAN_13,
BarcodeFormat.CODE_128,
BarcodeFormat.QR_CODE
}),
TryHarder = true //Gets or sets a flag which cause a deeper look into the bitmap.
};
zScanner.Options = zXingOptions;
// https://github.com/Redth/ZXing.Net.Mobile/issues/427
zOverlay.BindingContext = zOverlay;
fab.Clicked = OnClick_AddFabButton;
}
The first scan page works as expected.
When I leave the page I pause the scanner here:
protected override void OnDisappearing()
{
base.OnDisappearing();
zScanner.IsScanning = false;
}
(Otherwise the scanner will scan on each further page even if no scanner control is on the page (?!) And the camera works all the time.)
On a second page I try to initialize the scanner on the same way and set IsScanning = true.
protected override void OnAppearing()
{
zScanner.IsScanning = true;
base.OnAppearing();
}
But on this second page I see only a frozen camera picture (maybe the last shot from the first usage)
How can I use the ZXing scanner component for Xamarin.Forms on different pages without turned on camera for the whole camera usage?
thx