Xamarin.Forms.Page Class
A Xamarin.Forms.VisualElement that occupies the entire screen.

See Also: Page Members

Syntax

public class Page : VisualElement, ILayout

Remarks

Xamarin.Forms.Page is primarily a base class for more useful derived types. Objects that are derived from the see Xamarin.Forms.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 Xamarin.Forms;

namespace MyFirstFormsApp
{
    public class App
    {
        public static Page GetMainPage()
        {
            return new ContentPage {
                Content = new Label {
                    Text = "Hello, Forms!",
                    VerticalOptions = LayoutOptions.CenterAndExpand,
                    HorizontalOptions = LayoutOptions.CenterAndExpand,
                }
            }; 
        }
    }
}

While a Xamarin.Forms.ContentPage object was returned in the example above, note that any class that extends Xamarin.Forms.Page could have been passed, instead. For example, by using conditional compilation or by checking the platform, the developr can pass a Xamarin.Forms.CarouselPage to Windows Phone applications, in order to better match the style of the user interface on that platform, while passing Xamarin.Forms.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 Xamarin.Forms.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.

On all platforms, Application developers must call the Xamarin.Forms.Init method 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 Xamarin.Forms.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, Xamarin.Forms.Page objects and their descendants can be used with navigation classes, such as Xamarin.Forms.Navigation or Xamarin.Forms.MasterDetailPage, among others, to provide rich user experiences that conform to the expected behaviors on each platform.

The Xamarin.Forms.Page class has the following XAML properties:

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 Xamarin.Forms.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