56
Advanced Xamarin.Forms Gill Cleeren @gillcleeren – www.snowball.be

Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Embed Size (px)

Citation preview

Page 1: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Advanced Xamarin.FormsGill Cleeren

@gillcleeren – www.snowball.be

Page 2: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own
Page 4: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Agenda

• Custom renderers & Effects

• Native embedding

• Behaviors

• Animations

• Drawing with SkiaSharp

• Localization

• Dependency Service

• Push notifications

Page 5: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Custom renderers & Effects

Page 6: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

What’s a renderer?

• Renders a control for each

platform

• Xamarin.Forms is a collection of

renderers per platform

• Can be extended with own

renderers

• Style changes

• New controls

Page 7: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Xamarin.Forms rendering modelEntry

Xamarin.Forms

EntryRendererPlatform.iOS

UITextField

EntryRendererPlatform.UWP

TextBox

EntryRendererPlatform.Android

EditText

Page 8: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Custom RendererCustomEntry

Xamarin.Forms

CustomEntryRendererPlatform.iOS

Modified UITextField

CustomEntryRendererPlatform.UWP

Modified TextBox

CustomEntryRendererPlatform.Android

Modified EditText

Page 9: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

DemoCreating a custom renderer

Page 10: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

That’s a lot of work…

Page 11: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Enter Xamarin.Forms effects

• Small style changes only

• Limited to changing property of a

platform control

• Subclass of PlatformEffect in

platform-specific projects, then

attached to control in PCL/STL

• Can be reused

• Custom renderer can do more

• Different behaviour, overriding

methods

Page 12: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Xamarin.Forms effects

• PlaformEffect contains OnAttached/OnDetached methods

• Also exposes Container, Control and Element properties

• OnElementPropertyChanged is available for using the effect in combination with databinding/MVVM

Page 13: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Creating an effect

Page 14: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

DEMOCreating an effect

Page 15: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Native embedding

Page 16: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Native embedding

• Allows us to use a platform-specific control

inside Xamarin.Forms

• Can be added to each control in XF that has a

Content property or Children collection

• UILabel can be added directly into

StackLayout

• Requires use of #if if used from code

• Only applicable in Shared Projects

• Works using 2 extension methods

• Add: add to Children collection

• ToView: wraps as View and sets as Content

• Extra work may be required to support correct

sizing

Page 17: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Using Native Embedding in iOS

var uiLabel = new UILabel {MinimumFontSize = 14f,Lines = 0,LineBreakMode = UILineBreakMode.WordWrap,Text = originalText,

};

stackLayout.Children.Add (uiLabel);contentView.Content = uiLabel.ToView();

Page 18: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Using Native Embedding in Android

var textView = new TextView (Forms.Context) { Text = originalText, TextSize = 14 };

stackLayout.Children.Add (textView);contentView.Content = textView.ToView();

Page 19: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Embedding via XAML

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:ios="clr-namespace:UIKit;assembly=Xamarin.iOS;targetPlatform=iOS">

<ios:UISwitch On="True">

Page 20: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

DEMONative embedding

Page 21: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Behaviors

Page 22: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

What’s a behavior?

• Allows adding extra functionality to a control

without subclassing it

• Will be attached to another control

• Typically, replaces code that would go in Code-

behind

• Instead, can be packaged to be reused

across applications

• Typical usages

• Effects

• Controlling an animation

• Adding a gesture recognizer

• Adding a validator option to an entry

• Linking an event of a control to a Command

in MVVM

• Even for controls that don’t support

commanding

Page 23: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Attached behaviors

• Static class with attached properties

• Replaced with Xamarin.Forms behaviors

public static class NumericValidationBehavior{public static readonly BindableProperty AttachBehaviorProperty =

BindableProperty.CreateAttached ("AttachBehavior", typeof(bool), typeof(NumericValidationBehavior), false, propertyChanged: OnAttachBehaviorChanged);

Page 24: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Xamarin.Forms behaviors

• Similar to XAML behaviors(Blend)

• Class that derives from Behavior<T>

• Override the OnAttachedTo• Don’t forget the

OnDetachingFrom to avoidmemory leaks

public class CustomBehavior : Behavior<View>{

protected override void OnAttachedTo (View bindable){

base.OnAttachedTo (bindable);// Perform setup

}

protected override void OnDetachingFrom (View bindable){

base.OnDetachingFrom (bindable);// Perform clean up

}

// Behavior implementation}

Page 25: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

DEMOXamarin.Forms behavior

Page 26: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Animations

Page 27: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Animations in Xamarin.Forms• Simple API

• Time-based animations

• Not a XAML API

• Can be included in behaviour and be attached this

way

• Based on ViewExtensions class

• ScaleTo

• TranslateTo

• RotateTo

• FadeTo

• …

• Async API, returns Task<bool>, requires await

• Task is returned when animation is ready or

cancelled

• True when cancelled, false when completed

• Support chaining animations

Page 28: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Sample animation code

bool isCancelled = await image.ScaleTo (2, 2000);

await image.RotateTo (360, 2000);

Page 29: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Composite animations

image.RotateTo (360, 4000);await image.ScaleTo (2, 2000);await image.ScaleTo (1, 2000);

Page 30: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

ViewExtensions class

Page 31: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Easing functions

• Easing class is included to include a transfer function to specify how the animations speeds up or slows down

• Included• BounceIn/BounceOut

• SinIn/SinOut

• …

await image.TranslateTo(0, 200, 2000, Easing.BounceIn);await image.ScaleTo(2, 2000, Easing.CubicIn);await image.RotateTo(360, 2000, Easing.SinInOut);await image.ScaleTo(1, 2000, Easing.CubicOut);await image.TranslateTo(0, -200, 2000, Easing.BounceOut);

Page 32: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

DEMOWorking with animations

Page 33: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Drawing with SkiaSharp

Page 34: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Drawing with SkiaSharp

• 2D graphics system for .NET

• Open source

• Used commonly in Google products

• Can be used from Xamarin.Forms

• Vector graphics

• Bitmaps

• Text

• NuGet package:

SkiaSharp.Views.Forms

• Requires iOS 8 or later!

Page 35: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

DEMODrawing with SkiaSharp

Page 36: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Localization

Page 37: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Using resx files

• Standard .NET approach works in

Xamarin.Forms

• Resx files

• System.Resources &

System.Globalization classes

• Device language detection is

specific per platform

Page 38: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Using resource values in code

var myLabel = new Label ();var myEntry = new Entry ();var myButton = new Button ();

// populate UI with translated text values from resourcesmyLabel.Text = AppResources.NotesLabel;myEntry.Placeholder = AppResources.NotesPlaceholder;myButton.Text = AppResources.AddButton;

Page 39: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Translating in XAML using a markup extension

<StackLayout Padding="0, 20, 0, 0"><Label Text="{i18n:Translate NotesLabel}" /><Entry Placeholder="{i18n:Translate NotesPlaceholder}" /><Button Text="{i18n:Translate AddButton}" />

</StackLayout>

Page 40: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

DEMOLocalization

Page 41: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Dependency Service

Page 42: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Dependency Service

• Dependency Service allows resolving

platform-specific implementations in

shared code

• We can thus access iOS, Android or

WP features inside PCL/Shared

• Inside Xamarin.Forms, we have a simple

dependency resolver

• 3 parts

• Interface

• Registration

• Location

Page 43: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

DEMODependency Service

Page 44: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Push notifications

Page 45: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Why push notifications?

• Send updates to users

• Alert users about an event when

the app isn’t running

• Trigger ‘something’ in the

background

• Get users back into your

application

Page 46: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Typical push notification cycle

• Several steps are required to work with notifications• Registration at app launch

• Back-end sends notification to PNS

• Maintenance

Page 47: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

We’ll face some challenges though…• Different services (GCM, APNS,

PNS)

• Different protocols (HTTP, TCP)

• Different formats (XML, JSON)

• Scalability

• Back-end complications

• Maintenance of tokens, channels…

Page 48: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Hello Azure Notification Hub

• X-plat: from any back-end to any mobile platform

• No need to store device information in the app back-end (managed)

• Routing and interest groups

• Broadcast at scale, multicast

• Telemetry

Page 49: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Setting up an Azure Notification Hub

• One-time setup• Creation of the Notification Hub

• Registration• Clients register using PNS handle

• Register with NH using PNS handle

• Sending notifications• Back-end sends message to NH

• NH pushes to PNS

Page 50: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Cross-platform push

• Client SDKs for • Android – GCM/Firebase• iOS – APNS • Windows Phone – MPNS• Windows Store/UWP – WNS

• Capable of pushing to specific platform or to all at once

• Server-side SDKs for • REST• .NET• Node• Java

Page 51: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Sending targeted notifications

• Tags as interest groups• Client app registers with tags

• Tags are simple strings (no pre-provisioning is required)

• Back-end can target all clients with the same tag

Page 52: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

DEMOPush notifications

Page 53: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Summary

• Xamarin.Forms is a powerful platform

• Getting more and more features and new SDKs added constantly

• Definitely ready for prime-time!

Page 54: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Thanks!

Page 55: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own
Page 56: Advanced Xamarin - snowball.be · What’s a renderer? •Renders a control for each platform •Xamarin.Forms is a collection of renderers per platform •Can be extended with own

Advanced Xamarin.FormsGill Cleeren

@gillcleeren