MVVM in Windows 8 und Windows Phone 8 Thomas Claudius Huber Trivadis AG
Thomas Claudius Huber Principal Consultat bei Trivadis www.trivadis.com www.thomasclaudiushuber.com Trainer, Coach, Developer, Architect Spezialisiert auf XAML, WPF, UI Microsoft MVP im Bereich Client Development Autor umfassender Handbücher WPF, Windows Store Apps und Silverlight
Sessioninhalt MVVM - Grundlagen Portable Class Libraries Tipps & Tricks
MVVM-Grundlagen
MVVM - Grundlagen View ViewModel Model View Data Binding ViewModel Das UI (XAML) ViewModel Model für das UI Enthält Daten & Commands Model Datenobjekte View Data Binding ViewModel Referenz Model
Stärken von MVVM Höhere Wartbarkeit Unterstütztung von Unit-Tests UI und UI-Logik sind strikt getrennt Unterstütztung von Unit-Tests ViewModels und die darin enthaltene UI-Logik lassen sich testen Die View lässt sich einfach austauschen
Wichtige Klassen und Interfaces ICommand Erlaubt dem UI das Ausführen von Logik im ViewModel Beim MVVM-Pattern kommt eine spezielle Implementierung zum Einsatz Oft als DelegateCommand bezeichnet INotifyPropertyChanged Definiert das PropertyChanged-Event Darüber wird das UI über Änderungen von Properties einer Klasse informiert ObservableCollection<T> für Datensammlungen Implementiert das INotifyCollectionChanged-Interface Dieses benachrichtigt das UI bei einer Änderung der Collection
Eine kleine MVVM-Phone-App Demo
Neuerungen in .NET 4.5 CallerMemberName-Attribut wurde eingeführt Demo Fü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
Sessioninhalt MVVM - Grundlagen Portable Class Libraries Tipps & Tricks
Portable Class Libraries
.NET Frameworks .NET Framework 4.5 Silverlight 5 .NET für Windows Phone .NET für Windows Store Apps Silverlight 5
Portable Class Library Neue Projekt-Vorlage Vefügbar ab Visual Studio 2012 Referenzierbar in verschiedenen Projekten .NET, Windows Store Apps, Windows Phone, Silvelright Vereinfacht eine gemeinsame Codebasis Demo
Portable Class Library Feature Matrix .NET Framework 4.5 Windows Store Windows Phone 8 Silverlight 5 Core ü LINQ IQueryable MEF Serialization WCF MVVM Dynamic Keyword ...
Portable Libraries und MVVM View Das UI (XAML) ViewModel Model für das UI Enthält Daten & Commands Model Datenobjekte View Plattform -spezifisch Data Binding ViewModel Referenz Portable Class Library Model
ViewModel und Model in Portable Class Library migrieren Demo
Portable Class Library in Windows Store App nutzen Demo
Sessioninhalt MVVM - Grundlagen Portable Class Libraries Tipps & Tricks
Tipps & Tricks
Abstractions Windows Store App Windows Phone App View View Plattform-spezifische Impl. Plattform-spezifische Impl. Portable Class Library Referenz Referenz ViewModel Model Plattform Abstraktion
Abstractions in Portable Library definieren und in Apps implementieren Demo
Abstractions Windows Store App Windows Phone App View View ITodoItemDataProvider-Imp. ITodoItemDataProvider-Imp Portable Class Library Referenz Referenz ViewModel Model ITodoItemDataProvider
Dateien als Link referenzieren WinRT und WinPRT haben gemeinsame Basis Somit 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 arbeiten So lässt sich plattform-spezifischer Code ein-/ausschalten Jede Plattform hat sog. Conditional Compilation Symbols: Windows_Phone, NETFX_CORE (Windows Store App) Demo
ITodoItemDataProvider-Imp Portable Class Library ITodoItemDataProvider Linked Files Windows Store App Windows Phone App View View Link ITodoItemDataProvider-Imp Portable Class Library Referenz Referenz ViewModel Model ITodoItemDataProvider
Weitere Optimierungen Service Locator in Portable Class Library Dependency Injection Bspw. gibt es das Framework Autofac als Portable Class Library public static class ServiceLocator { public static ITodoItemDataProvider TodoItemDataProvider { get; set; } ... }
Sessioninhalt MVVM - Grundlagen Portable Class Libraries Tipps & Tricks
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 Abstraktionen Interface in Portable Class Library, Implementierung je Plattform WinRT und WinPRT über verlinkte Dateien
Next Session Weitere Infos zur WinPRT und Portable Class Libraries in der Session 17.00 Uhr: Entwickeln von Windows Phone Unternehmens-Apps Raum: San Francisco
Tolle Preise am Trivadis Stand
www.thomasclaudiushuber.com/talks.php Slides/Samples unter: 3/28/2017 8:12 PM Slides/Samples unter: www.thomasclaudiushuber.com/talks.php Twitter: @thomasclaudiush Mail: thomas.huber@trivadis.com © 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. © 2010 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.