Top Banner
Reed Copsey, Jr. C Tech Development Corporation Blog - http://reedcopsey.com Twitter - http://twitter.com/ReedCopsey
23

Windows Forms to WPF with MVVM - Reed Copsey, Jr

Feb 11, 2022

Download

Documents

dariahiddleston
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: Windows Forms to WPF with MVVM - Reed Copsey, Jr

Reed Copsey, Jr.

C Tech Development Corporation

Blog - http://reedcopsey.com

Twitter - http://twitter.com/ReedCopsey

Page 2: Windows Forms to WPF with MVVM - Reed Copsey, Jr

Graphical User Interfaces

Exists for Usability

User Experience is Entire Purpose

Works on data

Display

Editing

Copyright © 2010 by Reed Copsey, Jr.

Page 3: Windows Forms to WPF with MVVM - Reed Copsey, Jr

Developing a GUI: The Model

Domain Specific Data and Logic

Core of any program

Should be unaware of User Interface

Copyright © 2010 by Reed Copsey, Jr.

Page 4: Windows Forms to WPF with MVVM - Reed Copsey, Jr

DEMO:

Page 5: Windows Forms to WPF with MVVM - Reed Copsey, Jr

Windows Forms

Based on wrapping Windows API

Each control has separate Window Handle

Event Driven API

Original Client GUI Technology for .NET

Basically unchanged since .NET 2.0

Minor changes since .NET 1.1

Familiar API

Copyright © 2010 by Reed Copsey, Jr.

Page 6: Windows Forms to WPF with MVVM - Reed Copsey, Jr

DEMO:

Page 7: Windows Forms to WPF with MVVM - Reed Copsey, Jr

Windows Forms: Problems

Customization is tricky

Custom look and feel requires custom

control or manual drawing

Custom drawn via GDI+

Resolution dependence

Performance/Resource Usage

No new Development after 2005

Copyright © 2010 by Reed Copsey, Jr.

Page 8: Windows Forms to WPF with MVVM - Reed Copsey, Jr

Windows Presentation

Foundation (WPF)

Released in .NET 3.0, Improved in .NET

3.5

Actively developed - .NET 4 features many

improvements

Easy customization

Better Developer and Design Story

Extensive Framework

2D and 3D graphics, media support, etc.

Copyright © 2010 by Reed Copsey, Jr.

Page 9: Windows Forms to WPF with MVVM - Reed Copsey, Jr

Windows Presentation

Foundation (WPF)

Can be used like Windows Forms

Event-Driven API still works

Required Learning

Different Designer

Changes in Layout System

Copyright © 2010 by Reed Copsey, Jr.

Page 10: Windows Forms to WPF with MVVM - Reed Copsey, Jr

DEMO:

Page 11: Windows Forms to WPF with MVVM - Reed Copsey, Jr

Don’t do this!

Event Driven API has issues

Leads to tight coupling of logic to user

interface

Spaghetti code

Better alternatives, even in WinForms

MVC

MVP

User Interface Process Application Block

○ 134 Page Guidance on avoiding spaghetti

Copyright © 2010 by Reed Copsey, Jr.

Page 12: Windows Forms to WPF with MVVM - Reed Copsey, Jr

DataBinding in WPF

Effective DataBinding Requires:

Simple to bind user interface element to

data source

Clean way to specify data source for a

collection of objects

Consistency in binding specification

Flexibility in what triggers updates

Validation

Conversion in binding itself

Copyright © 2010 by Reed Copsey, Jr.

Page 13: Windows Forms to WPF with MVVM - Reed Copsey, Jr

DataContext

Can be any object of any type

INotifyPropertyChanged

INotifyCollectionChanged

Propagates down Visual Tree

Allows all controls in Window to share

DataContext automatically

Copyright © 2010 by Reed Copsey, Jr.

Page 14: Windows Forms to WPF with MVVM - Reed Copsey, Jr

DEMO:

Page 15: Windows Forms to WPF with MVVM - Reed Copsey, Jr

Templating

Control Behavior separated from Visual

Representation

Control “look” can be changed without

changing code

Allows for improved designability

DataTemplate

Provide custom “look” for any type

Basic means of customization

Copyright © 2010 by Reed Copsey, Jr.

Page 16: Windows Forms to WPF with MVVM - Reed Copsey, Jr

DEMO:

Page 17: Windows Forms to WPF with MVVM - Reed Copsey, Jr

Commands

ICommand

○ Execute: Executes the command

○ CanExecute: Returns boolean

Can implement using delegates

Invoked via DataBinding from multiple

Sources

Buttons

Gestures

MenuItems

Copyright © 2010 by Reed Copsey, Jr.

Page 18: Windows Forms to WPF with MVVM - Reed Copsey, Jr

DEMO:

Page 19: Windows Forms to WPF with MVVM - Reed Copsey, Jr

Model-View-ViewModel

Architectural Pattern

Model Domain Specific Data and Logic

Isolated

ViewModel Works with Model, exposing it for DataBinding

Manages Application Specific Work

View Ideally 100% XAML

“Hooks” into ViewModel via DataBinding

Copyright © 2010 by Reed Copsey, Jr.

Page 20: Windows Forms to WPF with MVVM - Reed Copsey, Jr

Model-View-ViewModel

View 100% Declarative via XAML

ViewModel Exposes Model to View via DataBinding

Exposes Logic via ICommand

Model Domain Specific Entities and Logic

No Knowledge of Specific Usage

DataBinding to Properties DataBinding to Commands

Direct Access in Code

Copyright © 2010 by Reed Copsey, Jr.

Page 21: Windows Forms to WPF with MVVM - Reed Copsey, Jr

MVVM Advantages

Flexible View – Redesign without code

changes

Testability

Easy testing in ViewModel

Clean Separation of Concerns

Copyright © 2010 by Reed Copsey, Jr.

Page 22: Windows Forms to WPF with MVVM - Reed Copsey, Jr

DEMO:

Page 23: Windows Forms to WPF with MVVM - Reed Copsey, Jr

Online Resources

http://WindowsClient.net Microsoft Windows Client Resource Center

Prism: Composite Application Guidance

for WPF and Silverlight

MVVM Frameworks

Blog and Contact: http://reedcopsey.com

Copyright © 2010 by Reed Copsey, Jr.