Major Enhancements to Xamarin.Forms

James Montemagno

Xamarin.Forms is a powerful library that enables developers to build native user interfaces from a shared C# code base for iOS, Android, and Windows Phone. The latest release, Xamarin.Forms 1.3, introduces several major enhancements to the platform to increase productivity and to extend your application even further. Additionally, this release also brings Unified support for iOS applications built with Xamarin.Forms to coincide with the release of Xamarin.iOS 8.6, which is required for this update.

crm-app

Application Lifecycle

It is now easier than ever to respond to application lifecycle events in your Xamarin.Forms applications. With the new Application base class you can tap into several lifecycle methods such as OnStart, OnSleep, and OnResume. The Application class also delivers a simplified solution for specifying your MainPage of your application. Here is an example of what your new Application class might look like:

public class App : Xamarin.Forms.Application
{
    public App ()
    {
        MainPage = new ContentPage { Title = "App Lifecycle Sample" }; // your page here
    }

    protected override void OnStart()
    {
        // Handle when your app starts
        Debug.WriteLine ("OnStart");
    }

    protected override void OnSleep()
    {
        // Handle when your app sleeps
        Debug.WriteLine ("OnSleep");
    }

    protected override void OnResume()
    {
        // Handle when your app resumes
        Debug.WriteLine ("OnResume");
    }
}

Be sure to read through our Xamarin.Forms Application Lifecycle documentation on how to get started using or transitioning your app to take advantage of these new events.

Styles

Xamarin developers love that they are able to use XAML to declare their user interfaces across iOS, Android, and Windows Phone. However, the immense amount of properties on controls can make your XAML cluttered. Additionally, if you wanted to change the look across all of your controls, you had to update each and every control. The introduction of Styles alleviates this issue by allowing you to define control defaults, known as styles, in your ResourceDictionary. Here is an example of what a few buttons might look like on a page:

<!--?xml version="1.0" encoding="UTF-8"?-->
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
	     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             x:Class="StylesApps.MyPage">
  <ContentPage.Content>
    <StackLayout Padding="10" Spacing="10">
      <Button Text="Login"
              HorizontalOptions = "FillAndExpand"
              HeightRequest = "42"
              BackgroundColor = "#77d065"
              BorderColor = "Black"
              BorderRadius = "5"
              BorderWidth = "0"
              TextColor = "White"/>
      <Button Text="Logout"
              HorizontalOptions = "FillAndExpand"
              HeightRequest = "42"
              BackgroundColor = "#77d065"
              BorderColor = "Black"
              BorderRadius = "5"
              BorderWidth = "0"
              TextColor = "White"/>
      <Button Text="Register"
              HorizontalOptions = "FillAndExpand"
              HeightRequest = "42"
              BackgroundColor = "#77d065"
              BorderColor = "Black"
              BorderRadius = "5"
              BorderWidth = "0"
              TextColor = "White"/>
    </StackLayout>
  </ContentPage.Content>
</ContentPage>

You can see there are a lot of properties that are the same across all of these buttons. To clean this up, you simply need define a base style in the ContentPage’s Resources, and then set the Button’s Style property to the Key.

<!--?xml version="1.0" encoding="UTF-8"?-->
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
			xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
			x:Class="StylesApps.MyPage">
  <ContentPage.Resources>
    <ResourceDictionary>
      <Style x:Key="ButtonStyle" TargetType="Button">
         <Setter Property="HorizontalOptions" Value="FillAndExpand"/>
         <Setter Property="BackgroundColor" Value="#77d065"/>
         <Setter Property="BorderColor" Value="Black"/>
         <Setter Property="HeightRequest" Value="42"/>
         <Setter Property="BorderRadius" Value="5"/>
         <Setter Property="BorderWidth" Value="0" />
         <Setter Property="TextColor" Value="White"/>
      </Style>
    </ResourceDictionary>
  </ContentPage.Resources>
    <ContentPage.Content>
      <StackLayout Padding="10" Spacing="10">
        <Button Text="Login" Style="{StaticResource ButtonStyle}" />
        <Button Text="Logout" Style="{StaticResource ButtonStyle}"/>
        <Button Text="Register" Style="{StaticResource ButtonStyle}"/>
      </StackLayout>
  </ContentPage.Content>
</ContentPage>

Now, adjusting any of the properties in the style will automatically apply to all of the buttons on the page that have this style set.

Styles

Triggers

There are several types of triggers now available in Xamarin.Forms including event, data, and multi triggers. If you have never heard of a trigger in styling before, they are essentially objects that enable you to apply changes when certain conditions are met on a control. For instance, you may want to scale up an Entry field when it has focus and also turn the text green. The code to implement this is as simple as extending the Style for a control in the resources. When the trigger’s property is met, the setter will be executed automatically. When the trigger’s property is not met, or changes, the setters will be returned to their original values.

<!--?xml version="1.0" encoding="UTF-8"?-->
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"     
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             x:Class="StylesApps.MyPage2">
  <ContentPage.Resources>
    <ResourceDictionary>
      <Style x:Key="EntryTrigger" TargetType="Entry">
        <Style.Triggers>
          <Trigger Property="IsFocused" Value="True" TargetType="Entry">
            <Setter Property="Scale" Value="1.1" />
            <Setter Property="TextColor" Value="Green" />
          </Trigger>
        </Style.Triggers>
      </Style>
    </ResourceDictionary>
  </ContentPage.Resources>
  <ContentPage.Content>
    <StackLayout Padding="20" Spacing="10">
      <Entry Placeholder="Username" Style="{StaticResource EntryTrigger}" />
      <Entry Placeholder="Password" Style="{StaticResource EntryTrigger}"/>
      <Button Text="Login" />
    </StackLayout>
  </ContentPage.Content>
</ContentPage>

Xamarin.Forms Triggers

The navigation system in Xamarin.Forms has also been overhauled in 1.3 to enable you to push, pop, and insert pages easily. In addition you can now specify whether you would like to animate pages in and out during navigation. Here is a look at the new INavigation API:

public interface INavigation
{
    IReadOnlyList NavigationStack { get; }
    IReadOnlyList ModalStack { get; }

    void RemovePage (Page page);
    void InsertPageBefore (Page page, Page before);

    Task PushAsync (Page page);
    Task PopAsync ();
    Task PopToRootAsync ();
    Task PushModalAsync (Page page);
    Task PopModalAsync ();

    Task PushAsync (Page page, bool animated);
    Task PopAsync (bool animated);
    Task PopToRootAsync (bool animated);
    Task PushModalAsync (Page page, bool animated);
    Task PopModalAsync (bool animated);
}

Xamarin.Forms navigation

In addition to these updates you will also find a new virtual method on any of your Pages: OnBackButtonPressed(), which allows you to tap in to find out when the physical back button was pressed on the device. Combining these updates together give you much more flexibility when building your application.

So Much More

These features are just the beginning of everything packed into Xamarin.Forms 1.3. You can read about the plethora of features and fixes in this release on the Xamarin Forums. Additionally, you will find new additions and updates to our comprehensive documentation on our developer portal to help you get started with this release.

*Note: Versions 1.3.0 and 1.3.1 have identical APIs except that the latter has support for the Unified API in Xamarin.iOS 8.6.

Learn More Live

To see Xamarin.Forms 1.3 in action covering these new features and much more, check out this webinar recording with Xamarin’s Craign Dunn.

0 comments

Comments are closed. Login to edit/delete your existing comments

Feedback usabilla icon