Lecture 3 Multiple Pages
MainPage = new GreetingsPage(); } In the previous App we set MainPage to a member of the GreetingsPage class. If we wish to display multiple pages in our App we can set public App() { MainPage = new GreetingsPage(); } public App() { // The root page of your application MainPage = new NavigationPage(new ButtonPage()); }
Create two or more content page classes…. e.g. buttonPage and another Page as shown here
1. Right-Click Project 2. Select Add -> New Item…
3. Choose Visual C# -> Cross-Platform 4. Highlight Forms ContentPage 5. Name this content Page class 6. Add to Project
using System; using Xamarin.Forms; namespace NowApp { public class anotherPage : ContentPage Label label1 = new Label Text = "This is Another Page", FontSize = 30, FontAttributes = FontAttributes.Bold, HorizontalOptions = LayoutOptions.Center }; public anotherPage() Content = new StackLayout Children = label1 }
public class ButtonPage : ContentPage public ButtonPage() using System; using Xamarin.Forms; namespace NowApp { public class ButtonPage : ContentPage public ButtonPage() Button button = new Button Text = "Now", BorderWidth = 1, HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.CenterAndExpand }; button.Clicked += OnButtonClicked; this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5); this.Content = new StackLayout Children = button } void OnButtonClicked(object sender, EventArgs e) Navigation.PushAsync(new anotherPage()); PushAsync permits the use of “back” to return to the Previous page… What are the alternatives to PushAsync?