Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Pro WPF in C# 2010 windows presentation foundation in .NET 4
PREMIUM
Số trang
1201
Kích thước
16.8 MB
Định dạng
PDF
Lượt xem
1792

Pro WPF in C# 2010 windows presentation foundation in .NET 4

Nội dung xem thử

Mô tả chi tiết

www.free-ebooks-library.com

Pro WPF in C# 2010:

Windows Presentation Foundation in

.NET 4.0

■ ■ ■

Matthew MacDonald

decbook.com

www.free-ebooks-library.com

■ CONTENTS

ii

Pro WPF in C# 2010: Windows Presentation Foundation in .NET 4.0

Copyright © 2010 by Matthew MacDonald

All rights reserved. No part of this work may be reproduced or transmitted in any form or by any

means, electronic or mechanical, including photocopying, recording, or by any information

storage or retrieval system, without the prior written permission of the copyright owner and the

publisher.

ISBN-13 (pbk): 978-1-4302-7205-2

ISBN-13 (electronic): 978-1-4302-7204-5

Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1

Trademarked names may appear in this book. Rather than use a trademark symbol with every

occurrence of a trademarked name, we use the names only in an editorial fashion and to the

benefit of the trademark owner, with no intention of infringement of the trademark.

Publisher and President: Paul Manning

Lead Editor: Ewan Buckingham

Technical Reviewer: Fabio Claudio Ferracchiati

Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell,

Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes,

Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic

Shakeshaft, Matt Wade, Tom Welsh

Project Manager: Anne Collett

Copy Editor: Marilyn Smith and Kim Wimpsett

Compositor: ContentWorks, Inc. and Bob Cooper

Indexer: BIM Indexing & Proofreading Services

Artist: April Milne

Cover Designer: Anna Ishchenko

Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th

Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer￾sbm.com, or visit www.springeronline.com.

For information on translations, please e-mail [email protected], or visit www.apress.com.

Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional

use. eBook versions and licenses are also available for most titles. For more information, reference

our Special Bulk Sales—eBook Licensing web page at www.apress.com/info/bulksales.

The information in this book is distributed on an “as is” basis, without warranty. Although every

precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall

have any liability to any person or entity with respect to any loss or damage caused or alleged to be

caused directly or indirectly by the information contained in this work.

The source code for this book is available to readers at www.apress.com. You will need to answer

questions pertaining to this book in order to successfully download the code.

decbook.com

www.free-ebooks-library.com

■ CONTENTS

iii

For my wonderful family,

Faria, Maya, and Brenna

■ CONTENTS

iv

Contents

■Chapter 1: Introducing WPF ............................................................................................................... 1

The Evolution of Windows Graphics ................................................................................................ 1

DirectX: The New Graphics Engine ............................................................................................. 2

Hardware Acceleration and WPF ................................................................................................ 3

WPF: A Higher-Level API ................................................................................................................ 4

Windows Forms Lives On ........................................................................................................... 6

DirectX Also Lives On ................................................................................................................. 6

Silverlight ................................................................................................................................... 6

Resolution Independence ............................................................................................................... 7

WPF Units .................................................................................................................................. 8

System DPI ................................................................................................................................ 9

Bitmap and Vector Graphics ..................................................................................................... 12

The Architecture of WPF ............................................................................................................... 12

The Class Hierarchy ................................................................................................................. 14

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

New Features ........................................................................................................................... 17

The WPF Toolkit ....................................................................................................................... 18

Visual Studio 2010 ................................................................................................................... 18

The Last Word .............................................................................................................................. 21

■Chapter 2: XAML .............................................................................................................................. 23

Understanding XAML .................................................................................................................... 24

Graphical User Interfaces Before WPF ...................................................................................... 24

The Variants of XAML ............................................................................................................... 25

XAML Compilation .................................................................................................................... 26

XAML Basics ................................................................................................................................ 27

XAML Namespaces .................................................................................................................. 28

decbook.com

■ CONTENTS

v

The Code-Behind Class ............................................................................................................ 30

Properties and Events in XAML ..................................................................................................... 32

Simple Properties and Type Converters .................................................................................... 34

Complex Properties .................................................................................................................. 35

Markup Extensions ................................................................................................................... 37

Attached Properties .................................................................................................................. 38

Nesting Elements ..................................................................................................................... 39

Special Characters and Whitespace ......................................................................................... 42

Events ...................................................................................................................................... 44

The Full Eight Ball Example ...................................................................................................... 45

Using Types from Other Namespaces ........................................................................................... 46

Loading and Compiling XAML ....................................................................................................... 48

Code-Only ................................................................................................................................ 48

Code and Uncompiled XAML .................................................................................................... 51

Code and Compiled XAML ........................................................................................................ 53

XAML Only ............................................................................................................................... 55

XAML 2009 .................................................................................................................................. 56

Automatic Event Hookup .......................................................................................................... 56

References............................................................................................................................... 58

Built-in Types ........................................................................................................................... 58

Advanced Object Creation ........................................................................................................ 59

The Last Word .............................................................................................................................. 60

■Chapter 3: Layout ............................................................................................................................. 61

Understanding Layout in WPF ....................................................................................................... 61

The WPF Layout Philosophy ..................................................................................................... 62

The Layout Process .................................................................................................................. 63

The Layout Containers ............................................................................................................. 63

Simple Layout with the StackPanel ............................................................................................... 65

Layout Properties ..................................................................................................................... 67

Alignment ................................................................................................................................ 68

Margin ..................................................................................................................................... 69

Minimum, Maximum, and Explicit Sizes ................................................................................... 71

■ CONTENTS

vi

The Border ............................................................................................................................... 74

The WrapPanel and DockPanel ..................................................................................................... 75

The WrapPanel ......................................................................................................................... 76

The DockPanel ......................................................................................................................... 77

Nesting Layout Containers ....................................................................................................... 79

The Grid ....................................................................................................................................... 80

Fine-Tuning Rows and Columns ............................................................................................... 83

Layout Rounding ...................................................................................................................... 85

Spanning Rows and Columns ................................................................................................... 86

Split Windows .......................................................................................................................... 87

Shared Size Groups .................................................................................................................. 91

The UniformGrid ....................................................................................................................... 93

Coordinate-Based Layout with the Canvas ................................................................................... 94

Z-Order .................................................................................................................................... 95

The InkCanvas ......................................................................................................................... 96

Layout Examples .......................................................................................................................... 99

A Column of Settings ................................................................................................................ 99

Dynamic Content ................................................................................................................... 100

A Modular User Interface ........................................................................................................ 102

The Last Word ............................................................................................................................ 104

■Chapter 4: Dependency Properties ................................................................................................ 105

Understanding Dependency Properties ....................................................................................... 105

Defining a Dependency Property ............................................................................................ 106

Registering a Dependency Property ........................................................................................ 107

Adding a Property Wrapper .................................................................................................... 109

How WPF Uses Dependency Properties .................................................................................. 110

Shared Dependency Properties .............................................................................................. 111

Attached Dependency Properties ............................................................................................ 112

Property Validation ..................................................................................................................... 114

The Validation Callback .......................................................................................................... 114

The Coercion Callback ........................................................................................................... 115

The Last Word ............................................................................................................................ 118

■ CONTENTS

vii

■Chapter 5: Routed Events .............................................................................................................. 119

Understanding Routed Events ..................................................................................................... 119

Defining, Registering, and Wrapping a Routed Event .............................................................. 120

Sharing Routed Events ........................................................................................................... 121

Raising a Routed Event .......................................................................................................... 121

Handling a Routed Event ........................................................................................................ 121

Event Routing ............................................................................................................................. 123

The RoutedEventArgs Class .................................................................................................... 125

Bubbling Events ..................................................................................................................... 126

Handling a Suppressed Event ................................................................................................. 129

Attached Events ..................................................................................................................... 129

Tunneling Events ................................................................................................................... 131

WPF Events ................................................................................................................................ 133

Lifetime Events ...................................................................................................................... 133

Input Events ........................................................................................................................... 136

Keyboard Input ........................................................................................................................... 137

Handling a Key Press ............................................................................................................. 138

Focus ..................................................................................................................................... 140

Getting Key State ................................................................................................................... 141

Mouse Input ............................................................................................................................... 143

Mouse Clicks ......................................................................................................................... 144

Capturing the Mouse .............................................................................................................. 146

Drag-and-Drop ...................................................................................................................... 146

Multitouch Input ......................................................................................................................... 149

The Levels of Multitouch Support ........................................................................................... 150

Raw Touch ............................................................................................................................. 150

Manipulation .......................................................................................................................... 153

Inertia .................................................................................................................................... 156

The Last Word ............................................................................................................................ 157

■Chapter 6: Controls ........................................................................................................................ 159

The Control Class ....................................................................................................................... 160

Background and Foreground Brushes ..................................................................................... 160

■ CONTENTS

viii

Fonts ..................................................................................................................................... 163

Mouse Cursors ....................................................................................................................... 168

Content Controls ........................................................................................................................ 169

The Content Property ............................................................................................................. 171

Aligning Content ..................................................................................................................... 173

The WPF Content Philosophy .................................................................................................. 174

Labels .................................................................................................................................... 175

Buttons .................................................................................................................................. 177

Tooltips .................................................................................................................................. 180

Specialized Containers ............................................................................................................... 188

The ScrollViewer .................................................................................................................... 188

Headered Content Controls ......................................................................................................... 192

The GroupBox ........................................................................................................................ 192

The TabItem ........................................................................................................................... 193

The Expander ......................................................................................................................... 195

Text Controls .............................................................................................................................. 197

Multiple Lines of Text ............................................................................................................. 198

Text Selection ........................................................................................................................ 199

Spell Checking ....................................................................................................................... 200

The PasswordBox .................................................................................................................. 202

List Controls ............................................................................................................................... 202

The ListBox ............................................................................................................................ 203

The ComboBox ....................................................................................................................... 206

Range-Based Controls ................................................................................................................ 207

The Slider .............................................................................................................................. 208

The ProgressBar .................................................................................................................... 209

Date Controls ............................................................................................................................. 210

The Last Word ............................................................................................................................ 213

■Chapter 7: The Application ............................................................................................................. 215

The Application Life Cycle .......................................................................................................... 215

Creating an Application Object ............................................................................................... 216

Deriving a Custom Application Class ...................................................................................... 217

■ CONTENTS

ix

Application Shutdown ............................................................................................................ 218

Application Events .................................................................................................................. 220

Application Tasks ....................................................................................................................... 222

Showing a Splash Screen ....................................................................................................... 222

Handling Command-Line Arguments ...................................................................................... 223

Accessing the Current Application .......................................................................................... 224

Interacting Between Windows ................................................................................................ 225

Single-Instance Applications .................................................................................................. 227

Assembly Resources .................................................................................................................. 234

Adding Resources .................................................................................................................. 234

Retrieving Resources ............................................................................................................. 236

Pack URIs .............................................................................................................................. 237

Content Files .......................................................................................................................... 239

Localization ................................................................................................................................ 239

Building Localizable User Interfaces ....................................................................................... 240

Preparing an Application for Localization ................................................................................ 241

The Translation Process ......................................................................................................... 242

The Last Word ............................................................................................................................ 248

■Chapter 8: Element Binding ........................................................................................................... 249

Binding Elements Together ........................................................................................................ 249

The Binding Expression .......................................................................................................... 250

Binding Errors ........................................................................................................................ 251

Binding Modes ....................................................................................................................... 251

Creating Bindings with Code .................................................................................................. 254

Multiple Bindings ................................................................................................................... 255

Binding Updates ..................................................................................................................... 259

Binding to Objects That Aren’t Elements ..................................................................................... 260

Source ................................................................................................................................... 261

RelativeSource ....................................................................................................................... 261

DataContext ........................................................................................................................... 263

The Last Word ............................................................................................................................ 264

■ CONTENTS

x

■Chapter 9: Commands ................................................................................................................... 265

Understanding Commands ......................................................................................................... 265

The WPF Command Model ......................................................................................................... 267

The ICommand Interface ........................................................................................................ 267

The RoutedCommand Class ................................................................................................... 268

The RoutedUICommand Class ................................................................................................ 269

The Command Library ............................................................................................................ 269

Executing Commands ................................................................................................................ 271

Command Sources ................................................................................................................. 271

Command Bindings ................................................................................................................ 272

Using Multiple Command Sources .......................................................................................... 275

Fine-Tuning Command Text ................................................................................................... 276

Invoking a Command Directly ................................................................................................. 276

Disabling Commands ............................................................................................................. 277

Controls with Built-in Commands ........................................................................................... 280

Advanced Commands ................................................................................................................ 282

Custom Commands ................................................................................................................ 282

Using the Same Command in Different Places ........................................................................ 284

Using a Command Parameter ................................................................................................. 286

Tracking and Reversing Commands ....................................................................................... 286

The Last Word ............................................................................................................................ 291

■Chapter 10: Resources ................................................................................................................... 293

Resource Basics ........................................................................................................................ 293

The Resources Collection ....................................................................................................... 294

The Hierarchy of Resources ................................................................................................... 295

Static and Dynamic Resources ............................................................................................... 297

Nonshared Resources ............................................................................................................ 299

Accessing Resources in Code ................................................................................................. 299

Application Resources ............................................................................................................ 300

System Resources ................................................................................................................. 301

Resource Dictionaries ................................................................................................................ 302

Creating a Resource Dictionary .............................................................................................. 302

■ CONTENTS

xi

Using a Resource Dictionary ................................................................................................... 303

Sharing Resources Between Assemblies ................................................................................ 304

The Last Word ............................................................................................................................ 307

■Chapter 11: Styles and Behaviors .................................................................................................. 309

Style Basics ............................................................................................................................... 309

Creating a Style Object ........................................................................................................... 313

Setting Properties .................................................................................................................. 314

Attaching Event Handlers ....................................................................................................... 316

The Many Layers of Styles ..................................................................................................... 317

Automatically Applying Styles by Type .................................................................................... 319

Triggers ..................................................................................................................................... 320

A Simple Trigger .................................................................................................................... 321

An Event Trigger .................................................................................................................... 323

Behaviors ................................................................................................................................... 325

Getting Support for Behaviors................................................................................................. 325

Understanding the Behavior Model ......................................................................................... 326

Creating a Behavior ................................................................................................................ 327

Using a Behavior .................................................................................................................... 329

Design-Time Behavior Support in Blend ................................................................................. 330

The Last Word ............................................................................................................................ 331

■Chapter 12: Shapes, Brushes, and Transforms ................................................................................ 333

Understanding Shapes ............................................................................................................... 333

The Shape Classes ................................................................................................................. 334

Rectangle and Ellipse ............................................................................................................. 337

Sizing and Placing Shapes ..................................................................................................... 338

Scaling Shapes with a Viewbox .............................................................................................. 341

Line ....................................................................................................................................... 343

Polyline .................................................................................................................................. 344

Polygon .................................................................................................................................. 345

Line Caps and Line Joins ....................................................................................................... 348

Dashes .................................................................................................................................. 349

Pixel Snapping ....................................................................................................................... 351

■ CONTENTS

xii

Brushes ..................................................................................................................................... 352

The SolidColorBrush ............................................................................................................... 353

The LinearGradientBrush ........................................................................................................ 354

The RadialGradientBrush ........................................................................................................ 356

The ImageBrush ..................................................................................................................... 358

A Tiled ImageBrush ................................................................................................................ 360

The VisualBrush ..................................................................................................................... 363

The BitmapCacheBrush .......................................................................................................... 364

Transforms ................................................................................................................................ 365

Transforming Shapes ............................................................................................................. 367

Transforming Elements .......................................................................................................... 369

Transparency ............................................................................................................................. 370

Making an Element Partially Transparent ............................................................................... 370

Opacity Masks ....................................................................................................................... 372

The Last Word ............................................................................................................................ 374

■Chapter 13: Geometries and Drawings .......................................................................................... 375

Paths and Geometries ................................................................................................................ 375

Line, Rectangle, and Ellipse Geometries ................................................................................. 376

Combining Shapes with GeometryGroup ................................................................................ 377

Fusing Geometries with CombinedGeometry .......................................................................... 379

Curves and Lines with PathGeometry ..................................................................................... 383

The Geometry Mini-Language ................................................................................................ 388

Clipping with Geometry .......................................................................................................... 391

Drawings ................................................................................................................................... 392

Displaying a Drawing ............................................................................................................. 394

Exporting Clip Art ................................................................................................................... 396

The Last Word ............................................................................................................................ 398

■Chapter 14: Effects and Visuals ..................................................................................................... 399

Visuals ....................................................................................................................................... 399

Drawing Visuals ..................................................................................................................... 400

Wrapping Visuals in an Element ............................................................................................. 402

Hit Testing ............................................................................................................................. 405

■ CONTENTS

xiii

Complex Hit Testing ............................................................................................................... 408

Effects ....................................................................................................................................... 411

BlurEffect ............................................................................................................................... 412

DropShadowEffect ................................................................................................................. 413

ShaderEffect .......................................................................................................................... 414

The WriteableBitmap Class ......................................................................................................... 416

Generating a Bitmap .............................................................................................................. 416

Writing to a WriteableBitmap .................................................................................................. 417

More Efficient Pixel Writing .................................................................................................... 419

The Last Word ............................................................................................................................ 422

■Chapter 15: Animation Basics ........................................................................................................ 423

Understanding WPF Animation ................................................................................................... 423

Timer-Based Animation .......................................................................................................... 424

Property-Based Animation ...................................................................................................... 425

Basic Animation ......................................................................................................................... 426

The Animation Classes ........................................................................................................... 426

Animations in Code ................................................................................................................ 429

Simultaneous Animations ....................................................................................................... 434

Animation Lifetime ................................................................................................................. 434

The Timeline Class ................................................................................................................. 436

Storyboards ............................................................................................................................... 439

The Storyboard ...................................................................................................................... 440

Event Triggers ........................................................................................................................ 440

Overlapping Animations ......................................................................................................... 443

Synchronized Animations ....................................................................................................... 444

Controlling Playback .............................................................................................................. 445

Monitoring Progress ............................................................................................................... 450

Animation Easing ....................................................................................................................... 452

Using an Easing Function ....................................................................................................... 452

Easing In and Easing Out ........................................................................................................ 453

Easing Function Classes ......................................................................................................... 455

Creating a Custom Easing Function ........................................................................................ 458

■ CONTENTS

xiv

Animation Performance .............................................................................................................. 460

Desired Frame Rate ............................................................................................................... 460

Bitmap Caching ..................................................................................................................... 463

The Last Word ............................................................................................................................ 465

■Chapter 16: Advanced Animation ................................................................................................... 467

Animation Types Revisited ......................................................................................................... 467

Animating Transforms ............................................................................................................ 468

Animating Brushes ................................................................................................................. 472

Animating Pixel Shaders ........................................................................................................ 475

Key Frame Animation ................................................................................................................. 477

Discrete Key Frame Animations .............................................................................................. 478

Easing Key Frames ................................................................................................................ 479

Spline Key Frame Animations ................................................................................................. 480

Path-Based Animation ................................................................................................................ 481

Frame-Based Animation ............................................................................................................. 483

Storyboards in Code ................................................................................................................... 487

The Main Window .................................................................................................................. 488

The Bomb User Control .......................................................................................................... 490

Dropping the Bombs .............................................................................................................. 491

Intercepting a Bomb ............................................................................................................... 494

Counting Bombs and Cleaning Up .......................................................................................... 496

The Last Word ............................................................................................................................ 498

■Chapter 17: Control Templates ...................................................................................................... 499

Understanding Logical Trees and Visual Trees ............................................................................ 500

Understanding Templates ........................................................................................................... 506

The Chrome Classes .............................................................................................................. 509

Dissecting Controls ................................................................................................................ 510

Creating Control Templates ........................................................................................................ 513

A Simple Button ..................................................................................................................... 514

Template Bindings ................................................................................................................. 515

Triggers That Change Properties ............................................................................................ 517

Triggers That Use Animation .................................................................................................. 520

Tải ngay đi em, còn do dự, trời tối mất!