在 Android 上不支持 PushAsync,请使用 NavigationPage-Xamarin. Forms

我有以下方法在一个 Xamarin.Forms.ContentPage连接到一个按钮点击事件

public class LoginPage : ContentPage
{
private Button _loginButton = null;
private Entry _PasswordInput = null;
private Entry _UsernameInput = null;


public LoginPage()
{
_UsernameInput = new Entry { Placeholder = "Username" };
_PasswordInput = new Entry { Placeholder = "Password", IsPassword = true };


_loginButton = new Button
{
Text = "Login",
BorderRadius = 5
}


_loginButton.Clicked += LogIn;


Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children =
{
_UsernameInput, _PasswordInput, _loginButton,
},
Spacing = 15
};
}


public async void LogIn(object sender, EventArgs eventsArgs)
{
//do authenticate stuff here
SSO.MyAuthentication client = new SSO.MyAuthentication();


bool isAuthenticated = client.Authenticate(_UsernameInput.Text, _PasswordInput.Text);


if(isAuthenticated)
{
//Push home page to top of navigation stack
Navigation.PushAsync(new HomePage());
}
}
}

在以下代码 Navigation.PushAsync(new HomePage());行中,我在调试时遇到了以下异常:

在 Android 上不支持 PushAsync,请使用 导航页

如何使用 Xamarin.Forms.NavigationPage对象解决这个问题?

95152 次浏览

You need to enclose your LoginPage in a NavigationPage. This will fix your error, but will leave you with the LoginPage contained on your navigation stack.

An alternate approach would be to make your HomePage the root of the application, then display the LoginPage modally on top of it. Only when the user successfully logs in do you dismiss the LoginPage modal so they can see the HomePage.

You are calling "PushAsync":

public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}


private void btnCourseList_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new PageB());
}
}

but you did not start the NavigationPage, which normally is done in the App.cs class, or at least it should be started before any call to "PushAsync":

MainPage = new NavigationPage(new PageA());

In app.xaml.cs file,

Replace

 MainPage = new <namespace>.MainPage();

With

 MainPage = new NavigationPage(new <namespace>.MainPage());

Then Use

 await Navigation.PushAsync(new NavigationPage(new MainPage2()));

I only change pushAsync with pushModalAsync :)

public async void LogIn(object sender, EventArgs eventsArgs)
{
//do authenticate stuff here
SSO.MyAuthentication client = new SSO.MyAuthentication();


bool isAuthenticated = client.Authenticate(_UsernameInput.Text, _PasswordInput.Text);


if(isAuthenticated)
{
//Push home page to top of navigation stack
//Navigation.PushAsync(new HomePage());
Navigation.PushModalAsync(new HomePage());
}
}

I got one problem mixing Rg.Plugins.Popup and ZXin.Net.Mobile Scanner.

Calling the scanner inside a popup was triggering this same error. PushModalAsync solved the error, but the popup was over the scan so easy solution was make the popup invisible until the scanner was on.

    private async void FrmQrCode_Tapped(object sender, EventArgs e)
{
ZXingScannerPage scanPage = new ZXingScannerPage();
scanPage.OnScanResult += (result) =>
{
scanPage.IsScanning = false;
ZXing.BarcodeFormat barcodeFormat = result.BarcodeFormat;
string type = barcodeFormat.ToString();
Device.BeginInvokeOnMainThread(() =>
{
Navigation.PopModalAsync();


this.IsVisible = true;


Token = result.Text.Trim();
});
};
this.IsVisible = false;
await Navigation.PushModalAsync(scanPage);
}

First make setting in "Main App Page" then do in "Content page" to go other page:

enter image description here

Verify that the previous page is not using a PushModalAsync. If later you use a PushAsync, you will have the error "PushAsync is not supported globally on Android, please use a NavigationPage."

Check that in the previous navigation you use NavigationPage:

Incorrect: Application.Current.MainPage = new LoginPage();

Correct: Application.Current.MainPage = new NavigationPage(new LoginPage());

When you add (in "public partial class App"):

public App()
{
InitializeComponent();


MainPage = new NavigationPage(new MainPage());
}

You can use:

await Navigation.PushAsync(new BleBleBle());

As you usually don't want the Login to be part of the page stack or for the user to be able to return after having authenticated correctly, you can do the following:

Inside the method that is executed when pressing login button

Application.Current.MainPage = new NavigationPage (new HomePage ());

This will take as the main screen of the NavigationPage your main screen and not the LoginPage. Also the user will not be able to return to the login unless you place a button to log out.