我在 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,请使用NavigationPage
如何使用 Xamarin.Forms.NavigationPage
对象解决此问题?
4 回答
你正在调用 "PushAsync" :
但是你没有启动NavigationPage,它通常在App.cs类中完成,或者至少应该在调用 "PushAsync" 之前启动它:
在app.xaml.cs文件中,
更换
同
然后使用
您需要将LoginPage包含在NavigationPage中 . 这将修复您的错误,但会让您在导航堆栈中包含LoginPage .
另一种方法是将HomePage作为应用程序的根目录,然后在其上以模态方式显示LoginPage . 只有当用户成功登录时才会关闭LoginPage模式,以便他们可以看到HomePage .
我只用pushModalAsync改变pushAsync :)