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

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

Embed Size (px)

Citation preview

Page 1: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

| Basel

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

Page 2: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

Thomas Claudius Huber

Principal Consultat bei Trivadiswww.trivadis.comwww.thomasclaudiushuber.comTrainer, Coach, Developer, Architect

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

Autor umfassender HandbücherWPF, Windows Store Apps und Silverlight

Page 3: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

Agenda

Sessioninhalt

MVVM - GrundlagenPortable Class LibrariesTipps & Tricks

Page 4: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

| Basel

MVVM-Grundlagen

Page 5: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

MVVM - Grundlagen

ViewDas UI (XAML)

ViewModelModel für das UIEnthält Daten & Commands

ModelDatenobjekte

Model

ViewModel

View

Data Binding

Referenz

Page 6: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

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: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

Wichtige Klassen und Interfaces

ICommandErlaubt dem UI das Ausführen von Logik im ViewModelBeim 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: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

Eine kleine MVVM-Phone-App

Demo

Page 9: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

Neuerungen in .NET 4.5

CallerMemberName-Attribut wurde eingeführtFür optionale ParameterDer Compiler fügt den Namen des Aufrufers automatisch ein, wenn dieser nicht angegeben wirdprotected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null){ var handler = PropertyChanged; if (handler != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); }}

Demo

Page 10: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

Agenda

Sessioninhalt

MVVM - GrundlagenPortable Class LibrariesTipps & Tricks

Page 11: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

| Basel

Portable Class Libraries

Page 12: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

.NET Frameworks.NET Framework 4.5

Silverlight 5.NET für WindowsStore Apps

.NET für

Windows

Phone

Page 13: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

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: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

Portable Class Library Feature MatrixFeature .NET Framework

4.5Windows Store

Windows Phone 8

Silverlight 5

Core ü ü ü üLINQ ü ü ü üIQueryable ü ü ü üMEF ü ü üSerialization ü ü ü üWCF ü ü ü üMVVM ü ü ü üDynamic Keyword

ü ü ü

...

Page 15: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

Portable Libraries und MVVM

ViewDas UI (XAML)

ViewModelModel für das UIEnthält Daten & Commands

ModelDatenobjekte

Model

ViewModel

View

Data Binding

Referenz

Plattform-spezifisch

PortableClass Library

Page 16: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

ViewModel und Model in Portable Class Library migrieren

Demo

Page 17: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

Portable Class Library in Windows Store App nutzen

Demo

Page 18: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

Agenda

Sessioninhalt

MVVM - GrundlagenPortable Class LibrariesTipps & Tricks

Page 19: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

| Basel

Tipps & Tricks

Page 20: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

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: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

Abstractions in Portable Library definieren und in Apps implementieren

Demo

Page 22: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

Portable Class Library

Abstractions

Model

ViewModel

ITodoItemDataProvider

Windows Store App

View

ITodoItemDataProvider-Imp.

Windows Phone App

View

ITodoItemDataProvider-Imp

ReferenzReferenz

Page 23: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

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-/ausschaltenJede Plattform hat sog. Conditional Compilation Symbols: Windows_Phone, NETFX_CORE (Windows Store App)

Demo

Page 24: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

Portable Class Library

Linked Files

Model

ViewModel

ITodoItemDataProvider

Windows Store App

View

Link

Windows Phone App

View

ITodoItemDataProvider-Imp

ReferenzReferenz

Page 25: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

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: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

Agenda

Sessioninhalt

MVVM - GrundlagenPortable Class LibrariesTipps & Tricks

Page 27: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

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: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

Next Session

Weitere Infos zur WinPRTund Portable Class Libraries in der Session17.00 Uhr: Entwickeln von Windows Phone Unternehmens-Apps Raum: San Francisco

Page 29: | Basel MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG

Tolle Preise am Trivadis Stand

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

© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a

commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

© 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a

commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Slides/Samples unter:

www.thomasclaudiushuber.com/talks.php

Twitter: @thomasclaudiushMail: [email protected]