简体   繁体   中英

Using ZXing Mobile in Xamarin Forms app

currently trying to build an app using ZXing Mobile for Xamarin Forms.

The code compiled without issue. But when trying to run on Android device, I got the following error:

An unhandled exception occcured.

The log shows the following:

[0:] Binding: 'DefaultOverlayTopText' property not found on 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay', target property: 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay.TopText'
[0:] Binding: 'DefaultOverlayBottomText' property not found on 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay', target property: 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay.BottomText'
[0:] Binding: 'DefaultOverlayShowFlashButton' property not found on 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay', target property: 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay.ShowFlashButton'
[0:] Binding: 'DefaultOverlayTopText' property not found on 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay', target property: 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay.TopText'
[0:] Binding: 'DefaultOverlayBottomText' property not found on 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay', target property: 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay.BottomText'
[0:] Binding: 'DefaultOverlayShowFlashButton' property not found on 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay', target property: 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay.ShowFlashButton'

This is how i implemented:

scanButton.Clicked += async (sender, args) =>
            {
                var options = new MobileBarcodeScanningOptions
                {
                    AutoRotate = false,
                    UseFrontCameraIfAvailable = false,
                    TryHarder = true
                };
                var scanPage = new ZXingScannerPage(options)
                {
                    DefaultOverlayTopText = "Align the barcode within the frame",
                    DefaultOverlayBottomText = string.Empty,
                    DefaultOverlayShowFlashButton = true
                };
                // Navigate to our scanner page
                await Navigation.PushAsync(scanPage);

                scanPage.OnScanResult += (result) =>
                {
                    // Stop scanning
                    scanPage.IsScanning = false;

                    // Pop the page and show the result
                    Device.BeginInvokeOnMainThread(async () =>
                    {
                        await Navigation.PopAsync();
                        await DisplayAlert("Scanned Barcode", result.Text, "OK");
                    });
                };
            };  

Also added the following in Android's Main activity, right above Xamarin's own Init :

ZXing.Net.Mobile.Forms.Android.Platform.Init();

Any help is appreciated. Thank you.

I tried your code and found the error was more related to the navigation.

You are using

await Navigation.PushAsync (scanPage);

Make sure you are calling this method from a page which contains a NavigationPage if not you will have a crash.

To fix this you can use

await Navigation.PushModalAsync (scanPage);

instead, which doesn't require a NavigationPage and the resulting page displayed will be modal. Using the line above you will need to also change the way you "pop" the view.

await Navigation.PopModalAsync (true);

Note:

These messages you will appear in the logs even when working.

[0:] Binding: 'DefaultOverlayTopText' property not found on 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay', target property: 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay.TopText'
[0:] Binding: 'DefaultOverlayBottomText' property not found on 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay', target property: 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay.BottomText'
[0:] Binding: 'DefaultOverlayShowFlashButton' property not found on 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay', target property: 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay.ShowFlashButton'
[0:] Binding: 'DefaultOverlayTopText' property not found on 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay', target property: 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay.TopText'
[0:] Binding: 'DefaultOverlayBottomText' property not found on 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay', target property: 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay.BottomText'
[0:] Binding: 'DefaultOverlayShowFlashButton' property not found on 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay', target property: 'ZXing.Net.Mobile.Forms.ZXingDefaultOverlay.ShowFlashButton'

Hope this helps.-

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM