Xamarin.Forms

35
Xamarin.Forms

description

Presentation (Dutch): https://www.youtube.com/watch?v=J7GHFf3M9Ow With the release of Xamarin 3.0, a new way has been created to develop the UI for mobile applications. This library, named Xamarin.Forms, is the next generation of abstraction and makes it possible for developers to create cross-platform applications with 99,9% code re-use. Using Xamarin.Forms, you'll be able to use C# or XAML to define the mobile UI using one single way. Xamarin deliveres a load of standard controls, but makes it possible to create custom controls for a specific platform with ease. This way, you can create full native controls, and get the best performance and user experience out of your app. We'll dive into some basics of Xamarin.Forms, and check the power of the library as well as some bumps you might run into.

Transcript of Xamarin.Forms

Page 1: Xamarin.Forms

Xamarin.Forms

Page 2: Xamarin.Forms

Hi!

Marcel de Vries• @marcelv• [email protected]

Marco Kuiper• @marcofolio• [email protected]

Page 3: Xamarin.Forms

Before

80% shared code

20% OS specific

Page 4: Xamarin.Forms

Meet Xamarin.Forms

Page 5: Xamarin.Forms

The promise

99,9% shared code

0,1% OS specific

Page 6: Xamarin.Forms

Pages

ContentPage MasterDetailPage NavigationPage TabbedPage CarouselPage

Page 7: Xamarin.Forms

Layouts

AbsoluteLayoutStackLayout RelativeLayout GridLayout ContentView ScrollView Frame

Page 8: Xamarin.Forms

Views

Page 9: Xamarin.Forms

Cheat Sheet http://tinyurl.com/xamarin-forms

Page 10: Xamarin.Forms

Cheat Sheet http://tinyurl.com/xamarin-forms

Page 11: Xamarin.Forms

Cheat Sheet http://tinyurl.com/xamarin-forms

Page 12: Xamarin.Forms

But how? C#

Page 13: Xamarin.Forms

But how? XAML

ViewModelData binding

Page 14: Xamarin.Forms
Page 15: Xamarin.Forms

Case Study

Page 16: Xamarin.Forms

ISKE App (Yes a new version is on its way!)

Page 17: Xamarin.Forms

Xamarin forms Architecture

• The Forms code is a thin wrapper that tanslates XAML to the native equivalents on the platform

Renderer

UILabel TextView TextBlock

LabelRendererLabelRendererLabelRenderer

Page 18: Xamarin.Forms

Xamarin forms Architecture

• The Forms code is a thin wrapper that tanslates XAML to the native equivalents on the platform

Renderer

UITableView ListView LongListSelector

ListViewRendererListViewRenderer ListViewRenderer

Page 19: Xamarin.Forms

Challenges

• How to translate a specificUI widget created in our previous app?• Custom controls to the

rescue

Page 20: Xamarin.Forms

Custom controls

• Create a class that contains the properties you need to bind on the screen• E.g. Date time String containing the Day, Month and Time

public class DateTile : View { public static readonly BindableProperty DateOnTileProperty = BindableProperty.Create<DateTile, string>(p => p.DateOnTile,

DateTime.Now.ToString("dd MMM hh:mm"));

//Gets or sets the color of the progress bar public string DateOnTile { get { return (string)GetValue(DateOnTileProperty); } set { SetValue(DateOnTileProperty, value); } }

}

Inherit from the control type that provides the basics of your custom control

Page 21: Xamarin.Forms

Custom controls

• Create a renderer for each platform (use the cheat sheet to know which renderer to use)

public class DateTileRenderer : ViewRenderer<DateTile,UIView>{ protected override void OnElementChanged(ElementChangedEventArgs<DateTile> e) { base.OnElementChanged (e); if (e.OldElement != null || this.Element == null)

return; var Control = new UIView(new RectangleF(0,0,60,78)); this.AddSubview(Control); this.SetNativeControl(Control); } protected override void OnElementPropertyChanged(object sender,System.ComponentModel.PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); if(e.PropertyName == DateTile.DateOnTileProperty.PropertyName) { var control = (DateTile)sender; // insert your code here to show the updated control (in my case update the labels) this.SetNativeControl(yourcustomNativeInstance); }}

Initially called to create the control

Called when a property on your control gets changed

Page 22: Xamarin.Forms

Custom controls that contains a set of other controls

• E.g. a control wrapping a row in a table and is nothing mre then a composition of other controls• In this case you can override the behavior in the shared code in stead of the custom

renderer per platform• Construct the abstract control in code on creations• Implement the OnPropertyChanged in the generic custom control

Page 23: Xamarin.Forms

Platform specific (Alternative to #ifdef)

• C#:Device.OnPlatform(

iOS: () => box.Padding = new Thickness (20, 0),

Android: () => box.Padding = new Thickness (20)

);

• XAML:<StackLayout.Padding>

<OnPlatform x:TypeArguments="Thickness">

<OnPlatform.iOS>20, 0</OnPlatform.iOS>

<OnPlatform.Android>20</OnPlatform.Android>

</OnPlatform>

</StackLayout.Padding>

Page 24: Xamarin.Forms
Page 25: Xamarin.Forms

More than forms

Page 26: Xamarin.Forms

Messaging Center

MessagingCenterMaster

Subscribes

Receives message

Detail

Sends message

Page 27: Xamarin.Forms

Animations

• Cross-platform animations

• Async / Await API

Page 28: Xamarin.Forms

Dependency Service

Access to Native Features

Three steps1. Interface2. Registration3. Location

Page 29: Xamarin.Forms
Page 30: Xamarin.Forms

Conclusion

Page 31: Xamarin.Forms

Visual Studio vs Xamarin Studio

• Windows Phone support only in visual studioand only Windows 8 Silverlight (no universal apps)

• Xamarin studio will not load Windows Phone apps• Sometimes visual studio injects 2013 version in SLN

header -> Will render solution unusable in Xamarin Studio

• Xamarin studio now also supports NuGetArguably better implemented then in VS

• On Pull from GIT, Xamarin studio does not update the solution, you need to manually Unload and reload

• Visual studio linker sometimes operated more aggressiveresulting in runtime error on missing libraries• Image suffers from this problem at the moment• Assembly redirects do not solve the issue, neither do the

auto generate redirects• Shared Libraries are not implemented consistent cross

Ide’s• Xamarin studio allows more-> results in crashed of VS

Page 32: Xamarin.Forms

XAML

• Is not a 1:1 translation, since you need to work with new concepts• StackPanel -> StackLayout• TextBlock -> Label• Etc.

• No UI editor• XAML intellisense is non existent• I first code in C# with Intellisense, then I translate it to Xaml

Page 33: Xamarin.Forms

PCL vs Shared

• Shared projects are great but:• They are clearly a 1.0 version• No feature parity between VS and Xamarin studio• Lot of stuff is allowed in Xamarin studio, that will break VS

• After a couple of tries, I moved to PCL• Way more mature at the moment• Did not hit any nasty issues yet

• When to use what (When it works eventualy)?• In theory I would propose: Use Share Libs for the stuff you want to share between

platforms but you don’t want to reuse cross projects• Use PCL when you create stuff that you would share cross projects• Better, also bundle it as NuGet package in your build

Page 34: Xamarin.Forms

Silver bullet ?

• At first we where very skeptical about this solution• We always believed the Native UI on each platform is a key thing to good apps• Xamarin seems to have found a way to get best of both worlds• Share cross platforms, but translate to native device equivalents• This gives us both the native feel and the best performance

• The 99% looks nice, but:• Remember the experience is counts not the % of code sharing you got!• You will need (at least at the moment) custom renderers to fix UI things on different

platforms (e.g. set transparent color so you can have an image background)

Page 35: Xamarin.Forms

Thank you!