Cross Platform Mobile
Development using
Xamarin Forms
Onebit Brownbag Session
Raka Westu Mogandhi
Slide 2
Slide 2 text
Why Cross-Platform
Development?
• Single code base
• Multiple deployment options
• Native application
Slide 3
Slide 3 text
Xamarin
“Deliver native iOS, Android, and Windows apps
using existing skills, teams, and code.”
Slide 4
Slide 4 text
Xamarin Overview
• Xamarin is not a cross platform framework but a multi platform
framework.
• Xamarin wraps all platform codes in C#.
• Xamarin mobile platforms
• Xamarin Android
• Tamarin Mac
• Xamarin iOS
• Xamarin Forms
Slide 5
Slide 5 text
Xamarin Forms
• Single UI code base using XAML
• Single C# code base
• Best for Apps that
• Require little platform specific functionality
• Code sharing is more important than custom UI
How is the Xamarin Forms
UI rendering works?
Each Xamarin UI control will render its control into
each platform native UI.
For example:
Slide 8
Slide 8 text
How is the Xamarin Forms
UI rendering works?
• TabbedPage
• TabLayout in Android
• UITabBar in iOS
• StackLayout -> Stack container
• LinearLayout in Android
• StackView in iOS
Slide 9
Slide 9 text
How is the Xamarin Forms
UI rendering works?
• Entry
• EditText in Android
• UITextField in iOS
• Button -> Stack container
• Button in Android
• UIButton in iOS
Slide 10
Slide 10 text
How is the Xamarin Forms
UI rendering works?
• Label
• TextView in Android
• UILabel in iOS
• Image
• ImageView in Android
• UIImageView in iOS
Slide 11
Slide 11 text
How is the Xamarin Forms
UI rendering works?
Slide 12
Slide 12 text
How to use Custom UI
Control?
• Define Custom UI Control in Shared Code
namespace Apollo
{
public class CustomLabel : Label
{
}
}
• Define Custom UI implementation in each platform
• Android
[assembly: ExportRenderer (typeof (CustomLabel), typeof (CustomViewRenderer))]
namespace Apollo.Droid
{
public class CustomViewRenderer : LabelRenderer
{
protected override void OnElementChanged (ElementChangedEventArgs e) {
base.OnElementChanged (e);
var textView = (TextView)Control;
// Custom your TextView here
}
}
}
Slide 13
Slide 13 text
How to use Custom UI
Control?
• Define Custom UI Control in Shared Code
namespace Apollo
{
public class CustomLabel : Label
{
}
}
• Define Custom UI implementation in each platform
• iOS
[assembly: ExportRenderer(typeof(CustomLabelRenderer), typeof(CustomLabel))]
namespace Apollo.iOS
{
public class CustomLabelRenderer: LabelRenderer
{
protected override void OnElementChanged (ElementChangedEventArgs e)
{
base.OnElementChanged (e);
var label = (UILabel) Control;
// Customize your UILabel here
}
}
}
Slide 14
Slide 14 text
How to Call Platform specific
Function?
• Use DependencyService and Interface
• Example:
• In Shared Code
• Interface
• Call using DependencyService
DependencyService.Get().TrackAppPage(pageName);
Slide 15
Slide 15 text
How to Call Platform specific
Function?
• Use DependencyService and Interface
• Example:
• In Android Implementation
[assembly: Dependency(typeof(AnalyticsService))]
namespace Apollo.Droid
{
public class AnalyticsService : IAnalyticsService
{
// Android Implementation
}
}
• In iOS Implementation
[assembly: Dependency(typeof(AnalyticsService))]
namespace Apollo.iOS
{
public class AnalyticsService : IAnalyticsService
{
// iOS implementation
}
}
Slide 16
Slide 16 text
Pros and Cons
• Pros
• Only use XAML and C# for shared codebase (Good for existing .NET
Developers)
• Single code base (UI and logic)
• Easy to use MVVM architecture (Easy data binding support both in XAML
and C# codes)
• Cons
• No UI (XAML) preview
• Different license to build each platform
• Not easy to import/use native library