我正在尝试在具有xamarin形式的andorid设备上使用Zxing代码扫描程序。 创建了一个按钮来打开Zxing扫描仪页面。
然而,当我这样写的时候。它发给我一个unhandel exceptoin: 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,请使用NavigationPage。
附上我的代码:
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);
}
}
如果我将Navagation.push异步更改为PushModalAsync,如下所示: Navigation.PushModalAsync(scanPage);
我移动到Zxing扫描仪页面,但它没有开始扫描。 (crusior事件不会动。
我的主要Andorid活动如下:
[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);
}
}
感谢任何帮助。
答案 0 :(得分:2)
您缺少App.cs上的导航:
MainPage = new NavigationPage(new YourFirstPage());
而不仅仅是MainPage = new YourFirstPage();