Tải bản đầy đủ (.pdf) (1,753 trang)

Pro c# 2010 and the NET 4 platform, troelsen, 5ed, apress, 2010

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (22.7 MB, 1,753 trang )

  CYAN
  MAGENTA

  YELLOW
  BLACK
  PANTONE 123 C

BOOKS FOR PROFESSIONALS BY PROFESSIONALS ®

Pro C# 2010 and the .NET 4
Platform, FIFTH EDITION

Pro VB 2008 and the
.NET 3.5 Platform
COM and .NET
Interoperability
Expert ASP.NET 2.0
Advanced Application
Design
Developer’s Workshop
to COM and ATL 3.0

• the Dynamic Language Runtime (DLR)
• the Task Parallel Library (TPL, including PLINQ)
• the ADO.NET Entity Framework (as well as LINQ to EF)
• expanded coverage of the Windows Presentation Foundation (WPF) API
• improved support for COM interop

Andrew Troelsen
Microsoft MVP Visual Developer—Visual C#; Microsoft Certified Trainer (MCT)
,


Companion eBook

THE APRESS ROADMAP

See last page for details
on $10 eBook version

Accelerated
C# 2010

Visual C# 2010
Recipes

Pro
ASP.NET 4 in C# 2010

Introducing
.NET 4.0

Pro
C# 2010 and the
.NET 4 Platform

Pro
Windows Azure

Pro
WPF in C# 2010

Pro Dynamic

.NET 4.0 Applications

www.apress.com

ISBN 978-1-4302-2549-2
5 59 9 9

US $59.99

C# 2010

and
the

.NET 4 Platform
Exploring the .NET universe using
curly brackets

FIFTH EDITION

FIFTH EDITION

Troelsen

SOURCE CODE ONLINE

Pro

and
the


If you’re checking out this book for the first time, understand that it targets experienced software professionals and students of computer science—so please don’t
expect three chapters devoted to “variable scope” here! The mission of this text
is to provide you with a rock-solid foundation in the C# programming language
(including a full discussion of OOP) and the critical aspects of the .NET 4 platform.
Once you digest the information presented in the book you hold in your hands,
you’ll be in a perfect position to apply this knowledge to your specific programming assignments, and you’ll be well-equipped to explore the .NET universe on
your own terms.

Pro

C# 2010

Andrew Troelsen, author of

The C# language and the .NET platform have existed for quite some time and have
become a mainstay of modern software development. As expected, .NET 4 brings a
wealth of new APIs to the base class libraries and several new syntactical constructs
to C#. For example, it is now possible to create C# methods that support optional
and named arguments. As well, you are able to resolve types completely at runtime
using the new dynamic keyword. As far as APIs are concerned, this edition of Pro
C# 2010 and the .NET 4 Platform will cover a number of new details, including:

Companion
eBook Available

.NET 4 Platform

Dear Reader,


THE EXPERT’S VOICE ® IN .NET

Andrew Troelsen

Shelve in:
Programming | C#
User level:
Intermediate–Advanced

9 7814
30 225492

this print for content only—size & color not accurate

7.5 x 9.25 spine = 3.28125" 1752 page count 544ppi


     


Pro C# 2010 and
the .NET 4 Platform
Fifth Edition

■■■
Andrew Troelsen


Pro C# 2010 and the .NET 4 Platform, Fifth Edition
Copyright © 2010 by Andrew Troelsen

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-2549-2
ISBN-13 (electronic): 978-1-4302-2550-8
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.
President and Publisher: Paul Manning
Lead Editor: Ewan Buckingham
Technical Reviewer: Andy Olsen
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
Coordinating Editor: Debra Kelly
Copy Editors: Patrick Meader, Katie Stence, and Sharon Terdeman
Compositor: folio 2
Indexer: Potomac Indexers
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 , or
visit www.springeronline.com.
For information on translations, please e-mail , 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.


To the memory of my wonderful grandmother, Maxine. I love you, Gerta. Rest peacefully.



Contents at a Glance
Contents................................................................................................................... viii
About the Author..................................................................................................... xxv
About the Technical Reviewer ............................................................................... xxvi
Acknowledgments ................................................................................................ xxvii
Introduction ......................................................................................................... xxviii
■ ■ ■ Part 1: Introducing C# and the .NET Platform.................................................1
■ Chapter 1: The Philosophy of .NET..........................................................................3
■ Chapter 2: Building C# Applications .....................................................................41
■ ■ ■ Part 2: Core C# Programming Constructs ....................................................71
■ Chapter 3: Core C# Programming Constructs, Part I ............................................73
■ Chapter 4: Core C# Programming Constructs, Part II.........................................125
■ Chapter 5: Defining Encapsulated Class Types...................................................167
■ Chapter 6: Understanding Inheritance and Polymorphism.................................219
■ Chapter 7: Understanding Structured Exception Handling .................................259
■ Chapter 8: Understanding Object Lifetime ..........................................................289
■ ■ ■ Part 3: Advanced C# Programming Constructs ..........................................319
■ Chapter 9: Working with Interfaces....................................................................321
■ Chapter 10: Understanding Generics ..................................................................361
v



■ CONTENTS AT A GLANCE

■ Chapter 11: Delegates, Events, and Lambdas.....................................................397
■ Chapter 12: Advanced C# Language Features ....................................................439
■ Chapter 13: LINQ to Objects................................................................................489
■ ■ ■ Part 4: Programming with .NET Assemblies...............................................523
■ Chapter 14: Configuring .NET Assemblies ..........................................................525
■ Chapter 15: Type Reflection, Late Binding, and Attribute-Based Programming......581
■ Chapter 16: Processes, AppDomains, and Object Contexts................................625
■ Chapter 17: Understanding CIL and the Role of Dynamic Assemblies................653
■ Chapter 18: Dynamic Types and the Dynamic Language Runtime .....................701
■ ■ ■ Part 5: Introducing the .NET Base Class Libraries ......................................725
■ Chapter 19: Multithreaded and Parallel Programming.......................................727
■ Chapter 20: File I/O and Object Serialization......................................................775
■ Chapter 21: ADO.NET Part I: The Connected Layer..............................................825
■ Chapter 22: ADO.NET Part II: The Disconnected Layer........................................885
■ Chapter 23: ADO.NET Part III: The Entity Framework .........................................951
■ Chapter 24: Introducing LINQ to XML .................................................................993
■ Chapter 25: Introducing Windows Communication Foundation .......................1013
■ Chapter 26: Introducing Windows Workflow Foundation 4.0 ...........................1077
■ ■ ■ Part 6: Building Desktop User Interfaces with WPF .................................1115
■ Chapter 27: Introducing Windows Presentation Foundation and XAML ...........1117
■ Chapter 28: Programming with WPF Controls ..................................................1179
■ Chapter 29: WPF Graphics Rendering Services ................................................1245
■ Chapter 30: WPF Resources, Animations, and Styles.......................................1285
■ Chapter 31: WPF Control Templates and UserControls.....................................1325
vi



■ CONTENTS AT A GLANCE

■ ■ ■ Part 7: Building Web Applications with ASP.NET......................................1377
■ Chapter 32: Building ASP.NET Web Pages ........................................................1379
■ Chapter 33: ASP.NET Web Controls, Master Pages and Themes ......................1429
■ Chapter 34: ASP.NET State Management Techniques.......................................1473
■ Appendix A: Programming with Windows Forms .............................................1511
■ Appendix B: Platform-Independent .NET Development with Mono...................1561
■ Index.................................................................................................................1583

vii


■ CONTENTS

Contents
About the Author..................................................................................................... xxv
About the Technical Reviewer ............................................................................... xxvi
Acknowledgments ................................................................................................ xxvii
Introduction ......................................................................................................... xxviii
■ ■ ■ Part 1: Introducing C# and the .NET Platform.................................................1
■ Chapter 1: The Philosophy of .NET..........................................................................3
Understanding the Previous State of Affairs......................................................................3
The .NET Solution ..............................................................................................................6
Introducing the Building Blocks of the .NET Platform (the CLR, CTS, and CLS).................7
Additional .NET-Aware Programming Languages............................................................10
An Overview of .NET Assemblies .....................................................................................12
Understanding the Common Type System.......................................................................19
Understanding the Common Language Specification......................................................23

Understanding the Common Language Runtime .............................................................25
The Assembly/Namespace/Type Distinction ...................................................................27
Exploring an Assembly Using ildasm.exe ........................................................................33
Exploring an Assembly Using Reflector ...........................................................................35
Deploying the .NET Runtime ............................................................................................36

viii


■ CONTENTS

The Platform-Independent Nature of .NET.......................................................................37
Summary .........................................................................................................................39
■ Chapter 2: Building C# Applications .....................................................................41
The Role of the .NET Framework 4.0 SDK .......................................................................41
Building C# Applications Using csc.exe...........................................................................42
Building .NET Applications Using Notepad++ .................................................................49
Building .NET Applications Using SharpDevelop..............................................................50
Building .NET Applications Using Visual C# 2010 Express ..............................................53
Building .NET Applications Using Visual Studio 2010 ......................................................54
Summary .........................................................................................................................69
■ ■ ■ Part 2: Core C# Programming Constructs ....................................................71
■ Chapter 3: Core C# Programming Constructs, Part I ............................................73
The Anatomy of a Simple C# Program.............................................................................73
An Interesting Aside: Some Additional Members of the System.Environment Class.......79
The System.Console Class...............................................................................................81
System Data Types and C# Shorthand Notation ..............................................................86
Working with String Data.................................................................................................97
Narrowing and Widening Data Type Conversions..........................................................106
Understanding Implicitly Typed Local Variables ............................................................112

C# Iteration Constructs ..................................................................................................117
Decision Constructs and the Relational/Equality Operators ..........................................119
Summary .......................................................................................................................122
■ Chapter 4: Core C# Programming Constructs, Part II.........................................125
Methods and Parameter Modifiers ................................................................................125
Understanding C# Arrays ...............................................................................................137
Understanding the Enum Type.......................................................................................144
ix


■ CONTENTS

Understanding the Structure Type.................................................................................151
Understanding Value Types and Reference Types.........................................................154
Understanding C# Nullable Types..................................................................................162
Summary .......................................................................................................................165
■ Chapter 5: Defining Encapsulated Class Types...................................................167
Introducing the C# Class Type .......................................................................................167
Understanding Constructors ..........................................................................................170
The Role of the this Keyword.........................................................................................174
Understanding the static Keyword.................................................................................181
Defining the Pillars of OOP.............................................................................................188
C# Access Modifiers ......................................................................................................192
The First Pillar: C#’s Encapsulation Services.................................................................194
Understanding Automatic Properties.............................................................................206
Understanding Object Initializer Syntax.........................................................................210
Working with Constant Field Data .................................................................................214
Understanding Partial Types..........................................................................................217
Summary .......................................................................................................................218
■ Chapter 6: Understanding Inheritance and Polymorphism.................................219

The Basic Mechanics of Inheritance..............................................................................219
Revising Visual Studio Class Diagrams .........................................................................224
The Second Pillar of OOP: The Details of Inheritance ....................................................226
Programming for Containment/Delegation ....................................................................232
The Third Pillar of OOP: C#’s Polymorphic Support .......................................................235
Understanding Base Class/Derived Class Casting Rules ...............................................247
The Master Parent Class: System.Object.......................................................................250
Summary .......................................................................................................................258
x


■ CONTENTS

■ Chapter 7: Understanding Structured Exception Handling .................................259
Ode to Errors, Bugs, and Exceptions .............................................................................259
The Role of .NET Exception Handling.............................................................................260
The Simplest Possible Example .....................................................................................263
Configuring the State of an Exception ...........................................................................267
System-Level Exceptions (System.SystemException) ...................................................272
Application-Level Exceptions (System.ApplicationException) .......................................273
Processing Multiple Exceptions .....................................................................................277
Who Is Throwing What? .................................................................................................282
The Result of Unhandled Exceptions .............................................................................283
Debugging Unhandled Exceptions Using Visual Studio .................................................284
A Brief Word Regarding Corrupted State Exceptions (CSE) ...........................................285
Summary .......................................................................................................................287
■ Chapter 8: Understanding Object Lifetime ..........................................................289
Classes, Objects, and References..................................................................................289
The Basics of Object Lifetime ........................................................................................291
The Role of Application Roots ........................................................................................294

Understanding Object Generations ................................................................................296
Concurrent Garbage Collection under .NET 1.0 - 3.5 .....................................................297
Background Garbage Collection under .NET 4.0............................................................297
The System.GC Type ......................................................................................................298
Building Finalizable Objects...........................................................................................302
Building Disposable Objects ..........................................................................................305
Building Finalizable and Disposable Types....................................................................309
Understanding Lazy Object Instantiation .......................................................................313
Summary .......................................................................................................................316
xi


■ CONTENTS

■ ■ ■ Part 3: Advanced C# Programming Constructs ..........................................319
■ Chapter 9: Working with Interfaces....................................................................321
Understanding Interface Types......................................................................................321
Defining Custom Interfaces ...........................................................................................325
Implementing an Interface.............................................................................................327
Invoking Interface Members at the Object Level ...........................................................329
Interfaces As Parameters ..............................................................................................331
Interfaces As Return Values...........................................................................................333
Arrays of Interface Types...............................................................................................334
Implementing Interfaces Using Visual Studio 2010 .......................................................335
Resolving Name Clashes via Explicit Interface Implementation....................................336
Designing Interface Hierarchies ....................................................................................339
Building Enumerable Types (IEnumerable and IEnumerator) ........................................343
Building Cloneable Objects (ICloneable) ........................................................................349
Building Comparable Objects (IComparable) .................................................................354
Summary .......................................................................................................................359

■ Chapter 10: Understanding Generics ..................................................................361
The Issues with Non-Generic Collections ......................................................................361
The Role of Generic Type Parameters ...........................................................................371
The System.Collections.Generic Namespace ................................................................376
Creating Custom Generic Methods ................................................................................385
Creating Custom Generic Structures and Classes .........................................................388
Constraining Type Parameters ......................................................................................392
Summary .......................................................................................................................395
■ Chapter 11: Delegates, Events, and Lambdas.....................................................397
Understanding the .NET Delegate Type .........................................................................397
xii


■ CONTENTS

Defining a Delegate Type in C#......................................................................................398
The System.MulticastDelegate and System.Delegate Base Classes .............................400
The Simplest Possible Delegate Example......................................................................402
Sending Object State Notifications using Delegates .....................................................405
Method Group Conversion Syntax..................................................................................411
Understanding Delegate Covariance..............................................................................413
Understanding Generic Delegates .................................................................................415
Understanding C# Events...............................................................................................418
Understanding C# Anonymous Methods........................................................................427
Understanding Lambda Expressions .............................................................................430
Summary .......................................................................................................................437
■ Chapter 12: Advanced C# Language Features ....................................................439
Understanding Indexer Methods....................................................................................439
Understanding Operator Overloading ............................................................................444
Understanding Custom Type Conversions .....................................................................454

Understanding Extension Methods ................................................................................460
Understanding Partial Methods .....................................................................................470
Understanding Anonymous Types .................................................................................473
Working with Pointer Types...........................................................................................479
Summary .......................................................................................................................487
■ Chapter 13: LINQ to Objects................................................................................489
LINQ Specific Programming Constructs.........................................................................489
Understanding the Role of LINQ.....................................................................................493
Applying LINQ Queries to Primitive Arrays.....................................................................496
Returning the Result of a LINQ Query ............................................................................503
Applying LINQ Queries to Collection Objects .................................................................505
xiii


■ CONTENTS

Investigating the C# LINQ Query Operators ...................................................................508
The Internal Representation of LINQ Query Statements ................................................517
Summary .......................................................................................................................522
■ ■ ■ Part 4: Programming with .NET Assemblies...............................................523
■ Chapter 14: Configuring .NET Assemblies ..........................................................525
Defining Custom Namespaces.......................................................................................525
The Role of .NET Assemblies .........................................................................................532
Understanding the Format of a .NET Assembly .............................................................533
Building and Consuming a Single-File Assembly ..........................................................538
Building and Consuming a Multifile Assembly...............................................................550
Understanding Private Assemblies ................................................................................553
Understanding Shared Assemblies................................................................................558
Consuming a Shared Assembly .....................................................................................567
Configuring Shared Assemblies.....................................................................................569

Understanding Publisher Policy Assemblies..................................................................574
Understanding the <codeBase> Element......................................................................576
The System.Configuration Namespace..........................................................................577
Summary .......................................................................................................................579
■ Chapter 15: Type Reflection, Late Binding, and Attribute-Based Programming ...581
The Necessity of Type Metadata....................................................................................581
Understanding Reflection ..............................................................................................586
Building a Custom Metadata Viewer..............................................................................590
Dynamically Loading Assemblies ..................................................................................596
Reflecting on Shared Assemblies ..................................................................................598
Understanding Late Binding ..........................................................................................600
Understanding the Role of .NET Attributes ....................................................................604
xiv


■ CONTENTS

Building Custom Attributes ............................................................................................609
Assembly-Level (and Module-Level) Attributes .............................................................612
Reflecting on Attributes Using Early Binding .................................................................614
Reflecting on Attributes Using Late Binding ..................................................................615
Putting Reflection, Late Binding, and Custom Attributes in Perspective .......................617
Building an Extendable Application ...............................................................................618
Summary .......................................................................................................................624
■ Chapter 16: Processes, AppDomains, and Object Contexts................................625
The Role of a Windows Process.....................................................................................625
Interacting with Processes Under the .NET Platform.....................................................627
Understanding .NET Application Domains .....................................................................637
Interacting with the Default Application Domain ...........................................................640
Creating New Application Domains ...............................................................................643

Understanding Object Context Boundaries ....................................................................648
Summarizing Processes, AppDomains, and Context .....................................................652
Summary .......................................................................................................................652
■ Chapter 17: Understanding CIL and the Role of Dynamic Assemblies................653
Reasons for Learning the Grammar of CIL.....................................................................653
Examining CIL Directives, Attributes, and Opcodes.......................................................654
Pushing and Popping: The Stack-Based Nature of CIL ..................................................656
Understanding Round-Trip Engineering.........................................................................658
Understanding CIL Directives and Attributes .................................................................666
.NET Base Class Library, C#, and CIL Data Type Mappings ...........................................673
Defining Type Members in CIL.......................................................................................674
Examining CIL Opcodes .................................................................................................677
xv


■ CONTENTS

Building a .NET Assembly with CIL ................................................................................683
Understanding Dynamic Assemblies .............................................................................688
Summary .......................................................................................................................698
■ Chapter 18: Dynamic Types and the Dynamic Language Runtime .....................701
The Role of the C# dynamic Keyword............................................................................701
The Role of the Dynamic Language Runtime (DLR) .......................................................707
Simplifying Late Bound Calls Using Dynamic Types......................................................710
Simplifying COM Interoperability using Dynamic Data ..................................................714
COM Interop using C# 4.0 Language Features ..............................................................718
Summary .......................................................................................................................723
■ ■ ■ Part 5: Introducing the .NET Base Class Libraries ......................................725
■ Chapter 19: Multithreaded and Parallel Programming.......................................727
The Process/AppDomain/Context/Thread Relationship .................................................727

A Brief Review of the .NET Delegate..............................................................................729
The Asynchronous Nature of Delegates.........................................................................731
Invoking a Method Asynchronously ...............................................................................733
The System.Threading Namespace ...............................................................................739
The System.Threading.Thread Class .............................................................................741
Programmatically Creating Secondary Threads ............................................................744
The Issue of Concurrency ..............................................................................................750
Programming with Timer Callbacks ..............................................................................758
Understanding the CLR ThreadPool ...............................................................................760
Parallel Programming under the .NET Platform.............................................................761
Parallel LINQ Queries (PLINQ) ........................................................................................771
Summary .......................................................................................................................774

xvi


■ CONTENTS

■ Chapter 20: File I/O and Object Serialization......................................................775
Exploring the System.IO Namespace.............................................................................775
The Directory(Info) and File(Info) Types.........................................................................777
Working with the DirectoryInfo Type .............................................................................778
Working with the Directory Type ...................................................................................782
Working with the DriveInfo Class Type ..........................................................................783
Working with the FileInfo Class .....................................................................................785
Working with the File Type ............................................................................................789
The Abstract Stream Class ............................................................................................792
Working with StreamWriters and StreamReaders.........................................................794
Working with StringWriters and StringReaders.............................................................798
Working with BinaryWriters and BinaryReaders............................................................799

Watching Files Programmatically ..................................................................................801
Understanding Object Serialization ...............................................................................804
Configuring Objects for Serialization .............................................................................806
Choosing a Serialization Formatter................................................................................808
Serializing Objects Using the BinaryFormatter ..............................................................811
Serializing Objects Using the SoapFormatter ................................................................813
Serializing Objects Using the XmlSerializer...................................................................814
Serializing Collections of Objects ..................................................................................816
Customizing the Soap/Binary Serialization Process ......................................................818
Summary .......................................................................................................................824
■ Chapter 21: ADO.NET Part I: The Connected Layer..............................................825
A High-Level Definition of ADO.NET...............................................................................825
Understanding ADO.NET Data Providers........................................................................827
Additional ADO.NET Namespaces ..................................................................................831
xvii


■ CONTENTS

The Types of the System.Data Namespace ...................................................................832
Abstracting Data Providers Using Interfaces .................................................................837
Creating the AutoLot Database ......................................................................................840
The ADO.NET Data Provider Factory Model ...................................................................847
Understanding the Connected Layer of ADO.NET ..........................................................853
Working with Data Readers ...........................................................................................859
Building a Reusable Data Access Library ......................................................................861
Creating a Console UI–Based Front End ........................................................................871
Understanding Database Transactions ..........................................................................877
Summary .......................................................................................................................883
■ Chapter 22: ADO.NET Part II: The Disconnected Layer........................................885

Understanding the Disconnected Layer of ADO.NET......................................................885
Understanding the Role of the DataSet..........................................................................886
Working with DataColumns ...........................................................................................889
Working with DataRows.................................................................................................893
Working with DataTables...............................................................................................897
Binding DataTable Objects to Windows Forms GUIs......................................................903
Working with Data Adapters ..........................................................................................913
Adding Disconnection Functionality to AutoLotDAL.dll ..................................................917
Multitabled DataSet Objects and Data Relationships ....................................................921
The Windows Forms Database Designer Tools..............................................................927
Isolating Strongly Typed Database Code into a Class Library........................................938
Programming with LINQ to DataSet ...............................................................................943
Summary .......................................................................................................................949
■ Chapter 23: ADO.NET Part III: The Entity Framework .........................................951
Understanding the Role of Entity Framework ................................................................951
xviii


■ CONTENTS

Building and Analyzing your First EDM ..........................................................................961
Programming Against the Conceptual Model ................................................................973
AutoLotDAL Version 4.0, Now with Entities ...................................................................979
Data Binding Entities to Windows Forms GUIs...............................................................986
Summary .......................................................................................................................991
■ Chapter 24: Introducing LINQ to XML .................................................................993
A Tale of Two XML APIs .................................................................................................993
Members of the System.Xml.Linq Namespace..............................................................997
Working with XElement and XDocument .....................................................................1002
Manipulating an in Memory XML Document................................................................1006

Summary .....................................................................................................................1011
■ Chapter 25: Introducing Windows Communication Foundation .......................1013
A Potpourri of Distributed Computing APIs ..................................................................1013
The Role of WCF...........................................................................................................1019
Investigating the Core WCF Assemblies ......................................................................1022
The Visual Studio WCF Project Templates ...................................................................1023
The Basic Composition of a WCF Application ..............................................................1025
The ABCs of WCF .........................................................................................................1027
Building a WCF Service................................................................................................1032
Hosting the WCF Service .............................................................................................1036
Building the WCF Client Application.............................................................................1046
Simplifying Configuration Settings with WCF 4.0 ........................................................1050
Using the WCF Service Library Project Template ........................................................1057
Hosting the WCF Service within a Windows Service ...................................................1061
Invoking a Service Asynchronously from the Client.....................................................1067
xix


■ CONTENTS

Designing WCF Data Contracts ....................................................................................1070
Summary .....................................................................................................................1076
■ Chapter 26: Introducing Windows Workflow Foundation 4.0 ...........................1077
Defining a Business Process .......................................................................................1078
Building a (Painfully) Simple Workflow........................................................................1079
The WF 4.0 Runtime ....................................................................................................1083
Examining the Workflow 4.0 Activities ........................................................................1088
Building a Flowchart Workflow....................................................................................1093
Isolating Workflows into Dedicated Libraries ..............................................................1103
Consuming the Workflow Library ................................................................................1112

Summary .....................................................................................................................1114
■ ■ ■ Part 6: Building Desktop User Interfaces with WPF .................................1115
■ Chapter 27: Introducing Windows Presentation Foundation and XAML ...........1117
The Motivation Behind WPF.........................................................................................1117
The Various Flavors of WPF .........................................................................................1121
Investigating the WPF Assemblies...............................................................................1126
Building a WPF Application without XAML...................................................................1135
Building a WPF Application using Only XAML ..............................................................1144
Transforming Markup into a .NET Assembly ...............................................................1150
Understanding The Syntax of WPF XAML ....................................................................1154
Building a WPF Application using Code-Behind Files ..................................................1165
Building WPF Applications Using Visual Studio 2010 ..................................................1167
Summary .....................................................................................................................1178
■ Chapter 28: Programming with WPF Controls ..................................................1179
A Survey of the Core WPF Controls..............................................................................1179
Controlling Content Layout Using Panels.....................................................................1184
xx


■ CONTENTS

Building a Window’s Frame Using Nested Panels .......................................................1195
Understanding WPF Control Commands......................................................................1200
Building a WPF User Interface with Expression Blend.................................................1207
Building the Ink API Tab...............................................................................................1216
Introducing the Documents API ...................................................................................1227
Building the Documents Tab........................................................................................1228
Introducing the WPF Data-Binding Model....................................................................1235
Summary .....................................................................................................................1244
■ Chapter 29: WPF Graphics Rendering Services ................................................1245

Understanding WPF's Graphical Rendering Services ..................................................1245
Rendering Graphical Data Using Shapes .....................................................................1247
WPF Brushes and Pens................................................................................................1257
Applying Graphical Transformations............................................................................1262
Working with Shapes using Expression Blend ............................................................1266
Rendering Graphical Data Using Drawings and Geometries........................................1271
Generating Complex Vector Graphics using Expression Design ..................................1275
Rendering Graphical Data Using the Visual Layer .......................................................1277
Summary .....................................................................................................................1284
■ Chapter 30: WPF Resources, Animations, and Styles.......................................1285
Understanding the WPF Resource System ..................................................................1285
Working with Object (Logical) Resources ....................................................................1292
Understanding WPF’s Animation Services...................................................................1303
Authoring Animations in XAML ....................................................................................1309
Understanding the Role of WPF Styles ........................................................................1312
Generating Styles with Expression Blend ....................................................................1320
Summary .....................................................................................................................1324
xxi


■ CONTENTS

■ Chapter 31: WPF Control Templates and UserControls.....................................1325
Understanding the Role of Dependency Properties .....................................................1325
Building a Custom Dependency Property ....................................................................1331
Understanding Routed Events .....................................................................................1337
Logical Trees, Visual Trees and Default Templates.....................................................1341
Building a Custom Control Template with Visual Studio 2010.....................................1348
Building Custom UserControls with Blend ...................................................................1356
Creating the Jackpot Deluxe WPF Application.............................................................1364

Summary .....................................................................................................................1375
■ ■ ■ Part 7: Building Web Applications with ASP.NET......................................1377
■ Chapter 32: Building ASP.NET Web Pages ........................................................1379
The Role of HTTP .........................................................................................................1379
Understanding Web Applications and Web Servers.....................................................1380
The Role of HTML.........................................................................................................1382
The Role of Client-Side Scripting.................................................................................1388
Posting Back to the Web Server ..................................................................................1390
The Feature Set of the ASP.NET API ............................................................................1391
Building a Single File ASP.NET Web Page ...................................................................1395
Building an ASP.NET Web Page using Code Files........................................................1406
ASP.NET Web Sites and ASP.NET Web Applications....................................................1412
The ASP.NET Web Site Directory Structure .................................................................1413
The Inheritance Chain of the Page Type ......................................................................1416
Interacting with the Incoming HTTP Request ..............................................................1417
Interacting with the Outgoing HTTP Response ............................................................1421
The Life Cycle of an ASP.NET Web Page......................................................................1423
xxii


■ CONTENTS

The Role of the Web.config File ...................................................................................1427
Summary .....................................................................................................................1428
■ Chapter 33: ASP.NET Web Controls, Master Pages and Themes ......................1429
Understanding the Nature of Web Controls .................................................................1429
The Control and WebControl Base Classes..................................................................1432
Major Categories of ASP.NET Web Controls ................................................................1438
Building the ASP.NET Cars Web Site ...........................................................................1441
The Role of the Validation Controls..............................................................................1457

Working with Themes..................................................................................................1465
Summary .....................................................................................................................1471
■ Chapter 34: ASP.NET State Management Techniques.......................................1473
The Issue of State ........................................................................................................1473
ASP.NET State Management Techniques ....................................................................1476
Understanding the Role of ASP.NET View State ..........................................................1476
The Role of the Global.asax File...................................................................................1479
Understanding the Application/Session Distinction.....................................................1483
Working with the Application Cache............................................................................1488
Maintaining Session Data ............................................................................................1493
Understanding Cookies................................................................................................1497
The Role of the <sessionState> Element ....................................................................1500
Understanding the ASP.NET Profile API .......................................................................1502
Summary .....................................................................................................................1509
■ Appendix A: Programming with Windows Forms .............................................1511
The Windows Forms Namespaces...............................................................................1512
Building a Simple Windows Forms Application ...........................................................1513
The Visual Studio Windows Forms Project Template ..................................................1518
xxiii


×