cross-platform mobile mit c#

31
Cross-Platform Mobile mit C#

Upload: zuehlke

Post on 15-Jan-2015

671 views

Category:

Technology


0 download

DESCRIPTION

Für Mobilgeräte gibt es unterschiedliche Ansätze, dieses Ziel zu erreichen. Im Rahmen dieses Vortrags wird der spannende Cross-Compile-Ansatz von Xamarin vorgestellt, der es ermöglicht, eine gemeinsame Codebasis für die wichtigsten Plattformen zu pflegen und trotzdem native Oberflächen zu verwenden. Mit Hilfe des MvvmCross-Frameworks soll während des Vortrags in Visual Studio eine App erstellt werden, die Code Sharing bis einschließlich ViewModel ermöglicht und trotzdem native Oberflächen für Android und iOS umsetzt. Präsentation auf der http://www.developer-week.de/ 2014

TRANSCRIPT

Page 1: Cross-Platform Mobile mit C#

Cross-Platform Mobile mit C#

Page 2: Cross-Platform Mobile mit C#

© Zühlke 2014

Nativ Cross-compiled Hybrid Web

Mobile Entwicklungslandschaft

Beste Usability? Überschaubare Kosten?

Page 3: Cross-Platform Mobile mit C#

Was ist Xamarin?

Page 4: Cross-Platform Mobile mit C#

© Zühlke 2014

Was ist Xamarin?

• Cross-Compiler auf Basis von Mono

• Plattformen – iOS – Android – Mac OS X

• Entwicklungsumgebung

• Partnerschaft mit Microsoft

Cross-Platform Mobile mit C# | Kerry W. Lothrop 16. Juli 2014

Page 5: Cross-Platform Mobile mit C#

© Zühlke 2014

Warum Xamarin?

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

• Nativ – Performance – User Interface – API

• Xamarin Studio (Mac, Windows) oder Visual Studio (Windows)

• Wiederverwendung von Code und Know-How

• Sehr gute Dokumentation

Cross-Platform Mobile mit C# | Kerry W. Lothrop 16. Juli 2014

Page 6: Cross-Platform Mobile mit C#

© Zühlke 2014

Cross-compiling

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

• (Fast) vollständiger Funktionsumfang von .NET 4.5

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

• Xamarin Scanner http://scan.xamarin.com

Cross-Platform Mobile mit C# | Kerry W. Lothrop 16. Juli 2014

Page 7: Cross-Platform Mobile mit C#

© Zühlke 2014

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

16. Juli 2014 Cross-Platform Mobile mit C# | Kerry W. Lothrop

.NET C# AOT iOS-API

.app ARM binary läuft nativ

Page 8: Cross-Platform Mobile mit C#

© Zühlke 2014

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

16. Juli 2014 Cross-Platform Mobile mit C# | Kerry W. Lothrop

.NET C# Android-API

.apk IL + JIT läuft nativ

kompilieren linken

Page 9: Cross-Platform Mobile mit C#

Native Libraries verwenden Bindings unter iOS und Android

Page 10: Cross-Platform Mobile mit C#

© Zühlke 2014

Bindings iOS

• Tool: Objective Sharpie

• Nacharbeit meist nötig

Cross-Platform Mobile mit C# | Kerry W. Lothrop 16. Juli 2014

.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

Page 11: Cross-Platform Mobile mit C#

© Zühlke 2014

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 16. Juli 2014

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

Page 12: Cross-Platform Mobile mit C#

© Zühlke 2014

Entwicklungsumgebung

Cross-Platform Mobile mit C# | Kerry W. Lothrop 16. Juli 2014

MacBook

Windows 8 (VM) Visual Studio 2013 ReSharper Xamarin (iOS &

Android)

OS X Xcode (SDK,

Simulatoren) Xamarin.iOS Xamarin Build Host

Page 13: Cross-Platform Mobile mit C#

Code Sharing

Page 14: Cross-Platform Mobile mit C#

© Zühlke 2014

Code Sharing Portable Class Libraries

• Sharing auf Binary-Ebene

• Eingeschränkter Funktionsumfang

Cross-Platform Mobile mit C# | Kerry W. Lothrop 16. Juli 2014

Page 15: Cross-Platform Mobile mit C#

© Zühlke 2014

Code Sharing Shared Projects

• Sharing auf Code-Ebene

• Compiler-Direktiven möglich (#if)

Cross-Platform Mobile mit C# | Kerry W. Lothrop 16. Juli 2014

Page 16: Cross-Platform Mobile mit C#

Architektur

Page 17: Cross-Platform Mobile mit C#

© Zühlke 2014

Architektur MVC

Cross-Platform Mobile mit C# | Kerry W. Lothrop 16. Juli 2014

View

Model Controller

Page 18: Cross-Platform Mobile mit C#

© Zühlke 2014

View

Architektur MVVM

Cross-Platform Mobile mit C# | Kerry W. Lothrop 16. Juli 2014

View Android

Model

ViewModel

plattformunabhängig

data binding

View iOS

View Windows

Phone

Page 19: Cross-Platform Mobile mit C#

MvvmCross

Page 20: Cross-Platform Mobile mit C#

© Zühlke 2014

MvvmCross Projekt

• Open Source MVVM Framework

• Data Binding

• Dependency Injection

• Lokalisierung

• Navigation

• Portable Class Libraries

• Plugins

• NuGet

• NinjaCoder

• Stuart Lodge Cross-Platform Mobile mit C# | Kerry W. Lothrop 16. Juli 2014

Page 21: Cross-Platform Mobile mit C#

© Zühlke 2014

MvvmCross Plattformen

Cross-Platform Mobile mit C# | Kerry W. Lothrop 16. Juli 2014

Page 22: Cross-Platform Mobile mit C#

© Zühlke 2014

MvvmCross Erweiterbarkeit

Cross-Platform Mobile mit C# | Kerry W. Lothrop 16. Juli 2014

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

View Android

Model

ViewModel

View iOS

View Windows

Phone

Windows-Phone-Dienst

Android-Dienst

iOS-Dienst

Page 23: Cross-Platform Mobile mit C#

© Zühlke 2014

MvvmCross Plugins

Cross-Platform Mobile mit C# | Kerry W. Lothrop 16. Juli 2014

Page 24: Cross-Platform Mobile mit C#

Demonstration

Page 25: Cross-Platform Mobile mit C#

User Interface

Page 26: Cross-Platform Mobile mit C#

© Zühlke 2014

User Interface Xamarin.Forms

Cross-Platform Mobile mit C# | Kerry W. Lothrop 16. Juli 2014

• 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>

Page 27: Cross-Platform Mobile mit C#

Demonstration

Page 28: Cross-Platform Mobile mit C#

Zusammenfassung

Page 29: Cross-Platform Mobile mit C#

© Zühlke 2014

Nativ Cross-compiled Hybrid Web

Mobile Entwicklungslandschaft

Beste Usability? Überschaubare Kosten?

Page 30: Cross-Platform Mobile mit C#

© Zühlke 2014

Zusammenfassung

Cross-Platform Mobile mit C# | Kerry W. Lothrop 16. Juli 2014

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

• Lizenzkosten lohnen sich oft schon bei nur einer Plattform

• Code, der während dieser Präsentation entstanden ist: https://github.com/lothrop/MvvmCrossDemo