69

Customizing Xamarin.Forms UI

  • Upload
    xamarin

  • View
    579

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Customizing Xamarin.Forms UI
Page 2: Customizing Xamarin.Forms UI

Training Manager

@RobGibbens

[email protected]

github.com/RobGibbens

arteksoftware.com

Page 3: Customizing Xamarin.Forms UI
Page 4: Customizing Xamarin.Forms UI
Page 5: Customizing Xamarin.Forms UI
Page 6: Customizing Xamarin.Forms UI

[DEMO]

[DEMO]

[DEMO]

Page 7: Customizing Xamarin.Forms UI

Our work and personal data is in the cloud

Page 8: Customizing Xamarin.Forms UI

We have multiple devices and use whichever is best for each task

Page 9: Customizing Xamarin.Forms UI

We use devices in all aspects of our lives

Page 10: Customizing Xamarin.Forms UI

We expect our data to be available on every device we use

Page 11: Customizing Xamarin.Forms UI

iOS

Page 12: Customizing Xamarin.Forms UI
Page 13: Customizing Xamarin.Forms UI

Visual Studio

for Windows or Mac.NET Library.NET Languages

Page 14: Customizing Xamarin.Forms UI

iOS

C# UI

Shared C# business logic

Android

C# UI

Windows

C# UI

Xamarin.Android and Xamarin.iOS

Separate UIs per platform

Page 15: Customizing Xamarin.Forms UI

iOS

C# UI

Shared C# business logic

Android

C# UI

Windows

C# UI

Xamarin.Android and Xamarin.iOS

Separate UIs per platform

Shared C# UI

Shared C# business logic

Xamarin.Forms

Shared UI definition

Page 16: Customizing Xamarin.Forms UI
Page 17: Customizing Xamarin.Forms UI

share

dp

latfo

rm

public class Button : Element{

public Color BorderColor { get; set; }public int BorderRadius { get; set; }public double BorderWidth { get; set; }public string Text { get; set; }public Color TextColor { get; set; }...

}

Android.Widget.Button

Click Me, I Dare You!

UIKit.UIButtoniOS

Click Me, I Dare You!

Windows.UI.Xaml.Controls.Button

Click Me, I Dare You!

Page 18: Customizing Xamarin.Forms UI

share

d

pla

tform

Fewer customization

options than native peers

public class Button : View ... {

...}

public Color CurrentHintColor {...}public Color CurrentTextColor {...}public Color HighlightColor {...}public Color SolidColor {...}public ColorStateList HintTextColors {...}public ColorStateList LinkTextColors {...}public ColorStateList TextColors {...}public Drawable Background {...}...

public class Button : Element{

public Color BorderColor { get; set; }public int BorderRadius { get; set; }public double BorderWidth { get; set; }public string Text { get; set; }public Color TextColor { get; set; }...

}

Page 19: Customizing Xamarin.Forms UI

RuntimePlatform checks

Platform Themes

Effects

Custom Renderers

Embedding Native Controls

Embedding Xamarin.Forms

Page 20: Customizing Xamarin.Forms UI
Page 21: Customizing Xamarin.Forms UI
Page 22: Customizing Xamarin.Forms UI

<OnPlatform />

<Label><Label.BackgroundColor>

<OnPlatform x:TypeArguments="Color"><On Platform="Windows" Value="Red" /><On Platform="iOS" Value="Green" /><On Platform="Android" Value="Black" />

</OnPlatform></Label.BackgroundColor>

</Label>

Page 23: Customizing Xamarin.Forms UI

if (Device.RuntimePlatform == Device.iOS) {// Code for iOS onlyif (Device.TargetIdiom == TargetIdiom.Tablet) {

// Code for iPad only}

}

Page 24: Customizing Xamarin.Forms UI
Page 25: Customizing Xamarin.Forms UI
Page 26: Customizing Xamarin.Forms UI

android:themeUIAppearance

Style +ControlTemplate

iOS

Page 27: Customizing Xamarin.Forms UI

<Application.Resources><Style TargetType="TextBlock">

<Setter Property="Foreground" Value="Yellow" /><Setter Property="FontFamily" Value="Verdana" /><Setter Property="FontSize" Value="96" />

</Style><Application.Resources/>

Hello Xamarin.Forms!

Page 28: Customizing Xamarin.Forms UI

public override bool FinishedLaunching(...){

UISwitch.Appearance.OnTintColor = UIColor.Orange;UISlider.Appearance.MinimumTrackTintColor = UIColor.Magenta;UISlider.Appearance.MaximumTrackTintColor = UIColor.Cyan;

UINavigationBar.Appearance.BarTintColor = UIColor.FromRGB(51, 134, 238);UINavigationBar.Appearance.SetTitleTextAttributes(new UITextAttributes(){ TextColor = UIColor.White, Font = UIFont.ItalicSystemFontOfSize(20)});

}

Page 29: Customizing Xamarin.Forms UI

[Activity(Label = "DroidThemes",Theme = "@android:style/Theme.Material.Light",MainLauncher = true, Icon = "@mipmap/icon")]

public class MainActivity : Activity{

...}

Page 30: Customizing Xamarin.Forms UI
Page 31: Customizing Xamarin.Forms UI
Page 32: Customizing Xamarin.Forms UI
Page 33: Customizing Xamarin.Forms UI

Xamarin

Forms Entry

ShadowEffectuse the Android APIs

EditText

Result is a native

control with the

effect applied

Page 34: Customizing Xamarin.Forms UI

Element

(Button)

Xamarin.Forms.Platform.iOS.ButtonRenderer

UIButton

MyIOSShadowEffect : PlatformEffect

share

dp

latfo

rm

iOS

Page 35: Customizing Xamarin.Forms UI

class MyAndroidShadowEffect : ...{ ... }

class MyIOSShadowEffect : ...{ ...}

class MyUWPShadowEffect : ...{ ...}

The implementation

has access to the

native APIs

share

d

pla

tform

iOS

Page 36: Customizing Xamarin.Forms UI

Button dareButton = new Button();...Effect eff = Effect.Resolve("MyCompany.ShadowEffect"));

dareButton.Effects.Add(eff); // ok

Add the effect

to the collection

Resolve by name

share

d

pla

tform

Page 37: Customizing Xamarin.Forms UI

public class ShadowEffect : RoutingEffect{

public ShadowEffect(): base("MyCompany.ShadowEffect")

{}

}

Supply effect identifier to the

RoutingEffect constructor

share

d

pla

tform

Page 38: Customizing Xamarin.Forms UI

Effect.Resolve("MyCompany.ShadowEffect")

[assembly: ResolutionGroupName("MyCompany")][assembly: ExportEffect(typeof(MyIOSShadowEffect), "ShadowEffect")]

shared

platform

Page 39: Customizing Xamarin.Forms UI
Page 40: Customizing Xamarin.Forms UI
Page 41: Customizing Xamarin.Forms UI

public class Button : Element{

public Color BorderColor { get; set; }public int BorderRadius { get; set; }public double BorderWidth { get; set; }public string Text { get; set; }public Color TextColor { get; set; }...

}

Android.Widget.Button

Windows.UI.Xaml.Controls.Buttonsh

are

dp

latfo

rm

UIKit.UIButton

iOS

Page 42: Customizing Xamarin.Forms UI

Xamarin.Forms.Platform.Android.ButtonRenderer

Xamarin.Forms.Platform.iOS.ButtonRenderer

Xamarin.Forms.Platform.WinRT.ButtonRenderer

Xamarin.Forms.ButtonElement

share

d

pla

tform iOS

Page 43: Customizing Xamarin.Forms UI

XF Element Button ContentPage ContentView EntryCell …

iOS ButtonRenderer PageRenderer ViewRenderer EntryCellRenderer …

Android ButtonRenderer PageRenderer ViewRenderer EntryCellRenderer …

Windows ButtonRenderer PageRenderer ViewRenderer EntryCellRenderer …

Page 44: Customizing Xamarin.Forms UI
Page 45: Customizing Xamarin.Forms UI

Subclass

element1

Subclass renderer

and adjust native

properties

2

Export

renderer for

element

3

share

d

pla

tform

iOS

Page 46: Customizing Xamarin.Forms UI

public class MyGaugeView : View{

...} sh

are

d

pla

tform

Page 47: Customizing Xamarin.Forms UI

public class MyGaugeRenderer : ViewRenderer<MyGaugeView, MyiOSGaugeView> {

...}

public class MyGaugeRenderer : ViewRenderer<MyGaugeView, MyAndroidGaugeView>{

...}

public class MyGaugeRenderer : ViewRenderer<MyGaugeView, MyWindowsGaugeView>{

...}

share

d

pla

tform

iOS

Page 48: Customizing Xamarin.Forms UI

protected override void OnElementChanged(...){

base.OnElementChanged(e);

var gaugeView = new MyiOSGaugeView();

base.SetNativeControl(gaugeView);}

Create the

native control

share

d

pla

tform

Tell Xamarin.Forms to add it to the native

screen, this assigns the Control property iOS

Page 49: Customizing Xamarin.Forms UI

share

d

pla

tform

[assembly: ExportRenderer(typeof(MyGaugeView), typeof(MyGaugeRenderer))]

The platform-specific renderer

visualizing the element

The element type

Page 50: Customizing Xamarin.Forms UI
Page 51: Customizing Xamarin.Forms UI
Page 52: Customizing Xamarin.Forms UI
Page 53: Customizing Xamarin.Forms UI

Many of the native

controls do not have

Xamarin.Forms versions

Page 54: Customizing Xamarin.Forms UI

A native control like the iOSUISegmentedControlcan be embedded into a

Xamarin.Forms layout

Page 55: Customizing Xamarin.Forms UI

var xfContentView = new ContentView();xfContentView.Content = xfView;

var iOSButton = UIButton.FromType(UIButtonType.DetailDisclosure);iOSButton.TouchUpInside += () => { ... };

View xfView = iOSButton.ToView();Get a View that can be used in

the Xamarin.Forms visual tree

The Xamarin.Forms View can be

assigned to the content property

Page 56: Customizing Xamarin.Forms UI
Page 57: Customizing Xamarin.Forms UI
Page 58: Customizing Xamarin.Forms UI
Page 59: Customizing Xamarin.Forms UI

Pages

Page 60: Customizing Xamarin.Forms UI

Fragment nativeElement = new MyXamarinFormsPage().CreateFragment(context: this);

FrameworkElement nativeElement = new MyXamarinFormsPage().CreateFrameworkElement();

UIViewController nativeElement = new MyXamarinFormsPage().CreateViewController(); iOS

Page 61: Customizing Xamarin.Forms UI
Page 62: Customizing Xamarin.Forms UI
Page 63: Customizing Xamarin.Forms UI
Page 64: Customizing Xamarin.Forms UI

Starting at $83.25/month

Use existing Azure subscription

Flexible billing options

Page 65: Customizing Xamarin.Forms UI

aka.ms/buy-xamarin-university

Start your free Xamarin University trial.

Learn to build better mobile apps.

Page 66: Customizing Xamarin.Forms UI

Download

visualstudio.com

xamarin.com

Demo Code

aka.ms/customizingxamarinforms

Xamarin University self-guided learning and free trial

aka.ms/buy-xamarin-university

“Xamarin University Presents” Webinar series

aka.ms/xam-u-webinars

aka.ms/xam-u-webinar-recordings

Page 67: Customizing Xamarin.Forms UI
Page 68: Customizing Xamarin.Forms UI

Rob Gibbens@RobGibbens

[email protected]

Page 69: Customizing Xamarin.Forms UI

© Copyright Microsoft Corporation. All rights reserved.