本文介绍了Android 全球不支持 PushAsync,请使用 NavigationPage - Xamarin.Forms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在连接到按钮单击事件的 Xamarin.Forms.ContentPage 中有以下方法

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

在以下代码行Navigation.PushAsync(new HomePage());,调试时出现以下异常:

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

Android 全球不支持 PushAsync,请使用导航页

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

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

推荐答案

你在调用"PushAsync":

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

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

但您没有启动 NavigationPage,这通常在 App.cs 类中完成,或者至少应该在任何调用 "PushAsync" 之前启动它:

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

这篇关于Android 全球不支持 PushAsync,请使用 NavigationPage - Xamarin.Forms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 16:55