!PPLIED70& IN#ONTEXTphilkildea.co.uk/james/books/Applied WPF 4 in Context.pdf · !pplied70&...

Preview:

Citation preview

■ ■ ■

Contents

The XAML Syntax ............................................................................................................... 2

Namespaces and Root Elements .............................................................................................................. 2

Objects and Common Elements ................................................................................................................ 3

The Code Behind ....................................................................................................................................... 6

Attached Properties .................................................................................................................................. 8

Introduction to WPF ........................................................................................................... 8

WPF Architecture .................................................................................................................................... 13

WPF 4 Tools ............................................................................................................................................ 17

Summary ......................................................................................................................... 23

Requirements .................................................................................................................. 25

Tools and Software ................................................................................................................................. 25

Icons and Templates .............................................................................................................................. 26

Third-Party Library and Controls ............................................................................................................ 26

Application Overview ....................................................................................................... 27

Application Architecture ......................................................................................................................... 27

User Stories ............................................................................................................................................ 31

Domain Model ......................................................................................................................................... 32

Database Schema ................................................................................................................................... 34

Application Configuration ................................................................................................ 35

Visual Studio Solution ............................................................................................................................. 35

Resources and Third-Party Components ................................................................................................ 38

Summary ......................................................................................................................... 41

Overview .......................................................................................................................... 43

Blend Workspace ............................................................................................................. 45

Workspace Panels .................................................................................................................................. 46

Drawing ........................................................................................................................... 48

Shapes and Objects ................................................................................................................................ 49

Text and Text Effects .............................................................................................................................. 50

Animations ....................................................................................................................... 51

Styling and Templating Controls ...................................................................................... 53

Modifying Properties .............................................................................................................................. 54

Creating Styles ....................................................................................................................................... 55

Design-Time Data ............................................................................................................ 57

SketchFlow and the Mockups ......................................................................................... 59

Summary ......................................................................................................................... 60

Overview .......................................................................................................................... 61

Creating a Mock-up and Prototyping ............................................................................... 62

The Main Screen ..................................................................................................................................... 64

The List Views ........................................................................................................................................ 66

The Details Views ................................................................................................................................... 69

The View Models .............................................................................................................. 70

Base ViewModel Implementation ........................................................................................................... 70

Details ViewModel .................................................................................................................................. 73

List ViewModel ....................................................................................................................................... 74

Navigation Between the Views ........................................................................................ 77

The App.xaml File ................................................................................................................................... 77

Wrapping Up with IoC ...................................................................................................... 79

Inversion of Control ................................................................................................................................ 79

A Classic Implementation, Navigation Service ....................................................................................... 79

Summary ......................................................................................................................... 85

Resources and Styles ...................................................................................................... 87

Differences Between Static and Dynamic Resources ............................................................................ 88

The Resource Dictionary ......................................................................................................................... 88

Controls’ Styles ....................................................................................................................................... 91

Creating the Views ........................................................................................................... 97

Login View .............................................................................................................................................. 98

Details View .......................................................................................................................................... 100

List View ............................................................................................................................................... 104

User Controls for the Dashboard .......................................................................................................... 107

The Dashboard (MainView) ................................................................................................................... 109

Add Features to the Application ..................................................................................... 110

Custom Effects ..................................................................................................................................... 110

Summary ....................................................................................................................... 111

Object Relational Mappers ................................................................................................................... 113

Active Record Pattern ........................................................................................................................... 115

Code First Development ....................................................................................................................... 117

The TimeTracker Domain ............................................................................................... 121

Mapping the Domain Model .................................................................................................................. 121

The TimeTracker Data Layer .......................................................................................... 135

The Unit of Work ................................................................................................................................... 135

The Repository ...................................................................................................................................... 151

Summary ....................................................................................................................... 158

WPF Data Binding Engine .............................................................................................. 159

The INotifyPropertyChanged Interface .................................................................................................. 160

Binding Direction .................................................................................................................................. 162

The IValueConverter Interface .............................................................................................................. 162

DataTemplate ....................................................................................................................................... 166

UI Validation .......................................................................................................................................... 168

Binding Syntax ...................................................................................................................................... 170

ViewModels ................................................................................................................... 171

A Generic ViewModel Validator ............................................................................................................. 171

Employee ViewModels .......................................................................................................................... 173

Additional ViewModels and Properties ................................................................................................. 184

Summary ....................................................................................................................... 187

Introduction to Commands ............................................................................................ 190

The Command Pattern .......................................................................................................................... 190

Command in WPF ................................................................................................................................. 191

Interactive Command, Listen for Changes ............................................................................................ 198

TimeTracker Commands ................................................................................................ 204

The EmployeeListView .......................................................................................................................... 204

Summary ....................................................................................................................... 208

Test Driven Development .............................................................................................. 209

Code Coverage. .................................................................................................................................... 210

TDD with MSTest . ................................................................................................................................ 212

Code Coverage with Visual Studio . ...................................................................................................... 216

TDD: the TimeTracker Application . ............................................................................... 219

Test the Business (Domain) Layer . ...................................................................................................... 220

Testing the Data Layer . ........................................................................................................................ 221

Testing the UI . ...................................................................................................................................... 223

Summary ....................................................................................................................... 225

SQL Server Reporting Services ..................................................................................... 228

Configure SQL Server Reporting Services ............................................................................................ 228

TimeTracker Reports . ................................................................................................... 231

First Report: EmployeeListReport . ....................................................................................................... 231

EmployeeDetailsReport . ...................................................................................................................... 237

Additional Reports . .............................................................................................................................. 241

Hosting the Reports in a WPF Control . .......................................................................... 241

Display the Report in a WPF View . ....................................................................................................... 242

Load a List of Employees Report . ........................................................................................................ 244

Load a Detailed Report . ....................................................................................................................... 246

Summary ....................................................................................................................... 248

ClickOnce Deployment .................................................................................................. 251

ClickOnce Security. .............................................................................................................................. 252

Publishing Options ......................................................................................................... 254

Updates ................................................................................................................................................ 254

Application Files and Prerequisites ...................................................................................................... 255

Additional Options ................................................................................................................................ 256

Deploy the TimeTracker Application .............................................................................. 257

Create a Temporary Certificate............................................................................................................. 258

Security ................................................................................................................................................ 259

Publish TimeTracker ............................................................................................................................. 259

Summary ....................................................................................................................... 260

Patterns for the UI .......................................................................................................... 261

Model View Presenter ........................................................................................................................... 262

Model View ViewModel ........................................................................................................................ 268

Summary ....................................................................................................................... 273

Writing Multithreading Code in .NET .............................................................................. 276

The Thread Object ................................................................................................................................ 276

Asynchronous Operations ..................................................................................................................... 280

The Task Object .................................................................................................................................... 283

The Background Worker Component .................................................................................................... 285

Summary ....................................................................................................................... 287

Introduction to WCF ....................................................................................................... 290

Brief Overview of WCF Architecture ..................................................................................................... 291

A Sample WCF Project ................................................................................................... 292

The WCF Test Client Utility ................................................................................................................... 294

Customize the Sample Service ............................................................................................................. 295

Test the Sample Service ....................................................................................................................... 300

Conclusion and Considerations ............................................................................................................ 304

Integrate WCF into the TimeTracker Application ........................................................... 304

WCF Data Service ................................................................................................................................. 304

Navigate the Data Using oData Queries ................................................................................................ 306

Integrate the WCF Data Service in WPF ................................................................................................ 307

WCF Security ................................................................................................................. 308

Security Requirements ......................................................................................................................... 309

Bindings ................................................................................................................................................ 309

Provide the Credentials ........................................................................................................................ 310

Authenticate and Authorize the Code ................................................................................................... 310

Summary ....................................................................................................................... 311

About the Author

Acknowledgments

Contents at a Glance

Introduction

C H A P T E R 1

■ ■ ■

Introducing WPF and XAML

The XAML Syntax

The Attribute Syntax

The TypeConverterAttribute

Property Element Syntax

The Content Property

Events in XAML

Introduction to WPF

UI Controls in WPF

Hello World Nr.1

Hello World Nr.2

Hello World Nr.3

Control Composition

Technologies

Visual Studio 2010

Expression Blend

Additional Tools

Summary

C H A P T E R 2

■ ■ ■

Sample Application: Overview and Getting Started

Requirements

Application Overview

Layered Applications

N-tier Applications

TimeTracker Architecture

Application Configuration

Layers in TimeTracker

Project name Project Type Folder Layer Type

How WPF Uses the Resources

Summary

C H A P T E R

■ ■ ■

Microsoft Expression Blend

Overview

Hardware Minimum

Blend Workspace

Drawing

Animations

Styling and Templating Controls

Design-Time Data

SketchFlow and the Mockups

Summary

C H A P T E R

■ ■ ■

Creating the Views

Overview

u

Creating a Mock-up and Prototyping

➤ ➤

➤ ➤

Dummy Data

1.

2.

3.

4.

5.

Behaviors for SketchFlow

1.

2.

3.

4.

The View Models

The INotifyPropertyChanged

BaseViewModel

Navigation Between the Views

Custom Startup Process

Wrapping Up with IoC

Summary

C H A P T E R

■ ■ ■

Adding Controls to the Views

Resources and Styles

Embed Images in a Dictionary

Location Syntax Example

Basic Style Concepts

Triggers

Image Button

WPF Themes

Creating the Views

Property Value Description

User Controls

The DataGrid

Charts with WPF

Drag and Drop (Appointments Control)

Add Features to the Application

Summary

C H A P T E R

■ ■ ■

The Entity Framework

Pros and Cons of the Active Record Pattern

The Repository Pattern

The Unit of Work

The TimeTracker Domain

The Entity Framework

The Role Domain Entity

1.

2.

3.

4.

Autogenerated Code

The Employee Domain Entity

Property name Type Nullable Max Length

The First Relationship, Role and Employee

1.

2.

3.

Property Value

The Address Entity

Property name Type Nullable Max Length

Property Value

The Contact Entity

Property name Type Nullable Max Length

Property Value

Generating the Database Schema

1.

2.

3.

The Customer Entity

Property name Type Nullable Max Length

The Appointment Entity

Property name Type Nullable Max Length

The TimeTracker Data Layer

Set Up the First Test Fixture

1.

2.

3.

4.

5.

1.

2.

3.

4.

Transaction Management

Add, Update, and Remove Objects

n

Final Consideration for the Unit of Work

Add a New Role

u

Delete an Existing Role

Update an Existing Role

GetAll Roles

Get a Role by Id

Additional Methods

Summary

C H A P T E R

■ ■ ■

Data Binding

WPF Data Binding Engine

Binding Syntax Description

Binding Path Description

Binding Path Description

ViewModels

1.

2.

3.

Employee Details

Employee List

Summary

C H A P T E R

■ ■ ■

Command Handling and Event Routing

Introduction to Commands

The RoutedCommand Class

Implement the ICommand Interface

TimeTracker Commands

Search for an Employee

Select and Edit an Employee

Delete an Employee

Suggested Steps

Summary

C H A P T E R

■ ■ ■

Testing with TDD

Test Driven Development

u

Calculator Example ➤ ➤

Available Tests

➤ ➤

➤ ➤

TDD: the TimeTracker Application

Summary

C H A P T E R

■ ■ ■

Reports with Microsoft Reporting Services

SQL Server Reporting Services

➤ ➤ ➤ ➤

1.

2.

3.

4.

5.

6.

7.

TimeTracker Reports

➤ ➤

Create a DataSource

Create the Dataset

Design the Report

➤ ➤

➤ ➤

Create a Query with a Parameter

Create the Details

1.

2.

3.

4.

Hosting the Reports in a WPF Control

Summary

C H A P T E R

■ ■ ■

Deploy the Application Using ClickOnce

ClickOnce Deployment

Certificates and Authenticode

➤ ➤ ➤ ➤

Code Access Security

Publishing Options

Deploy the TimeTracker Application

➤ ➤

Summary

C H A P T E R 2

■ ■ ■

Design Patterns in WPF

Patterns for the UI

When to Use the MVP

Exposing the Commands

Summary

C H A P T E R

■ ■ ■

WPF and Multithreading

Writing Multithreading Code in .NET

Interacting with the Thread class

1.

2.

3.

4.

The ThreadPool Component

Summary

C H A P T E R

■ ■ ■

Interacting with WCF

Introduction to WCF

The Service and the Client

A Sample WCF Project

➤ ➤

Working with DataContracts

Add Methods to the Service

Customize the Way the Service Will Behave

Add Service Reference

Test the Endpoint

Integrate WCF into the TimeTracker Application

WCF Security

Summary

Index

■A

■C

■E

■H

■P

■R

■T

Recommended