Top Banner
MVVM SESSION
14
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Mvvm basics

MVVM SESSION

Page 2: Mvvm basics

CONTENTS• Prerequisites

• What is MVVM?

• Elements of MVVM

• Architectural Diagram

• Why we need MVVM?

• What is INotifyPropertyChanged Interface?

• What is Icommand Interface?

• Pros & Cons

• Practical Implementation

• Supported Frameworks & References

Page 3: Mvvm basics

BASICS AND KEY POINTS TO KNOW

• Xaml

• Data Binding

• Key Concepts

1.observablecollection

2.INotifyPropertyChanged

3.ICommand

Page 4: Mvvm basics

MVVM(MODEL, VIEW, VIEWMODEL)

• is an architectural pattern created by John Gossman from WPF team

• is a variation of MVC pattern

• Also known as Presentation model

• Woven into WPF/SL

• WPF Data Binding & Commanding

Page 5: Mvvm basics

ELEMENTS OF MVVM

• Model : Business Logic

• View : UI logic

• ViewModel : Presentational Logic

Page 6: Mvvm basics

ARCHITECTURAL DIAGRAM

Page 7: Mvvm basics

NEED OF MVVM

To Avoid Glue Code

Page 8: Mvvm basics

RULES OF MVVM COMPLIANT APPLICATIONS

• Minimum Code Behind (Recommended No code Behind)

• Events as Commands

• ViewModel as DataContext

• Design View and ViewModel Independently

Page 9: Mvvm basics

INOTIFYPROPERTYCHANGED

•  any class that implements this interface, notifies any listeners when a property has changed. 

• Notifies clients that a property value has changed.

• To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.

Page 10: Mvvm basics

ICOMMAND

• Methods

1.CanExecute (Defines the method that determines whether the command can execute in its current state.)

2.Execute(Defines the method to be called when the command is invoked.)

• Events

1.CanExecuteChanged (Occurs when changes occur that affect whether or not the command should execute.)

Page 11: Mvvm basics

OBSERVABLECOLLECTION

An ObservableCollection is a dynamic collection of objects of a given type.

When an object is added to or removed from an observable collection, the UI is automatically updated.

WPF automatically adds a CollectionChanged event handler to the ObservableCollecion's events.

public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged

Page 12: Mvvm basics

PROS & CONS

• Cons

Ease of Maintainance

Ease of Testing

Flexibility

Re Usability

Low Degree of coupling

• Pros

Not Suitable for Simple Applications (Need best coding practices and standards)

Not a standardized one , Everyone follows their own flavours

Page 13: Mvvm basics

PRACTICAL IMPLEMENTATION

Page 14: Mvvm basics

SUPPORTED FRAMEWORKS AND REFERENCES• WPF Team : MVVM Toolkit• MS Pattern and Practice Team : Composite WPF (Prism)• Josh Smith. "MVVM Foundation"• Sacha Barber. "Cinch."• Karl Shifflett. "Ocean"• Laurent Bugnion. "MVVM Light Toolkit"• Lester Lobo. "CoreMVVM"• Rob Eisenberg. "Caliburn"• William e Kempf. "Onyx"• Peter O’Hanlon. "GoldLight"• jbe. "WPF Application Framework (WAF)"• Paul Stovel : MacroModelsReferences: http://www.codeproject.com/Articles/659614/MVVM-in-Depthhttp://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial (For Practical Implementation)