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

时间:2014-07-08 00:04:44

标签: navigation xamarin xamarin.android 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,请使用a   NavigationPage

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

9 个答案:

答案 0 :(得分:92)

您正在呼叫" PushAsync"

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

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

但你没有启动NavigationPage,这通常在App.cs类中完成,或者至少应该在调用&#34; PushAsync&#34; 之前启动它:< / p>

MainPage = new NavigationPage(new PageA());

答案 1 :(得分:22)

在app.xaml.cs文件中,

替换

 MainPage = new <namespace>.MainPage();

使用

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

然后使用

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

答案 2 :(得分:13)

您需要将LoginPage包含在NavigationPage中。这将解决您的错误,但会让您在导航堆栈中包含LoginPage。

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

答案 3 :(得分:3)

我只使用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());
    }
}

答案 4 :(得分:2)

  

解决了我那样的问题...首先在“主应用程序页面”中进行设置,然后在“内容页面”中进行设置以进入其他页面。

enter image description here

答案 5 :(得分:1)

验证上一页没有使用PushModalAsync。 如果以后使用PushAsync,则会出现错误“ Android上不全局支持PushAsync,请使用NavigationPage。”

答案 6 :(得分:1)

检查是否在上一个导航中使用了NavigationPage:

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

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

答案 7 :(得分:1)

添加时(在“ 公共局部类应用”中):

public App()
{
    InitializeComponent();

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

您可以使用:

等待Navigation.PushAsync(new BleBleBle());

答案 8 :(得分:0)

我在混合Rg.Plugins.Popup和ZXin.Net.Mobile扫描程序时遇到了一个问题。

在弹出窗口中调用扫描仪会触发此相同错误。 PushModalAsync解决了该错误,但是弹出窗口位于扫描范围内,因此简单的解决方案是使弹出窗口不可见,直到打开扫描仪为止。

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