djklasj dsajdkla
djklasj dsajdkla

Reputation: 81

Zxing bar code scanner on Android device using Xamarin.Forms

I am trying to use the Zxing code scanner on an andorid device with xamarin forms. created a button to open the Zxing scanner page.

however when I am writed like this. it sends to me an 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);
    }
}

however if I move it to synchronize way I get this error:

System.InvalidOperationException: PushAsync is not supported globally on Android, please use a NavigationPage.

attaching my code:

     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);
    }
}

and if I change the Navagation.push async to PushModalAsync like this: Navigation.PushModalAsync(scanPage);

I move to the Zxing scanner page however it does not start to scan. (crusior event don't move.

my main Andorid activity looks like this:

[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);
    }
}

appericate any help thanks.

Upvotes: 1

Views: 958

Answers (1)

Luis Beltran
Luis Beltran

Reputation: 1704

You are missing the Navigation on App.cs:

MainPage = new NavigationPage(new YourFirstPage());

Instead of simply MainPage = new YourFirstPage();

Upvotes: 2

Related Questions