mvvm in windows 8 und windows phone 8 - thomas cl. huber · 2015-01-16 · mvvm in windows 8 und...

30
| Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

Upload: others

Post on 01-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

| Basel

MVVM in Windows 8 und Windows Phone 8Thomas Claudius HuberTrivadis AG

Page 2: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Thomas Claudius Huber

Principal Consultat bei Trivadiswww.trivadis.com

www.thomasclaudiushuber.com

Trainer, Coach, Developer, Architect

Spezialisiert auf XAML, WPF, UIMicrosoft MVP im Bereich Client Development

Autor umfassender HandbücherWPF, Windows Store Apps und Silverlight

Page 3: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Agenda

Sessioninhalt

MVVM - Grundlagen

Portable Class Libraries

Tipps & Tricks

Page 4: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

| Basel

MVVM-Grundlagen

Page 5: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

MVVM - Grundlagen

ViewDas UI (XAML)

ViewModelModel für das UIEnthält Daten & Commands

ModelDatenobjekte

Model

ViewModel

View

Data Binding

Referenz

Page 6: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Stärken von MVVM

Höhere WartbarkeitUI und UI-Logik sind strikt getrennt

Unterstütztung von Unit-TestsViewModels und die darin enthaltene UI-Logik lassen sich testen

Die View lässt sich einfach austauschen

Page 7: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Wichtige Klassen und Interfaces

ICommandErlaubt dem UI das Ausführen von Logik im ViewModel

Beim MVVM-Pattern kommt eine spezielle Implementierung zum Einsatz

Oft als DelegateCommand bezeichnet

INotifyPropertyChangedDefiniert das PropertyChanged-Event

Darüber wird das UI über Änderungen von Properties einer Klasse informiert

ObservableCollection<T> für DatensammlungenImplementiert das INotifyCollectionChanged-Interface

Dieses benachrichtigt das UI bei einer Änderung der Collection

Page 8: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Eine kleine MVVM-Phone-App

Demo

Page 9: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Neuerungen in .NET 4.5

CallerMemberName-Attribut wurde eingeführtFür optionale Parameter

Der Compiler fügt den Namen des Aufrufers automatisch ein, wenn dieser nicht angegeben wird

protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)

{

var handler = PropertyChanged;

if (handler != null)

{

PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

}

}

Demo

Page 10: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Agenda

Sessioninhalt

MVVM - Grundlagen

Portable Class Libraries

Tipps & Tricks

Page 11: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

| Basel

Portable Class Libraries

Page 12: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

.NET Frameworks

.NET Framework 4.5

Silverlight 5.NET für Windows

Store Apps

.NET für

Windows

Phone

Page 13: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Portable Class Library

Neue Projekt-VorlageVefügbar ab Visual Studio 2012

Referenzierbar in verschiedenen Projekten.NET, Windows Store Apps, Windows Phone, Silvelright

Vereinfacht eine gemeinsame Codebasis

Demo

Page 14: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Portable Class Library Feature MatrixFeature .NET Framework 4.5 Windows Store Windows Phone 8 Silverlight 5

Core

LINQ

IQueryable

MEF

Serialization

WCF

MVVM

Dynamic Keyword

...

Page 15: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Portable Libraries und MVVM

ViewDas UI (XAML)

ViewModelModel für das UIEnthält Daten & Commands

ModelDatenobjekte

Model

ViewModel

View

Data Binding

Referenz

Plattform

-spezifisch

Portable

Class Library

Page 16: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

ViewModel und Model in Portable Class Library migrieren

Demo

Page 17: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Portable Class Library in Windows Store App nutzen

Demo

Page 18: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Agenda

Sessioninhalt

MVVM - Grundlagen

Portable Class Libraries

Tipps & Tricks

Page 19: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

| Basel

Tipps & Tricks

Page 20: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Portable Class Library

Abstractions

Model

ViewModel

Plattform Abstraktion

Windows Store App

View

Plattform-spezifische Impl.

Windows Phone App

View

Plattform-spezifische Impl.

ReferenzReferenz

Page 21: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Abstractions in Portable Library definieren und in Apps implementieren

Demo

Page 22: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Portable Class Library

Abstractions

Model

ViewModel

ITodoItemDataProvider

Windows Store App

View

ITodoItemDataProvider-Imp.

Windows Phone App

View

ITodoItemDataProvider-Imp

ReferenzReferenz

Page 23: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Dateien als Link referenzieren

WinRT und WinPRT haben gemeinsame BasisSomit lässt sich Code aus Windows Phone 8 in einer Windows Store App wiederverwenden

WinRT und WinPRT sind aber kein .NET!!!Somit sind die Gemeinsamkeiten in Portable Class Libraries nicht verfügbar

Diese unterstützten nur .NET Schnittmengen

In verlinkten Dateien optional mit Compiler-Direktiven wie #if arbeitenSo lässt sich plattform-spezifischer Code ein-/ausschalten

Jede Plattform hat sog. Conditional Compilation Symbols:

Windows_Phone, NETFX_CORE (Windows Store App) Demo

Page 24: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Portable Class Library

Linked Files

Model

ViewModel

ITodoItemDataProvider

Windows Store App

View

Link

Windows Phone App

View

ITodoItemDataProvider-Imp

ReferenzReferenz

Page 25: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Weitere Optimierungen

Service Locator in Portable Class Library

Dependency InjectionBspw. gibt es das Framework Autofac als Portable Class Library

public static class ServiceLocator

{

public static ITodoItemDataProvider TodoItemDataProvider { get; set; }

...

}

Page 26: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Agenda

Sessioninhalt

MVVM - Grundlagen

Portable Class Libraries

Tipps & Tricks

Page 27: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Fazit

ViewModels und Models lassen sich mit Portable Class Libraries in verschiedensten Projekten nutzen.NET, Windows Phone, Windows Store App und Silverlight

Plattform-spezifische Logik mit AbstraktionenInterface in Portable Class Library, Implementierung je Plattform

WinRT und WinPRT über verlinkte Dateien

Page 28: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Next Session

Weitere Infos zur WinPRTund Portable Class Libraries in der Session

17.00 Uhr: Entwickeln von Windows Phone Unternehmens-Apps

Raum: San Francisco

Page 29: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Tolle Preise am Trivadis Stand

Page 30: MVVM in Windows 8 und Windows Phone 8 - Thomas Cl. Huber · 2015-01-16 · MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber TrivadisAG. Thomas Claudius Huber Principal

Slides/Samples unter:

www.thomasclaudiushuber.com/talks.php

Twitter: @thomasclaudiush

Mail: [email protected]