testing
testing

Reputation: 20289

Xamarin.Forms: Use of GetMainPage()

I'm currently reading the navigation section from An Introduction to Xamarin.Forms. One should use the GetMainPage() method. But how should that be used?

The default implementation of the app delegate looks like the following:

Applicaton Delegate:

public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    public override bool FinishedLaunching (UIApplication app, NSDictionary options)
    {
        global::Xamarin.Forms.Forms.Init ();

        LoadApplication (new App ());

        return base.FinishedLaunching (app, options);
    }
}

App:

public class App : Application
{
    public App ()
    {
        MainPage = GetMainPage ();
    }

    public static Page GetMainPage()
    {
        var mainNav = new NavigationPage(new ListExample());
        return mainNav;
    }
}

I got it managed to use the GetMainPage()method instead of getting

Application windows are expected to have a root view controller at the end of application launch

If I look into the (old?) examples (example1, example2) the app delegate is different and a CreateViewController() method is available. In my case it is not!

What is the correct way of loading the root page on to the stack?

Upvotes: 2

Views: 3958

Answers (1)

Krumelur
Krumelur

Reputation: 33068

You don't have to use GetMainPage(); that's just a method you create. The way X.Forms works these days is: it exposes a MainPage property in the Xamarin.Forms Application class. You set this to an instance of a Page. How you create that page is up to you. You can either use

this.MainPage = new ContentPage { Content = ... }

or you create one file per page (which IMHO is best for maintainability):

this.MainPage = new MyLoginPage();

or you use helper methods which create your pages:

this.MainPage = this.GetMainPage();

The main page is the first page of your Forms application. You can set the MainPage property to a different value to show another page.

Earlier versions of Forms used different approaches and not all samples have been updated yet. Now all platforms only need a call to the Forms Init() method and a call to LoadApplication() instead of creating a view controller, an activity or a page (WP8).

Upvotes: 6

Related Questions