Issue
I have a page with ScannerView as MainPage. When I start the app it can't scan a barcode. I have to set another page as mainpage then navigate to the scanner page, before it can scan a barcode. Or lock and then unlock the phone, then it will start scanning.
App.xaml.cs:
MainPage = new NavigationPage(new ScannerPage());
ScannerPage.xaml:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.Pages.ScannerPage"
xmlns:zxing="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms">
<ContentPage.Content>
<Grid>
<zxing:ZXingScannerView x:Name="ScannerView"
IsScanning="True"
IsAnalyzing="True" />
<zxing:ZXingDefaultOverlay x:Name="ScannerOverlay"
TopText="Hold your phone up to the QR code"
BottomText="Scanning will happen automatically"
ShowFlashButton="True"/>
</Grid>
</ContentPage.Content>
</ContentPage>
ScannerPage.xaml.cs:
public partial class ScannerPage : ContentPage
{
public ScannerPage ()
{
InitializeComponent ();
ScannerView.Options = new MobileBarcodeScanningOptions
{
PossibleFormats = new List<BarcodeFormat>
{
BarcodeFormat.DATA_MATRIX,
},
TryHarder = true
};
ScannerView.OnScanResult += (result) => Device.BeginInvokeOnMainThread(async () =>
{
ScannerView.IsAnalyzing = false;
await DisplayAlert("Scanned", result.Text, "Ok");
ScannerView.IsAnalyzing = true;
});
}
}
Solution
I ended up using the following code programmatically:
public static async Task ScanConnection()
{
MobileBarcodeScanningOptions options = new ZXing.Mobile.MobileBarcodeScanningOptions()
{
TryHarder = true,
PossibleFormats = new List<ZXing.BarcodeFormat>() { ZXing.BarcodeFormat.QR_CODE }
};
MobileBarcodeScanner scanner = new ZXing.Mobile.MobileBarcodeScanner();
ZXing.Result result = await scanner.Scan(options);
if (result != null && !string.IsNullOrEmpty(result.Text))
{
...
}
}
You could call that code from an overridden OnAppearing() method from within your derived page.
Answered By - Markus Michel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.