Barney Chambers
Barney Chambers

Reputation: 2783

Xamarin PushAsync method not supported

I've read this thread and a few others about similar errors, but unfortunately I still don't understand how to fix my problem.

I have a method that should open a ZXScannerPage so that I can read QR codes

protected override async void OnAppearing()
    {
        base.OnAppearing();

        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); // Here is the error            
    }

I need to use this function before my await Navigation.PushAsync(scanPage); call

MainPage = new NavigationPage(<Something goes here>);

But I am unsure where this should go, and what arguments I should feed it

Upvotes: 0

Views: 1713

Answers (2)

Mukesh Modhvadiya
Mukesh Modhvadiya

Reputation: 2178

I think you want to know how to initialize and use Navigation page functionality,

Before using PushAsync and PopAsync functionality you need to initialize a new Navigation Page with some page in your app.

MainPage = new NavigationPage(Something goes here);

You can set Navigation Page as your main page in App class using some base page in your app i.e. Login page or Welcome page

public class App : Application
{
  public App()
  {
    var nPage = new NavigationPage(new WelcomePage()); // or new LoginPage()
    MainPage = nPage;
  }
}

Now that you have initialized a Navigation Page with some basic page, you can Push or Pop other pages i.e. your scan page.

Upvotes: 0

puko
puko

Reputation: 2970

PushAsync method is not supported because MainPage of the app is not NavigationPage.

Create page in which overrides OnAppearing method. In this method use your code.

When application started in App.xaml.cs or App.cs depends on project type, call in constructor

MainPage = new NavigationPage(new YourPage());

This will call OnAppearing method in your page and your code push the scanner page up.

EDIT U can use your scannerPage like

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");
            });
        };
MainPage = new NavigationPage(scanPage);

In this case after scan is completed Navigation.PopAsync() will not work because in navigation stack is only one page (except NavigationPage).

Upvotes: 1

Related Questions