Pages

The Page class is a visual element that occupies part or all of a screen and contains a single child. A Xamarin.Forms.Page represents an Activity in Android, a View Controller in iOS, or a Page in Windows Phone.

Pages

ContentPage

These page are created when a user clicks on our menu items.

They display a single view that often contains a StackLayout or ScrollView, which we will analyze shortly.

To define a content page, we need to use the following line of code:

using Xamarin.Forms; 

Then inherit from ContentPage:

public class MonkeyContractsPage : ContentPage 
{ 
  public MonkeyContractsPage (){} 
} 
  
public class MonkeyLeadsPage : ContentPage 
{ 
  public MonkeyLeadsPage (){} 
} 
  
public class MonkeyAccountsPage : ContentPage 
{ 
  public MonkeyAccountsPage (){} 
} 
  
public class MonkeyOpportunitiesPage : ContentPage 
{ 
  public MonkeyOpportunitiesPage (){} 
} 

The appearances on the three platforms are basically the same since this is a simple page with content.

MasterDetailPage

MasterDetailPage manages two panes of information:

  • A master page that presents data at a high level
  • A detail page that displays low-level details about information in the master

MasterDetailPage

Think of MasterDetailPage as the remote controller of our application navigation. When a user clicks on the menu icon, MasterDetail pushes the menu onto the screen. Then, when the user taps on a menu item, MasterDetail creates the target page and pushes it onto the stack.

To create it, we need to define the following:

  • Menu page
  • Menu items
  • Content pages
  • Menu list data
  • Menu list view
  • Root page

Menu page

The menu page is a Xamarin.Forms page that will be shown when the user clicks the menu button.

In this example, our MenuPage is a simple content page with a StackLayout containing a label and a ListView of our menu items:

public class MonkeyMenuPage : ContentPage 
{ 
  public ListView MonkeyMenu { get; set; } 
  
  public MonkeyMenuPage () 
  { 
    Icon = "ourMenuButton.png"; 
    Title = "menu"; // The Title property must be set. If we don't the
    application will throw an error 
 
    MonkeyMenu = new MenuListView (); 
  
    var menuLabel = new ContentView { 
      Content = new Label { 
        Text = "MENU"  
      } 
    }; 
  
    var layout = new StackLayout {  
      Spacing = 0,  
      VerticalOptions = LayoutOptions.FillAndExpand 
    }; 
 
    layout.Children.Add (menuLabel); 
    layout.Children.Add (MonkeyMenu); 
  
    Content = layout; 
  } 
} 

Tip

The public property MonkeyMenu is a ListView that allows our MasterDetail page access to the ItemSelected event. When the event is fired, the item selected will be sent to the NavigateTo method on the master detail object.

Menu item

Menu items will contain our menu data. The most important data type is Target.

When a user clicks on a menu item, the NavigateTo method will create a new page from TargetType and push that page onto the stack:

 
    public class MonkeyMenuItem 
    { 
        public string Title { get; set; } 
 
        public string IconSource { get; set; } 
 
        public Type TargetType { get; set; } 
    } 

Content pages

We can create three pages, which we will call Species, History, and Settings, where we can put the content we want to see in the detail page.

If we right-click on the project and click on the New File menu item, the New File dialog will open. We can select Cross-Platform on the left-hand list and select Forms ContentPage on the right-hand list and name it as SpeciesPage, shown as follows:

Content pages

This will create a hello world page for us to show a simple message on screen.

The SpeciesPage will appear like this:

using Xamarin.Forms; 
 
namespace XBE 
{ 
    public class SpeciesPage : ContentPage 
    { 
        public SpeciesPage  () 
        { 
            Content = new StackLayout {  
                Children = { 
                    new Label { Text = "Hello ContentPage" } 
                } 
            }; 
        } 
    } 
}  

You may also add HistoryPage and SettingsPage and continue to the next step.

MenuListData

The MenuListData class is the data structure that contains our application's navigation.

We can get this data from the database or configuration file.

The menu structure of this example is done by adding the code below to the list of pages we want to show:

public class MonkeyMenuListData : List<MonkeyMenuItem> 
    { 
        public MonkeyMenuListData  () 
        { 
            this.Add (new MonkeyMenuItem () {  
                Title = "Species",  
                IconSource = "species.png",  
                TargetType = typeof(SpeciesPage) 
            }); 
 
            this.Add (new MenuItem () {  
                Title = "History",  
                IconSource = "history.png",  
                TargetType = typeof(HistoryPage) 
            }); 
 
            this.Add (new MenuItem () {  
                Title = "Settings",  
                IconSource = "settings.png",  
                TargetType = typeof(SettingsPage) 
            }); 
        } 
    } 

Menu List View

The menu list models the ListView defining the appearance of the cells:

public class DrawerMenuListView : ListView 
{ 
  public MonkeyMenuListView () 
        { 
            List<MonkeyMenuItem> data = new MonkeyMenuListData (); 
 
            ItemsSource = data; 
            VerticalOptions = LayoutOptions.FillAndExpand; 
            BackgroundColor = Color.Transparent; 
 
            var cell = new DataTemplate (typeof(ImageCell)); 
            cell.SetBinding (TextCell.TextProperty, "Title"); 
        }  
} 

Root page

The root page needs to inherit from MasterDetailPage and set in the constructor the initialization of the view. We can set the instance of MonkeyMenu as Master and the first page we want to show (SpeciesPage) as Detail:

using Xamarin.Forms; 
 
namespace XBE 
{ 
    public class RootPage : MasterDetailPage 
    { 
        public RootPage () 
        { 
            var menuPage = new MonkeyMenu (); 
 
            menuPage.Menu.ItemSelected += (sender, e) =>
            NavigateTo (e.SelectedItem as MonkeyMenuItem); 
 
            Master = menuPage; 
            Detail = new NavigationPage (new SpeciesPage ()); 
        } 
 
        void NavigateTo (MonkeyMenuItem menu) 
        { 
            Page displayPage = (Page)Activator.CreateInstance
            (menu.TargetType); 
 
            Detail = new NavigationPage (displayPage); 
 
            IsPresented = false; 
        } 
    } 
} 

If we set the root page as the main page in the application file, we can already deploy it on our cross platform devices.

The constructor of our app should now appear like this:

public App () 
{ 
     // The root page of your application 
     MainPage = new RootPage (); 
} 

NavigationPage

NavigationPage is a page that manages users' navigation in a stack. A navigation page implements the interface INavigation.

With access to the INavigation interface, our page can do things like push and pop new pages on the stack, push and pop modal pages, reshuffle the stack of pages, or remove everything from the stack except the root page:

public interface INavigation  
{      
Task PushAsync(Page page);      
Task<Page> PopAsync();      
Task PopToRootAsync();      
Task PushModalAsync(Page page);      
Task<Page> PopModalAsync();  
} 

To start the navigation stack, we have to pass a ContentPage into the NavigationPage constructor.

The first page added to NavigationPage is called the rootPage of the navigation stack:

ContentPage rootPage = new ContentPage(); 
var myNavigationPage = new NavigationPage( rootPage ); 

It is very important to construct the NavigationPage with the root page, and we cannot forget this step otherwise the app will crash as soon as the NavigationPage is called.

Push and Pop

We can use the push and pop  methods of the INavigation interface to navigate between different pages. Calling those methods creates a stack of pages. When we call the push methods, a page will be added on top of this stack making the stack bigger. It's very important to pop in order to remove the page from the stack when we want to go back to other pages. On the navigation pages, the back button will pop the current page from the stack revealing the page beneath it.

When we pop all the pages and we arrive in the root page, the back button will not be available and the pop methods cannot be used. In the INavigation interface, there is also a pop to root method that will remove all the pages of the stack except the root page.

Tip

Both pop and push methods to the navigation stack is made asynchronously. Asynchronous calls (such as PushAsync, PopAsync, and so on) run without blocking the CPU until they finish. This makes application runs fluent, even if the job takes a long time to complete. The CPU may continue to execute other tasks while asynchronous tasks are running. Typically, the CPU continuously executes tasks, but some tasks takes longer to finish and the CPU starts to wait for them. In this situation, the CPU does nothing and the device spends precious CPU cycles for nothing. With the asynchronous approach, the CPU has to wait for a task's finish signal before it starts to execute other tasks.

TabbedPage

TabbedPage displays an array of tabs, each of which loads content onto the screen.

The default appearance of the tab is platform-specific. If we want to customize them, we need to write our own custom renderer. We will see how to customize the controls using custom renderers in Chapter 6Custom Renderers .

To create TabbedPage, we need to prepare the content pages to add to it.

When we have them, we can add a new C# file called, for example, TabbedRootPage, and inherit from TabbedPage. On the constructor we can now add to the Children property the pages we want, and they will be reachable by the user by just touching the title of the page in the TabBar on the screen:

public class TabbedRootPage : TabbedPage 
{ 
   public TabbedRootPage () 
   { 
     SendPageMenu sendPage = new SendPageMenu(); 
     LivePageMenu livePage = new LivePageMenu (); 
     InboxPageMenu inboxPage = new InboxPageMenu (); 
     InvitePageMenu invitePage = new InvitePageMenu (); 
             
     this.Children.Add (sendPage); 
     this.Children.Add (livePage); 
     this.Children.Add (inboxPage);  
     this.Children.Add (invitePage); 
   } 
} 

TabbedPage

In order to work properly with TabbedPage, it is mandatory to give a value to the Title property of each content page. We can also assign the Icon property if we want to show it. The Icon property is only used by default in the iOS version of the app:

public SendPageMenu () 
        { 
            Title = "Send"; 
            Icon = Icon = "menu_send.png"; 
        }

CarouselPage

CarouselPage displays an array of pages, each of which loads content onto the screen and allows the final user to swipe from side to side to navigate between them.

The use of it is exactly like TabbedPage, with the only difference being that we need to inherit from CarouselPage. Like we did for TabbedPage, we need to prepare the content pages to add to CarouselPage.

When we have them, we can add a new C# file called, for example, CarouselRootPage, and inherit from CarouselPage. On the constructor we can now add the pages we want to the Children property, and they will be reachable by the user by just swiping between pages:

public class CarouselRootPage : CarouselPage  
{ 
   public CarouselRootPage  () 
   { 
     SendPageMenu sendPage = new SendPageMenu(); 
     LivePageMenu livePage = new LivePageMenu (); 
     InboxPageMenu inboxPage = new InboxPageMenu (); 
     InvitePageMenu invitePage = new InvitePageMenu (); 
             
     this.Children.Add (sendPage); 
     this.Children.Add (livePage); 
     this.Children.Add (inboxPage);  
     this.Children.Add (invitePage); 
   } 
} 

Also, in this case it is mandatory to give a value to the Title property of each content page.

Tip

When embedding a CarouselPage into a MasterDetailPage.Detail application, we should set MasterDetailPage.IsGestureEnabled to false to prevent gesture conflicts between the CarouselPage and MasterDetailPage.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.0.61