7 Pages and navigation

 

This chapter covers

  • How to break your app up into pages using the ContentPage base class
  • How to use the navigation paradigms supported by .NET MAUI to navigate between different pages in your app
  • Using Shell to simplify organizing the pages in your app
  • Passing data between pages when navigating

So far, we’ve built a handful of apps, all using a single page. A single page has worked well for the sample apps we’ve been building and works equally well for several commercial apps in the real world. But often, you need more than one page, for example, to distinguish different areas of an app or logically group functionality. Additional pages become especially necessary with nontrivial apps when they grow too much to cram everything into a single screen. In this chapter, we’ll look at navigation paradigms and the different ways that .NET MAUI supports providing multiple pages in your apps and navigating between them.

7.1 ContentPage

ContentPage is the most important page type. Other page types are just containers that provide different navigation paradigms that offer a way to present a ContentPage.

ContentPage has a single public property of type View called Content, which is what the page will render on screen. In XAML, the Content property is assigned by placing the XAML element between the opening and closing <ContentPage...> ...</ContentPage> tags, as in listing 7.1. In C#, you can assign it just like any other property, as in listing 7.2.

7.2 Common page properties

7.2.1 IconImageSource

7.2.2 Background Images

7.2.3 Padding

7.2.4 Title

7.2.5 MenuBarItems

7.3 Common page lifecycle methods

7.3.1 OnAppearing

7.3.2 OnDisappearing

7.3.3 OnNavigatedTo and OnNavigatedFrom

7.3.4 OnSizeAllocated

7.3.5 BackButtonPressed

sitemap