Here's what I'm trying to do, when I launch the application I check to see if the user is authenticated, if not I present the Login Page using Navigation.PushModalAsync(new LoginPage());. This works. But I have two buttons on this page, along with the username and password fields. One button if to Login, and the other is to Register.
The Login button works. It simply pops off the page modally if the user authenticates and displays the main page. But when I click the Register button I get the error: "PushAsync is not supported globally on iOS, use a Navigation page".
Here's my App code:
public class App : Application
{
public App()
{
MainPage = new NavigationPage(new RootPage());
}
}
Here's my RootPage() class. This class contains a lot more code... I removed it since it had no impact on this discussion.
`public class RootPage : MasterDetailPage
{
protected async override void OnAppearing()
{
base.OnAppearing();
if (LoginViewModel.ShouldShowLogin(App.LastUseTime))
await Navigation.PushModalAsync(new LoginPage());
}
}`
This is the LoginPage() class that won't let me go any further. The error is caused by the code in the registerButton_Clicked event handler.
`public class LoginPage : ContentPage
{
#region ctors
public LoginPage()
{
BindingContext = Model;
BindPage();
}
#endregion
#region Private Methods
void BindPage()
{
var layout = new StackLayout { Padding = new Thickness(10, 100, 10, 10) };
/************* Set Branding Image *************/
var image = new Image
{
Source = "Icon-60@3x.png",
VerticalOptions = LayoutOptions.Center
};
layout.Children.Add(image);
/************* Set Login text boxes *************/
var userName = new Entry { Placeholder = "Username" };
userName.SetBinding(Entry.TextProperty, LoginViewModel.UserNamePropertyName);
layout.Children.Add(userName);
var password = new Entry { Placeholder = "Password", IsPassword = true };
password.SetBinding(Entry.TextProperty, LoginViewModel.PasswordPropertyName);
layout.Children.Add(password);
/************* Set Buttons *************/
var buttonLayout = new StackLayout();
buttonLayout.Spacing = 10;
buttonLayout.Orientation = StackOrientation.Horizontal;
var loginButton = new Button
{
Text = "Login",
BackgroundColor = Color.Navy,
HorizontalOptions = LayoutOptions.FillAndExpand,
TextColor = Color.White
};
loginButton.Clicked += loginButton_Clicked;
buttonLayout.Children.Add(loginButton);
var registerButton = new Button
{
Text = "Register",
BackgroundColor = Color.Maroon,
HorizontalOptions = LayoutOptions.FillAndExpand,
TextColor = Color.White
};
registerButton.Clicked += registerButton_Clicked;
buttonLayout.Children.Add(registerButton);
layout.Children.Add(buttonLayout);
var labelMessage = new Label();
labelMessage.SetBinding(Label.TextProperty, LoginViewModel.MessagePropertyName);
layout.Children.Add(labelMessage);
Content = new ScrollView { Content = layout };
}
#endregion
#region Event Handlers
async void registerButton_Clicked(object sender, EventArgs e)
{
await Navigation.PopModalAsync(); // This works...
await Navigation.PushAsync(new AgreeToTermsPage()); // This doesn't
}
void loginButton_Clicked(object sender, EventArgs e)
{
if (_loginViewModel.CanLogin)
{
_loginViewModel.LoginAsync(System.Threading.CancellationToken.None)
.ContinueWith(t =>
{
App.LastUseTime = DateTime.UtcNow;
Navigation.PopAsync();
});
Navigation.PopModalAsync();
}
else
{
DisplayAlert("Login Error", "Invalid credentials", "OK");
}
}
#endregion
#region Properties
private LoginViewModel _loginViewModel;
private LoginViewModel Model
{
get
{
if (_loginViewModel == null)
_loginViewModel = new LoginViewModel(App.AuthService);
return _loginViewModel;
}
}
#endregion
}
}`
I would really like to know what's happening here and why I can't make this work. What are the rules that I'm missing? I've looked at all the documentation I can get my hands on and also the Forms Gallery project, and while the Navigation Sample works, mine doesn't, so I'm obviously missing something. Any help is appreciated.
Thanks,
King Wilder