在Xamarin Forms应用程序中使用ZXing Mobile
问题描述:
当前正试图使用ZXing Mobile为Xamarin Forms构建应用程序。在Xamarin Forms应用程序中使用ZXing Mobile
编译的代码没有问题。但是,试图将Android设备上运行的时候,我得到了以下错误:
An unhandled exception occcured.
日志显示以下内容:
[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'
我这是怎么实现的:
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");
});
};
};
还增加以下是Android的主要活动,位于Xamarin自己的Init上方:
ZXing.Net.Mobile.Forms.Android.Platform.Init();
任何帮助表示赞赏。谢谢。
答
我试过了你的代码,发现这个错误与导航更相关。
您正在使用
await Navigation.PushAsync (scanPage);
确保您所呼叫从其中包含NavigationPage如果不是,你将有崩溃的网页此方法。
为了解决这个问题,你可以使用
await Navigation.PushModalAsync (scanPage);
代替,这不需要NavigationPage和显示的将是模态的结果页面。使用上面的代码你还需要改变你“弹出”视图的方式。
await Navigation.PopModalAsync (true);
注:
工作,即使你会出现在日志中这些消息。
[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'
希望这helps.-
+0
谢谢。不知道错误是关于缺少'NavigationPage'。不知道为什么错误信息并不意味着.. – ipohfly
什么版本的斑马线您使用的是? Nuget或组件?你是否也要求获得相机的许可? – apineda
该版本是2.2.9。通过Nuget安装。是的,权限被添加到'AndroidManifest.xml'上。 – ipohfly