Supercharge xamarin forms with custom renderers and animations

26
Supercharge Xamarin Forms with Custom Renders and Animations Tom Walker Microsoft and Xamarin MVP [email protected]

Transcript of Supercharge xamarin forms with custom renderers and animations

Page 1: Supercharge xamarin forms with custom renderers and animations

Supercharge Xamarin Forms with Custom Renders and Animations

Tom Walker Microsoft and Xamarin [email protected]

Page 2: Supercharge xamarin forms with custom renderers and animations

Meet Tom Walker | [email protected]

• Founder @LdnOntNetDevs | LondonNetDevelopers.ca

• Microsoft and Xamarin MVP • Developer for 15+ years now focusing on mobile

and web frontend • Xamarin Certified Developer

Page 3: Supercharge xamarin forms with custom renderers and animations

Summary• Quick Intro to Xamarin• Animations• Custom Renderers

Page 4: Supercharge xamarin forms with custom renderers and animations

Quick Intro to Xamarin

Traditional Xamarin Approach With Xamarin.Forms:More code-sharing, all

native

iOS C# UI

Windows C# UIAndroid C# UI

Shared UI Code

Shared C# BackendShared C# Backend

Page 5: Supercharge xamarin forms with custom renderers and animations

Animations

Page 6: Supercharge xamarin forms with custom renderers and animations

Animations• Great way to add polish to your user interface

• Changing a property from one state or position to another state or position over a period of time

Page 7: Supercharge xamarin forms with custom renderers and animations

ViewExtensions• Class provides number of extensions

• TranslateTo• ScatleTo• RotateTo• FadeTo

• Async• Can use Task.WhenAll to create composite animations• Default animation takes 250 milliseconds• CancelAinmations method can be used to cancel

animations

Page 8: Supercharge xamarin forms with custom renderers and animations

Introduction Xamarin Bootstrapped Mobile Apps

https://blog.xamarin.com/kickstart-your-project-with-our-new-bootstrapped-mobile-apps/

Page 9: Supercharge xamarin forms with custom renderers and animations

LoginPage.xaml

<ContentPage> …<StackLayout x:Name="StackLayoutHeader" Spacing="30" Grid.ColumnSpan="2" Scale="0">

<Image…<Label ….

...</ContentPage>

Page 10: Supercharge xamarin forms with custom renderers and animations

LoginPage.xaml.cs

private async void Initialize() { await Task.Delay(300); await StackLayoutHeader.ScaleTo(1, (uint)App.AnimationSpeed, Easing.SinIn); await ButtonNowNow.ScaleTo(1, (uint)App.AnimationSpeed, Easing.SinIn); await ButtonSignIn.ScaleTo(1, (uint)App.AnimationSpeed, Easing.SinIn);

}

Page 11: Supercharge xamarin forms with custom renderers and animations

Demo

Page 12: Supercharge xamarin forms with custom renderers and animations

Sport App for iOS and Android

Page 13: Supercharge xamarin forms with custom renderers and animations

Custom Renderers

Page 14: Supercharge xamarin forms with custom renderers and animations

Xamarin.Forms rendering modelEntry

Xamarin.Forms

EntryRendererPlatform.iOS

UITextField

EntryRendererPlatform.UWP

TextBox

EntryRendererPlatform.Android

EditText

Page 15: Supercharge xamarin forms with custom renderers and animations

Custom RendererEntry

Xamarin.Forms

CustomEntryRendererPlatform.iOS

UITextField

CustomEntryRendererPlatform.UWP

TextBox

CustomEntryRendererPlatform.Android

EditText

Page 16: Supercharge xamarin forms with custom renderers and animations

Custom Renderers• Customer renders provide a powerful

approach for customizing the appearance and behavior of Xamarin.Froms controls.• If a custom renderer isn't

registered, then the default renderer for the control's base class will be used.

Page 17: Supercharge xamarin forms with custom renderers and animations

When to create a custom rendered

Custom Controls• Calendar• Accordion• Charting

Custom Rendering

• Text Decoration• Shadows• Platform-specific

features

Page 18: Supercharge xamarin forms with custom renderers and animations

What We’ll Build First

Page 19: Supercharge xamarin forms with custom renderers and animations

Steps To A Simple Customized Control

• Create the new class (MyEntry)• Derive from Entry • Create custom rendered in each platform• Key method is OnElementChanged

Page 20: Supercharge xamarin forms with custom renderers and animations

The MyEntry Class

public class MyEntry: Entry{ }

Page 21: Supercharge xamarin forms with custom renderers and animations

Consuming the Custom Control<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:CustomRenderer;assembly=CustomRenderer" x:Class="CustomRenderer.MainPageXaml"> <StackLayout …

<Label Text="Hello, Custom Renderer!" /> <local:MyEntry Text="In Shared Code" /> </StackLayout></ContentPage>

Page 22: Supercharge xamarin forms with custom renderers and animations

The MyEntryRenderer (iOS)

public class MyEntryRenderer: EntryRenderer{ protected override void OnElementChanged(ElementChangedEventArgs<Button> e) { base.OnElementChanged(e);

if(Control != null) { Control.BackgroundColor = UIColor.FromRGB(204, 153, 255);

Control.BoderStyle = UITextBorderStyle.Line; } }}

Page 23: Supercharge xamarin forms with custom renderers and animations

Don’t forget!

[assembly: ExportRenderer(typeof(MyEntry), typeof(MyEntryRenderer))]namespace CustomRenderer.iOS{ public class MyEntryRenderer: EntryRenderer {

Page 24: Supercharge xamarin forms with custom renderers and animations

Demo

Page 25: Supercharge xamarin forms with custom renderers and animations

Views Renderer iOS Android UWP

BoxView

BoxRenderer (iOS and Android)BoxViewRenderer (Windows Phone and WinRT)

UIView ViewGroup Rectangle

Button ButtonRenderer UIButton Button ButtonCarouselView CarouselViewRenderer UIScrollVie

w RecyclerView FlipViewDatePicker DatePickerRenderer UITextField EditText DatePickerEditor EditorRenderer UITextView EditText TextBoxEntry EntryRenderer UITextField EditText TextBox

https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/renderers/

Page 26: Supercharge xamarin forms with custom renderers and animations

Thank you, Questions?

Tom Walker Microsoft and Xamarin [email protected]