Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
658 views
in Technique[技术] by (71.8m points)

navigation - PushAsync is not supported globally on Android, please use a NavigationPage - Xamarin.Forms

I have the following method in an Xamarin.Forms.ContentPage wired to a button click event

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

On the following line of code Navigation.PushAsync(new HomePage());, I am getting the following exception while debugging:

PushAsync is not supported globally on Android, please use a NavigationPage

How do I resolve this issue using a Xamarin.Forms.NavigationPage object?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...