With the new release of the Xamarin.Forms 1.3 version, the App Class has its new Avatar.
The older version of the App Class was just a class with static methods.
Unlike the older version, the new version is an inherited class from the Xamarin.Forms.Application class.
Summary of improvements
New life cycle events have been added.
We can handle separate events during various phases of an app's interaction life cycle.
- on Start: Allows us to handle the app start up. We can write code here to be executed during on app start up.
- on Sleep: We can write code here to be executed when an app is entering into the sleep/minimize mode.
- on Resume: Code can be executed after the app regains the focus.
Self static object
The Application class uses a static instance of its own that is named as Current. It can be accessed using out the application.
MainPage attribute
MainPage is an attribute of the Application Class of type ContentPage. As the name suggests, we can assign the root page of the application to this attribute.
-
- MainPage = new ContentPage
- {
- Content = new StackLayout
- {
- VerticalOptions = LayoutOptions.Center,
- Children = {
- new Label {
- XAlign = TextAlignment.Center,
- Text = "Welcome to Xamarin Forms!"
- }
- }
- }
- };
Properties
Not to be confused, this is an attribute of the Application Class. It contains the Dictionary object. We can read/write application level global variables to it for further use in a Key and Value pair format.
- Application.Current.Properties.Add(new KeyValuePair<string, object>("AppOwner", "Nirmal"));
Resources
The Application Class Resources attribute can store the resources for application use. This attribute is of type ResourceDictionary. We can add Implicit Styles (in XAML) and Key Value pair strings to it.
- Application.Current.Resources.Add("AppCaption", "My App");
Happy coding.