简体   繁体   English

在Xamarin Forms应用程序中使用ZXing Mobile

[英]Using ZXing Mobile in Xamarin Forms app

currently trying to build an app using ZXing Mobile for Xamarin Forms. 目前正在尝试使用ZXing Mobile for Xamarin Forms构建应用程序。

The code compiled without issue. 该代码编译没有问题。 But when trying to run on Android device, I got the following error: 但是,当尝试在Android设备上运行时,出现以下错误:

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 : 还在Xamarin自己的Init上方的Android主要活动中添加了以下内容:

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. 确保从包含NavigationPage的页面调用此方法,否则将崩溃。

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. 相反,它不需要NavigationPage,显示的结果页面将是模式页面。 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.- 希望这可以帮助。-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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