Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cross Platform Development using Xamarin Forms

Cross Platform Development using Xamarin Forms

Xamarin Forms is a platform for cross platform development using .NET as its base technology.

Raka Westu Mogandhi

April 15, 2016
Tweet

More Decks by Raka Westu Mogandhi

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. Xamarin Forms Project Structure • Shared Code • Contains business

    logic code (C#) • UI control (XAML) • Platform Specific Code • Platform specific resources • Platform specific libraries • Platform specific implementation (Custom UI, Native Interface Methods, etc.)
  4. How is the Xamarin Forms UI rendering works? Each Xamarin

    UI control will render its control into each platform native UI. For example:
  5. How is the Xamarin Forms UI rendering works? • TabbedPage

    • TabLayout in Android • UITabBar in iOS • StackLayout -> Stack container • LinearLayout in Android • StackView in iOS
  6. How is the Xamarin Forms UI rendering works? • Entry

    • EditText in Android • UITextField in iOS • Button -> Stack container • Button in Android • UIButton in iOS
  7. How is the Xamarin Forms UI rendering works? • Label

    • TextView in Android • UILabel in iOS • Image • ImageView in Android • UIImageView in iOS
  8. 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<Label> e) {
 base.OnElementChanged (e);
 var textView = (TextView)Control; 
 // Custom your TextView here
 }
 }
 }
  9. 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<Label> e)
 {
 base.OnElementChanged (e);
 var label = (UILabel) Control;
 // Customize your UILabel here
 }
 }
 }

  10. How to Call Platform specific Function? • Use DependencyService and

    Interface • Example: • In Shared Code • Interface • Call using DependencyService DependencyService.Get<IAnalyticsService>().TrackAppPage(pageName);
  11. 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
 
 }
 }
 

  12. 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