我试图在具有xamarin形式的Android设备上使用Zxing代码扫描程序。创建了一个按钮来打开Zxing扫描仪页面。
但是当我这样写的时候。它向我发送了一个未处理的异常:
private void BtnPress_Clicked(object sender, EventArgs e)
{
goToScan();
}
private async void goToScan()
{
var scanPage = new ZXingScannerPage();
scanPage.OnScanResult += (result) => {
// Stop scanning
scanPage.IsScanning = false;
// Pop the page and show the result
Device.BeginInvokeOnMainThread(() => {
Navigation.PopAsync();
DisplayAlert("Scanned Barcode", result.Text, "OK");
});
};
// Navigate to our scanner page
await Navigation.PushAsync(scanPage);
}
}
但是,如果我将其移动到同步方式,我会收到此错误:
System.InvalidOperationException:Android上不支持全局支持PushAsync,请使用导航页面。
附上我的代码:
private void goToScan()
{
var scanPage = new ZXingScannerPage();
scanPage.OnScanResult += (result) => {
// Stop scanning
scanPage.IsScanning = false;
// Pop the page and show the result
Device.BeginInvokeOnMainThread(() => {
Navigation.PopAsync();
DisplayAlert("Scanned Barcode", result.Text, "OK");
});
};
// Navigate to our scanner page
Navigation.PushAsync(scanPage);
}
}
如果我将Navigation.pushasync更改为PushModalAsync,如下所示:Navigation.PushModalAsync(scan Page);
我移动到Zxing扫描仪页面,但它没有开始扫描。 (crusior事件不动。
我的主要Android活动如下所示:
[Activity(Label = "QRCode4", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
ZXing.Net.Mobile.Forms.Android.Platform.Init();
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
global::ZXing.Net.Mobile.Forms.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
感谢任何帮助谢谢。
你错过了App.cs上的导航:
MainPage = new NavigationPage(new YourFirstPage());
而不是简单的MainPage = new YourFirstPage();