Windows 8 apps and the MVVM pattern SILVERLIGHTSHOW.NET WEBINARS SERIES GILL CLEEREN, October 16 th 2012 -

Slides:



Advertisements
Similar presentations
MVVM Overview Frank Shoemaker MindCrafted Systems
Advertisements

Module 1: Creating an Application by Using Windows Presentation Foundation Overview of WPF Creating a Simple WPF Application Handling Events and Commands.
Expression Blend 4 – deep dive
Understanding the MVVM pattern
MVVM: Filling the Pattern Gap in Silverlight Application Development Bart McDonough Principal Consultant Incline Technical Group.
USING THE MODEL-VIEW- VIEWMODEL PATTERN Laurent Bugnion Director, User Experience Integration IdentityMine
Lessons learned from developing a Windows 8 Metro application in C# Frode Nilsen Nilsen Labs Ticki.
An Introduction to the Model-View-ViewModel Pattern Srsly? Another MV* Pattern? Srsly? Another MV* Pattern?
Dinko Jakovljević Microsoft Student Partner | BambooLab
Essentials of Developing Windows Phone Apps Chinthaka Dissanayake Tech Lead Exilesoft.
Or How I Learned to Stop Worrying and Love the Binding Bryan Anderson.
Evan Hutnick Session Roadmap  Introduction  Silverlight Foundation  The Tools of Silverlight  Layout  Databinding  Data.
An Introduction To Silverlight Gergely Orosz
Laurent Bugnion Senior User Experience Integrator IdentityMine
WPF MVVM Training Michael Sync (Silverlight MVP).
Real world Windows Phone development Igor
Building Silverlight Applications Using the MVVM pattern An Introduction by Peter Messenger Senior Developer – Qmastor
Chapter 26 Applying Gang of Four Design Patterns 1CS6359 Fall 2012 John Cole.

© 2014 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg.
CIS 375—Web App Dev II ASP.NET 2 Introducing Web Forms.
Building Data Driven Applications Using WinRT and XAML Sergey Barskiy, Magenic Microsoft MVP – Data Platform Principal Consultant Level: Intermediate.
.NET Database Technologies: Introduction to WPF and Entity Framework DataBinding.
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
Todd Snyder Development Team Lead Infragistics Experience Design Group.
Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no.
Todd Kitta  Covenant Technology Partners  Professional Windows Workflow Foundation.
Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.
Building an Offline Smart Client using Domain-Driven Design Principles Tim McCarthy.
Getting Started with Caliburn.Micro and Windows Phone 7 Gary Ewan Park Twitter: Blog:
V 1.0 Programming III. Automatic notifications (…Changed, INofityPropertyChanged, ObservableCollection ) Data formatters Data conversions Resources.
Windows Phone MVVM and Unit Testing Step by Step Andy Wigley Windows Phone Development MVP, Mobile Software Consultant APPA Mundi Ltd WPH208.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Object Oriented Software Development 10. Persistent Storage.
Observer design pattern A closer look at INotifyPropertyChanged, INotifyPropertyChanging and ObservableCollection Observer design pattern1.
Nikhil Kothari Software Architect Microsoft Corporation Session Code: WUX312.
Windows 10 UWP MVVM In Depth
1 Catching up on Rich Clients (round 1) Mike Ormond, Mike Taulty Developer & Platform Group Microsoft Ltd
04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga
V 1.0 Programming III. Automatic notifications with data binding (…Changed, INofityPropertyChanged, ObservableCollection, DataTemplate) Data formatters.
V 1.0 Programming III. XAML Data Binding I.. V 1.0ÓE-NIK, 2014 XAML namespaces Namespaces define the allowed tags and attributes The one without the prefix.
Model View ViewModel Architecture. MVVM Architecture components.
Developing for Windows 8/WinRT Session 10 Kevin Stumpf Introduction to Interknowlogy’s WinRT MVVM Framework.
Modern Development Technologies in SharePoint SHAREPOINT SATURDAY OMAHA APRIL, 2016.
MVVM Pattern Mahender Senior Software Engineer United Health Group.
Programming with MVVM Miguel A. Castro Architect -
Principled N-Tier Design or, a Solution to the Solution Problem Steve | ardalis.com Telerik, Inc.
/DEV/TM #1 Building Cross-Platform Apps with Xamarin and MvvmCross Flavius-Radu DEMIAN.
Igor Ralić igrali.com Open source u razvoju Windows Phone aplikacija.
Data Binding, Binding Properties Doncho Minkov Telerik School Academy Technical Trainer
Adam Schultz MVVM and WPF. MVVM Model, View, ViewModel A software architecture designed to separate out User Interface design, Business Logic, and Data.
In Windows 8 Store Applications
Real world Windows Phone development
Introduction to .NET Florin Olariu
MVVM Made Simple with Prism
Better Unit Tests through Design Patterns: Repository, Adapter, Mocks, and more… Benjamin
Software Architecture & Difference from Design
Ben Riga 02 | Basics of View Models Ben Riga
Build Windows 10 UWP MVVM Apps with Prism
It’s a Knockout! MVVM Style Web Development
Jim Fawcett CSE775 – Distributed Objects Spring 2011
Miguel A. Castro Architect IDesign
Introduction to the MVVM Pattern
An introduction to MVVM using WPF NISCHAL S
GoF Design Patterns (Ch. 26)
Build data-driven collection and list apps using ListView in HTML5
XAML Deep Dive for Windows & Windows Phone Apps Jump Start
Architecting Silverlight Applications with MVVM
Tech Ed North America /6/2019 2:07 PM Required Slide
An Introduction to the Model-View-ViewModel Pattern
Presentation transcript:

Windows 8 apps and the MVVM pattern SILVERLIGHTSHOW.NET WEBINARS SERIES GILL CLEEREN, October 16 th

Agenda Core concepts – Data binding in Windows 8 – Data templates – INotifyPropertyChanged & INotifyCollectionChanged The MVVM pattern – What is MVVM – Why and why not MVVM – Parts of MVVM MVVM Light introduction Linking view and viewmodel Commanding – EventToCommand? Messaging Navigation and other servicing Contracts and MVVM

CORE CONCEPTS

DATABINDING

Data binding explained Data + binding Data: properties of objects or collections thereof Binding: linking these to properties of controls Infrastructure for binding control properties to objects and collections “ Loosely coupled model Bound control doesn’t need to know to what is being bound to Databinding exists in Silverlight WPF ASP.NET Windows Forms Windows 8 5

Data binding explained A binding is defined by 4 items: Source object Source property Target object (must be dependency object) Target property (must be dependency property) Data binding works on in-memory data Coming from a service possibly 6

Data binding What is supported in Windows 8? – Regular data binding – Element binding – Converters – Model binding – Indexers – Data templating – Binding to a CollectionViewSource – Notifications Single objects Collections – Binding to JSON collections WCF service – Exceptions in output window

That means... No StringFormat, FallbackValue,... No custom markup extensions No implicit data templates No data binding breakpoints... More or less at the level of Silverlight 3

SIMPLE DATA BINDING DEMO

Data templates Block of XAML that is generated for each item of the source list Mostly used on List controls such as the GridView or ListView Uses regular data binding syntax – Source for binding is the individual item Windows 8 doesn’t know – AncestorType/AncestorLevel for RelativeSource Binding – Implicit data templates

DATA TEMPLATES DEMO

INotifyPropertyChanged Synchronization doesn’t work automagically! Source needs to implement INotifyPropertyChanged If so, source update will result in target update TwoWay enables bidirectional flow of data 12 namespace System.ComponentModel { // Summary: // Notifies clients that a property value has changed. public interface INotifyPropertyChanged { // Summary: // Occurs when a property value changes. event PropertyChangedEventHandler PropertyChanged; }

INotifyPropertyChanged PropertyChanged is to be raised when value of the property changes Only works when binding is set to OneWay or TwoWay Won’t work if binding is set to OneTime Windows 8 checks item being bound If it implements the interface, it will start listening for events and update UI accordingly If not, it will not react to eventual changes of the source 13

INotifyCollectionChanged Also exists for collections in form of INotifyCollectionChanged interface ObservableCollection already implements this Update will only show updates of collection, not indivual items Sync therefore does not work on List ObservableVector also works This is a specific WinRT type 14

INOTIFYPROPERTYCHANGED AND INOTIFYCOLLECTIONCHANGED DEMO

THE MVVM PATTERN

What is MVVM? MVVM : – is an architectural pattern created by John Gossman from WPF team – is a variation of MVC pattern – is similar to Martin Fowler’s PresentationModel pattern – works because of XAML data Binding & commanding

What is MVVM?

Understanding MVVM V is part for the designer M is for the developer VM glues both these objects together – V can bind to properties of VM – VM remodels data so it is usable for the V

Why MVVM Better SoC (Seperation of Concerns) More maintainable – Model never needs to be changed to support changes to the view – ViewModel rarely needs to be changed to support changes to the view – Code is easier to find More testable – ViewModel is easier to unit test than code-behind or event driven code Eliminates the need to do code behind which leaves the UI all in XAML Because the framework have the power to support it – Databinding/DataContext/Observable pattern

Why not MVVM Lack of standardization so everyone has own favor Windows 8 Grid template has its own “MVVM” model Message to community is not clear!! For simple UI, M-V-VM can be overkill Too much code needed – INotifyPropertyChanged – Commands

Everything in code-behind Data Model View XAML Code-Behind Event Handlers

Model – View – ViewModel Data Model View XAML Code-Behind View Model State + Operations Change notification Data-binding and commands

The parts of MVVM View knows ViewModel ViewModel knows Models But not vice versa. ViewViewModelModel

The View The view – represents the user interface that the user will see. – can be a user control or Data Template – shouldn't contain any logic that you want to test – should be kept as simple as possible.

The ViewModel An abstraction of View Connector between View and Model Keep View State, Value Conversion No strong or weak (via Interface) reference of View Make VM as testable as possible (e.g. no call to Singleton class) No Control related Stuff in VM

The Model Can be Data Model, DTO, POCO, auto- generated proxy of domain class and UI Model based on how you want to have the separation between Domain Service and Presentation Layer No reference to ViewModel

MVVM LIGHT INTRODUCTION

MVVM Light Open-source project by Laurent Bugnion – Light-weight, base classes for MVVM implementation – Not a real framework – Contains base implementation for ViewModel, Messaging, Locator…

Most important classes of MVVM Light ViewModel – ViewModelBase Commands – RelayCommand – EventToCommand Messaging – Messenger – Several types of messages IOC – SimpleIOC

LOOKING AT A SAMPLE MVVM IMPLEMENTATION DEMO

Linking the View and the ViewModel A view needs to “find” its ViewModel – ViewModel is the DataContext 2 options: – View First – ViewModel First

Locator pattern Implemented through a class that contains all VMs as properties An instance is then made available as Resource All Views can bind, no code needed in View – Clean way – Not good since all VMs need to be known upfront Property for each available VM Not easy if more than one instance exists of a View – In this case, some form of IOC is recommended

THE LOCATOR PATTERN DEMO

COMMANDING

Commands MVVM-based code have no event handlers in code-behind How to handle events happening in the UI?  Commands WinRT has the ICommand interface – Execute() – CanExecute() – CanExecuteChanged event

Commands Way to create commands: – Write ICommand implementation – Create instance on VM – Bind Command property of control to this instance Works only on some controls For others, we need to use EventToCommand class – Allows linking of an event with a Command on the ViewModel – … Oops, it’s not available in WinRT!!!!

Using EventToCommand as a behavior Take a look at WinRTBehaviors on CodePlex <Button x:Name="btnSubmit" Content="Submit" FontFamily="{StaticResource MyFont}" FontSize="{StaticResource MyFontSize}" HorizontalAlignment="Center" Margin="0,20,0,0"> <Win8nl_Behavior:EventToCommandBehavior Event="Tapped" Command="AreYouSureCommand" CommandParameter="{Binding MyName}"/>

COMMANDING DEMO

MESSAGING

Communication between VMs View Model

Messaging Data Model View XAML Code- Behind Data Model View XAML Code- Behind Message View Model State + Operations View Model State + Operations View XAML Code- Behind Message Event Aggregator View Model State + Operations Publish messages Subscribe to messages

Communication between VMs VM’s need to be able to talk to each other, eg: send messages to each other – Not a good idea to have each VM reference all other VMs Solution: – Event Aggregator/mediator/messenger – VM can register to receive messages of a certain type (for example string messages) – Another VM can register with the same messenger to send messages – This allows both VMs to communicate with each other without tight coupling

MESSAGING DEMO

NAVIGATION, DIALOGS AND OTHER SERVICING

External servicing Many tasks remain open – Don’t belong in View – Don’t belong in ViewModel If this is the case, they should be an separate service – Navigation – Showing a dialog, error… – Loading data into the model – …

External servicing An external service is nothing more than a class that does ONE specific job – Remember the SOC principle An instance of these classes is often created application-wide – An IOC is recommended for this task

NAVIGATION, DIALOGS AND OTHER SERVICING DEMO

WINDOWS 8 SPECIFICS

Contracts Sharing – Allows apps to share information with other apps – Mediator pattern Search – Creates a unified way of searching in the content of the app

Contracts in MVVM Using contracts from MVVM – Service-type solution – Event-based model – Works by calling from ViewModel without problems

ADDING THE SHARE CONTRACT USING MVVM DEMO

Summary MVVM allows us to build more maintainable Windows 8 apps Not very different from MVVM in Silverlight, WP7 or WPF MVVM Light is a solid base

THANKS!

Q&A

Windows 8 apps and the MVVM pattern SILVERLIGHTSHOW.NET WEBINARS SERIES GILL CLEEREN, October 16 th