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

Mic*_*ern 57 navigation xamarin.android xamarin 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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Android上不支持全局支持PushAsync,请使用NavigationPage

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

小智 105

您正在调用"PushAsync":

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

    private void btnCourseList_Clicked(object sender, EventArgs e)
    {
        Navigation.PushAsync(new PageB());
    }
}
Run Code Online (Sandbox Code Playgroud)

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

MainPage = new NavigationPage(new PageA());
Run Code Online (Sandbox Code Playgroud)

  • 有人应该制作一个 IDE,将错误警告与 StackOverflow 投票最高的答案配对 (2认同)

Ama*_*hur 30

在app.xaml.cs文件中,

更换

 MainPage = new <namespace>.MainPage();
Run Code Online (Sandbox Code Playgroud)

 MainPage = new NavigationPage(new <namespace>.MainPage());
Run Code Online (Sandbox Code Playgroud)

然后使用

 await Navigation.PushAsync(new NavigationPage(new MainPage2()));
Run Code Online (Sandbox Code Playgroud)

  • @OlaStröm 和其他想要删除主页导航栏的人,请转到 xaml 并在 ContentPage 标记中添加 `NavigationPage.HasNavigationBar="False"` (3认同)
  • 这搞乱了设计,因为它在页面顶部添加了一个导航栏。 (2认同)

Jas*_*son 14

您需要将LoginPage包含在NavigationPage中.这将修复您的错误,但会让您在导航堆栈中包含LoginPage.

另一种方法是将HomePage作为应用程序的根目录,然后在其上以模态方式显示LoginPage.只有当用户成功登录时才会关闭LoginPage模式,以便他们可以看到HomePage.


Aya*_*ifa 8

首先在“主应用程序页面”中进行设置,然后在“内容页面”中进行设置以转到其他页面:

在此处输入图片说明

  • 这搞乱了设计,因为它在页面顶部添加了一个导航栏。 (3认同)

小智 5

我只用pushModalAsync更改pushAsync :)

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());
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

检查您在之前的导航中是否使用了 NavigationPage:

错误:Application.Current.MainPage = new LoginPage();

正确的:Application.Current.MainPage = new NavigationPage(new LoginPage());