35
Cross-Platform Mobile mit .NET

Cross-Platform Mobile mit C# - entwicklertag.de · • Kompilieren aus C# (oder F#) in nativen, plattformabhängigen Code • (Fast) vollständiger Funktionsumfang von .NET 4.6 und

Embed Size (px)

Citation preview

Cross-Platform Mobile mit .NET

© Zühlke 2016

Nativ Cross-compiled Hybrid Web

Mobile Entwicklungslandschaft

Beste Usability? Überschaubare Kosten?

Was ist Xamarin?

© Zühlke 2016

Was ist Xamarin?

• 2011 Gegründet

• aus dem Ximian, Novell und dem Mono-Projekt hervorgegangen

• wichtigste Produkte: – Xamarin Platform, ein Cross-Compiler auf Basis von Mono – Xamarin.Forms, eine plattformübergreifende XAML-basierte UI-Bibliothek – Xamarin Test Cloud, eine Lösung für UI-Tests auf einer Vielzahl von Geräten

• Februar 2016 von Microsoft aufgekauft

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

© Zühlke 2016

Was ist Xamarin Platform?

• Cross-Compiler

• Wrapper-Libraries um native Bibliotheken

• Plattformen – iOS – Android – Mac OS X

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

© Zühlke 2016

Warum Xamarin Platform?

• Cross-Platform – Gemeinsame Codebasis – Eine moderne Sprache (C#) für alle Plattformen

• Nativ – Performance – User Interface – API

• Wiederverwendung von Code und Know-How

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

© Zühlke 2016

Cross-compiling

• Kompilieren aus C# (oder F#) in nativen, plattformabhängigen Code

• (Fast) vollständiger Funktionsumfang von .NET 4.6 und C# 6

• Durch Integration von Teilen des Frameworks steigt die Paketgröße

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

© Zühlke 2016

Xamarin.iOS (früher MonoTouch)

• Ahead-of-time-Compiler direkt in ARM-Assembler-Code

• Keine dynamische Codegenerierung – System.Reflection.Emit fehlt – Keine Dynamic Language Runtime

10. März 2016 Cross-Platform Mobile mit C# | Kerry W. Lothrop

.NET C# AOT iOS-API

.app ARM binary läuft nativ

© Zühlke 2016

Xamarin.Android (früher Mono for Android)

• IL (Intermediate Language) und Mono-Runtime werden auf das Gerät deployed

• Just-in-time compiler

• Mono-Runtime läuft parallel zur Dalvik-VM

• Ahead-of-time-Kompilierung als Option möglich

10. März 2016 Cross-Platform Mobile mit C# | Kerry W. Lothrop

.NET C# Android-API

.apk IL + JIT läuft nativ

kompilieren linken

Native Libraries verwenden Bindings unter iOS und Android

© Zühlke 2016

Bindings iOS

• Tool: Objective Sharpie

• Nacharbeit meist nötig

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

.h

[BaseType(typeof(NSObject))] interface MyClass { // Method [Export("draw:atColumn:andRow:")] void Draw(string text, int column, int row); // Property [Export("menuVisible")] bool MenuVisible { [Bind("isMenuVisible")] get; set; } }

.cs

© Zühlke 2016

Bindings Android

• *.jar einfügen

• C# wird automatisch generiert (JNI)

• Transformationen müssen u.U. manuell ergänzt werden

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

<metadata> <attr path="/api/package[@name='com.nostra13.universalimageloader.cache.memory.impl'] /class[@name='FIFOLimitedMemoryCache'] /method[@name='removeNext']" name="managedReturn"> Java.Lang.Object </attr> <attr path="/api/package[@name='com.nostra13.universalimageloader.core.imageaware'] /class[@name='ImageViewAware'] /method[@name='getWrappedView']" name="managedReturn"> Android.Views.View </attr> </metadata>

© Zühlke 2016

Bindings C/C++

• Aus Android und iOS möglich

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

public static class MyClass { [DllImport("myLib")] int Calculate(int oneValue, int anotherValue); }

© Zühlke 2016

Entwicklungsumgebung

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

MacBook

Windows 10 (VM) Visual Studio 2015 ReSharper Xamarin (iOS &

Android)

OS X Xcode (SDK, Simulatoren) Xamarin.iOS

Code Sharing

© Zühlke 2016

Code Sharing Portable Class Libraries

• Sharing auf Binary-Ebene

• Eingeschränkter Funktionsumfang

• Nur andere PCLs als Referenzen

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

© Zühlke 2016

Code Sharing Shared Projects

• Sharing auf Code-Ebene

• Compiler-Direktiven möglich (#if)

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

Architektur

© Zühlke 2016

Architektur MVC

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

View

Model Controller

© Zühlke 2016

View

Architektur MVVM

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

View Android

Model

ViewModel

plattformunabhängig

data binding

View iOS

View Windows Phone

MvvmCross

© Zühlke 2016

MvvmCross Projekt

• Open Source MVVM Framework

• Data Binding

• Dependency Injection

• Lokalisierung

• Navigation

• Messenger

• Portable Class Libraries

• Plugins

• NuGet

• NinjaCoder

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

© Zühlke 2016

MvvmCross Plattformen

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

© Zühlke 2016

MvvmCross Erweiterbarkeit

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

interface IMvxPhoneCallTask { void MakePhoneCall( string name, string number); }

View Android

Model

ViewModel

View iOS

View Windows Phone

plattform-spezifischer Dienst

Windows-Phone-Dienst Android-Dienst iOS-Dienst

© Zühlke 2016

MvvmCross Plugins

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

Demonstration

Was gibt es noch?

© Zühlke 2016

User Interface Xamarin.Forms

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

• Code-Sharing für den UI-Code

• XAML

• Native User-Interfaces

• Mischen von Xamarin.Forms und plattformspezifischen Views möglich

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="ButtonXaml.ButtonXamlPage"> <Button Text="Tap for click count!" Clicked="OnButtonClicked" /> </ContentPage>

© Zühlke 2016

User Interface Xamarin Inspector

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

© Zühlke 2016

User Interface Xamarin Test Cloud

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

Zusammenfassung

© Zühlke 2016

Nativ Cross-compiled Hybrid Web

Mobile Entwicklungslandschaft

Beste Usability? Überschaubare Kosten?

© Zühlke 2016

Zusammenfassung

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

• Cross-Platform für alle wichtigen Endgeräte ohne JavaScript

• Native Performance und Möglichkeiten

• Mit bekannten Tools mehr Plattformen erreichen

• Die jeweilige plattformspezifische UI-Technologie muss trotzdem erlernt werden.

• Ein breites Tooling unterstützt die Entwicklung

© Zühlke 2016

Fragen

Cross-Platform Mobile mit C# | Kerry W. Lothrop 10. März 2016

Kerry W. Lothrop kerry.lothrop.de @kwlothrop