Rob Gibbens shows you how to build better Xamarin.Forms UI using platform-specific APIs that aren’t directly exposed by the framework. He’ll demo how to integrate native controls and adjust visual properties that are unique to each platform while still sharing the majority of your code across platforms. You’ll leave ready with the step-by-step guidance you need to take full advantage of the latest OS-specific UI patterns and designs.
Watch the webinar recording at aka.ms/xamu-xamarin.forms-ui-video
Explore Xamarin University at xamarin.com/university
14. iOS
C# UI
Shared C# business logic
Android
C# UI
Windows
C# UI
Xamarin.Android and Xamarin.iOS
Separate UIs per platform
15. 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
16.
17. shared
platform
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.UIButton
iOS
Click Me, I Dare You!
Windows.UI.Xaml.Controls.Button
Click Me, I Dare You!
18. shared
platform
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; }
...
}
35. class MyAndroidShadowEffect : ...
{ ...
}
class MyIOSShadowEffect : ...
{ ...
}
class MyUWPShadowEffect : ...
{ ...
}
The implementation
has access to the
native APIs
shared
platform
iOS
36. Button dareButton = new Button();
...
Effect eff = Effect.Resolve("MyCompany.ShadowEffect"));
dareButton.Effects.Add(eff); // ok
Add the effect
to the collection
Resolve by name
shared
platform
37. public class ShadowEffect : RoutingEffect
{
public ShadowEffect()
: base("MyCompany.ShadowEffect")
{
}
}
Supply effect identifier to the
RoutingEffect constructor
shared
platform
41. 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.Buttonshared
platform
UIKit.UIButton
iOS
47. public class MyGaugeRenderer : ViewRenderer<MyGaugeView, MyiOSGaugeView>
{
...
}
public class MyGaugeRenderer : ViewRenderer<MyGaugeView, MyAndroidGaugeView>
{
...
}
public class MyGaugeRenderer : ViewRenderer<MyGaugeView, MyWindowsGaugeView>
{
...
}
shared
platform
iOS
48. protected override void OnElementChanged(...)
{
base.OnElementChanged(e);
var gaugeView = new MyiOSGaugeView();
base.SetNativeControl(gaugeView);
}
Create the
native control
shared
platform
Tell Xamarin.Forms to add it to the native
screen, this assigns the Control property iOS
53. Many of the native
controls do not have
Xamarin.Forms versions
54. A native control like the iOS
UISegmentedControl
can be embedded into a
Xamarin.Forms layout
55. 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
60. Fragment nativeElement =
new MyXamarinFormsPage().CreateFragment(context: this);
FrameworkElement nativeElement =
new MyXamarinFormsPage().CreateFrameworkElement();
UIViewController nativeElement =
new MyXamarinFormsPage().CreateViewController(); iOS