Xamarin.Forms Guides | Samples

Xamarin.Forms.Page Class

A VisualElement that occupies the entire screen.

See Also: Page

Syntax

[Xamarin.Forms.RenderWith(typeof(Xamarin.Forms.Platform._PageRenderer))]
public class Page : VisualElement, IElementConfiguration<Page>, ILayout, IPageController

Remarks

Page is primarily a base class for more useful derived types. Objects that are derived from the see Page class are most prominently used as the top level UI element in Xamarin.Forms applications. Typically, application developers will provide such an object to the target platforms by returning it from a static method that the developer created in a project that references Xamarin.Forms. The contents of a typical App.cs file that would appear in a project that reference Xamarin.Forms are shown below:

C# Example

using System;
using Xamarin.Forms;

namespace MyFirstFormsApp
{
	public class App : Application
	{
		
		public App()
		{
			MainPage = new ContentPage {
				Content = new Label {
					Text = "Hello, Forms!",
					VerticalOptions = LayoutOptions.CenterAndExpand,
					HorizontalOptions = LayoutOptions.CenterAndExpand,
				}
			}; 
		}
		protected override void OnStart ()
		{
			// Handle when your app starts
		}

		protected override void OnSleep ()
		{
			// Handle when your app sleeps
		}

		protected override void OnResume ()
		{
			// Handle when your app resumes
		}
	}
}

While a ContentPage object was returned in the example above, note that any class that extends Page could have been passed, instead. For example, by using conditional compilation or by checking the platform, the developr can pass a CarouselPage to Windows Phone applications, in order to better match the style of the user interface on that platform, while passing ContentPage objects or other Page types to the other platforms.

The other projects in the solution that target the Windows Phone, iOS, and Android platforms can call the GetMainPage method to obtain the Page descendant that describes the portable user interface. This object can then be used with platform-specific static methods or extension methods to incorporate it into the native UI for each platform.

In each platform-specific project, Application developers must call the Xamarin.Forms.Forms.Init() method, with platform-specific parameters, before they get or create any Xamarin.Forms elements.

Each targeted platform uses the returned page in a different way. The Xamarin.Forms.Platform.iOS library provides Xamarin.Forms.Page.CreateViewController() extension method, which returns a UIViewController that application developers can assign to the UIWindow.RootViewController property of the top-level UI. This code is typically placed inside the UIApplicationDelegate.FinishedLaunching override for the main application class. A typical example is shown below:

C# Example

using System;
using Xamarin.Forms;

namespace MyFirstFormsApp.iOS
{
    [Register("AppDelegate")]
    public partial class AppDelegate : UIApplicationDelegate
    {
        UIWindow window;

        public override bool FinishedLaunching(UIApplication app,
                                               NSDictionary options)
        {
            Forms.Init();

            window = new UIWindow(UIScreen.MainScreen.Bounds);

            window.RootViewController = App.GetMainPage().CreateViewController();
            window.MakeKeyAndVisible();

            return true;
        }
    }
}

The Xamarin.Forms.Platform.Android.AndroidActivity class provides the Xamarin.Forms.Platform.Android.AndroidActivity.SetPage method, which performs the work that is necessary to make its page argument the top-level UI element of the Xamarin.Forms.Platform.Android.AndroidActivity. A typical example is shown below:

C# Example

using System;
using Android.App;
using Android.OS;
using Xamarin.Forms.Platform.Android;


namespace MyFirstFormsApp.Android
{
    [Activity(Label = "MyFirstFormsApp", MainLauncher = true)]
    public class MainActivity : AndroidActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            Xamarin.Forms.Forms.Init(this, bundle);

            SetPage(App.GetMainPage());
        }
    }
}

For Windows Phone, Xamarin.Forms provides an extension method for Page that is called Page.ConvertPageToUIElement. This method returns a System.Windows.UIElement object that has the page that was passed to it as its current page. A typical example is shown below:

C# Example

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

using Xamarin.Forms;


namespace MyFirstFormsApp.WinPhone
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();

            Forms.Init();
            Content = Phoneword.App.GetMainPage().ConvertPageToUIElement(this);
        }
    }
}

In addition to their role as the main pages of Xamarin.Forms applications, Page objects and their descendants can be used with navigation classes, such as Navigation or MasterDetailPage, among others, to provide rich user experiences that conform to the expected behaviors on each platform.

XAML for Xamarin.Forms supports the following properties for the Page class:

PropertyValue
BackgroundImage

A local file specification that identifies an image.

Icon

A local file specification that identifies an image.

Padding

A comma-separated list of 4 integers that represent a Thickness structure.

Title

Text that represents the title of the page.

ToolbarItems

A list of ToolBarItem elements.

Requirements

Namespace: Xamarin.Forms
Assembly: Xamarin.Forms.Core (in Xamarin.Forms.Core.dll)
Assembly Versions: 1.0.0.0, 1.1.0.0, 1.2.0.0, 1.3.0.0, 1.4.0.0, 1.5.0.0, 2.0.0.0

The members of Xamarin.Forms.Page are listed below.

See Also: VisualElement

Public Constructors

Creates a new Page element with default values.

Public Fields

const
ActionSheetSignalNameString. This method is for internal use.
const
AlertSignalNameString. This method is for internal use .
static readonly
BackgroundImagePropertyBindableProperty. Identifies the Page.BackgroundImage property.
const
BusySetSignalNameString. This method is for internal use.
static readonly
IconPropertyBindableProperty. Identifies the Page.Icon property.
static readonly
IsBusyPropertyBindableProperty. Identifies the Page.IsBusy property.
static readonly
PaddingPropertyBindableProperty. Identifies the Page.Padding property.
static readonly
TitlePropertyBindableProperty. Identifies the Page.Title property.

Public Properties

BackgroundImageString. Identifies the image used as a background for the Page.
IconFileImageSource. Resource identifier for the Page's associated icon.
IsBusyBoolean. Marks the Page as busy. This will cause the platform specific global activity indicator to show a busy state.
PaddingThickness. The space between the content of the Page and it's border.
TitleString. The Page's title.
[read-only]
ToolbarItemsIList<ToolbarItem>. A set of ToolbarItems, implemented in a platform-specific manner.

Public Methods

DisplayActionSheet(String, String, String, params String[]) : System.Threading.Tasks.Task<string>
Displays a native platform action sheet, allowing the application user to choose from several buttons.
DisplayAlert(String, String, String) : Task
Presents an alert dialog to the application user with a single cancel button.
DisplayAlert(String, String, String, String) : System.Threading.Tasks.Task<bool>
Presents an alert dialog to the application user with an accept and a cancel button.
ForceLayout()
Forces the Page to perform a layout pass.
On<T>() : IPlatformElementConfiguration<T, Page>
TReturns the platform-specific instance of this Page, on which a platform-specific method may be called.
SendBackButtonPressed() : Boolean
Calls Page.OnBackButtonPressed.

Protected Methods

LayoutChildren(Double, Double, Double, Double)
Lays out children Elements into the specified area.
OnAppearing()
When overridden, allows application developers to customize behavior immediately prior to the Page becoming visible.
OnBackButtonPressed() : Boolean
Application developers can override this method to provide behavior when the back button is pressed.
override
OnBindingContextChanged()
Invoked whenever the binding context of the Page changes. Override this method to add class handling for this event.
OnChildMeasureInvalidated(Object, EventArgs)
Indicates that the preferred size of a child Element has changed.
OnDisappearing()
When overridden, allows the application developer to customize behavior as the Page disappears.
override
OnParentSet()
Called when the Page's Element.Parent property has changed.
override
OnSizeAllocated(Double, Double)
Indicates that the Page has been assigned a size.
UpdateChildrenLayout()
Requests that the children Elements of the Page update their layouts.

Public Events

AppearingIndicates that the Page is about to appear.
DisappearingIndicates that the Page is about to cease displaying.
LayoutChangedRaised when the layout of the Page has changed.

Explicitly Implemented Interface Members