Pro Business Applications with Silverlight 4 (cid:2) (cid:2) (cid:2) Chris Anderson Pro Business Applications with Silverlight 4 Copyright © 2010 by Chris Anderson 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-7207-6 ISBN-13 (electronic): 978-1-4302-7206-9 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: Jon Hassell Technical Reviewer: 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: Tracy Brown Copy Editors: Julie Hammond, Damon Larson, Heather Lang, Kim Wimpsett. Compositor: Bytheway Publishing Services Indexer: Toma Mulligan 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. For my grandmother Connie, a great role model for will power, optimism, generousity, patience, and love. I miss her dearly. Contents at a Glance (cid:2) Contents at a Glance..............................................................................................iv (cid:2) Contents..................................................................................................................v (cid:2) About the Author..................................................................................................xix (cid:2) Acknowledgments................................................................................................xx (cid:2) Chapter 1: Introduction...........................................................................................1 (cid:2) Chapter 2: Getting Started with Silverlight...........................................................13 (cid:2) Chapter 3: An Introduction to XAML.....................................................................35 (cid:2) Chapter 4: The Navigation Framework.................................................................73 (cid:2) Chapter 5: Exposing Data from the Server: Using WCF RIA Services....................93 (cid:2) Chapter 6: Implementing Summary Lists...........................................................141 (cid:2) Chapter 7: Building Data Entry Forms................................................................189 (cid:2) Chapter 8: Securing Your Application.................................................................245 (cid:2) Chapter 9: Styling Your Application....................................................................269 (cid:2) Chapter 10: Advanced XAML and Data Binding..................................................287 (cid:2) Chapter 11: Creating Custom Controls ...............................................................331 (cid:2) Chapter 12: The Model-View-ViewModel (MVVM) Design Pattern......................373 (cid:2) Chapter 13: Printing and Reporting....................................................................403 (cid:2) Chapter 14: Out of Browser Mode, and Interacting with the Operating System...............................................................................................427 (cid:2) Chapter 15: Application Deployment..................................................................483 (cid:2) Index...................................................................................................................513 iv (cid:2) C Contents (cid:2) Contents at a Glance..............................................................................................iv (cid:2) Contents..................................................................................................................v (cid:2) About the Author..................................................................................................xix (cid:2) Acknowledgments................................................................................................xx (cid:2) Chapter 1: Introduction...........................................................................................1 Who This Book Is For.........................................................................................................1 About This Book.................................................................................................................2 About the Author................................................................................................................3 Silverlight Overview...........................................................................................................4 What Is Silverlight?...................................................................................................................................4 A Short History of Silverlight....................................................................................................................6 What Can Silverlight Bring to Your Business Application?.......................................................................7 When Should You Not Use Silverlight?.....................................................................................................8 Comparing Silverlight to Other Microsoft Platforms.................................................................................9 Comparison with Adobe Flash/Flex........................................................................................................11 Business Applications Overview......................................................................................11 Summary.........................................................................................................................12 (cid:2) Chapter 2: Getting Started with Silverlight...........................................................13 Required Tools.................................................................................................................13 Visual Studio...........................................................................................................................................13 Expression Blend 4 and SketchFlow......................................................................................................14 v (cid:2) CONTENTS Silverlight 4 Tools...................................................................................................................................15 WCF RIA Services...................................................................................................................................15 Silverlight Toolkit....................................................................................................................................15 SQL Server 2008 Express Edition...........................................................................................................15 Silverlight Spy (and .NET Reflector)........................................................................................................16 Creating a Silverlight Application....................................................................................16 Silverlight Application.............................................................................................................................17 Silverlight Navigation Application...........................................................................................................18 Silverlight Business Application.............................................................................................................18 Silverlight Class Library..........................................................................................................................18 WCF RIA Services Class Library..............................................................................................................19 Silverlight Unit Test Application..............................................................................................................19 Running and Exploring the Default Silverlight Business Application Project...................19 Exploring the Initial Silverlight Project Structure.............................................................22 Project Links...........................................................................................................................................24 The Web Application Project Structure...................................................................................................26 The Silverlight Application Project Structure..........................................................................................28 Recommended Project Template Modifications..............................................................31 XAP Files..........................................................................................................................32 Summary.........................................................................................................................33 (cid:2) Chapter 3: An Introduction to XAML.....................................................................35 Overcoming XAML’s Steep Learning Curve.....................................................................35 Why Learn XAML?............................................................................................................36 XAML Syntax, Document Structure, and Features...........................................................37 Core XAML Syntax..................................................................................................................................37 Creating an Object Hierarchy..................................................................................................................38 Namespaces...........................................................................................................................................39 Assigning Property Values to Controls....................................................................................................41 vi (cid:2) CONTENTS Attached Properties................................................................................................................................44 XAML Namespace Properties.................................................................................................................45 Design-Time Properties..........................................................................................................................46 Markup Extensions.................................................................................................................................47 Namescopes...........................................................................................................................................49 Controls...........................................................................................................................50 Base Control Classes..............................................................................................................................51 Layout Controls.......................................................................................................................................53 XAML vs. Windows Forms Controls’ Property Names............................................................................57 Assigning Event Handlers.......................................................................................................................58 Creating a Simple User Interface............................................................................................................60 Resources and Resource Dictionaries.............................................................................62 Styles...............................................................................................................................64 Templates........................................................................................................................65 Data Binding....................................................................................................................65 Binding to an Object...............................................................................................................................66 Binding to a Collection............................................................................................................................69 Designing User Experiences............................................................................................70 The Designer/Developer Workflow..................................................................................71 XAML User Interfaces: A Different Perspective................................................................71 Summary.........................................................................................................................72 (cid:2) Chapter 4: The Navigation Framework.................................................................73 Getting Started with the Navigation Framework.............................................................73 Components of the Navigation Framework.....................................................................75 The Frame Control..................................................................................................................................75 The Page Class.......................................................................................................................................76 The NavigationService Object.................................................................................................................76 The NavigationContext Object................................................................................................................77 vii (cid:2) CONTENTS Navigating Between Views..............................................................................................77 View URIs................................................................................................................................................77 Navigation Methods on the Frame Control.............................................................................................78 Navigation Methods on the NavigationService Object............................................................................78 Using a HyperlinkButton Control.............................................................................................................79 Using the Source Property of the Frame Control....................................................................................79 User-Initiated Navigation........................................................................................................................79 Passing Data Between Views..........................................................................................80 Passing Data Using Query String Parameters........................................................................................80 Reading Query String Parameters..........................................................................................................81 Passing Complex Data Types Between Views........................................................................................82 Deep Links.......................................................................................................................82 URI Mapping to Enable Friendly URIs...............................................................................83 Integrating with the Browser History...............................................................................85 Handling Navigation Events.............................................................................................87 Frame Events..........................................................................................................................................87 View Events............................................................................................................................................88 Caching Views.................................................................................................................89 Visual Transition Effects..................................................................................................90 Alternative User Interface Frameworks...........................................................................91 Summary.........................................................................................................................91 (cid:2) Chapter 5: Exposing Data from the Server: Using WCF RIA Services....................93 What Is WCF RIA Services?..............................................................................................94 How the WCF RIA Services Code Generator Works..........................................................95 How Do You Use WCF RIA Services?...............................................................................96 Linking Your Silverlight and Web Projects..............................................................................................97 Creating Your Domain Services..............................................................................................................97 Creating Domain Operations on Your Domain Services..........................................................................97 viii (cid:2) CONTENTS Consuming the Domain Services............................................................................................................98 Getting Started.................................................................................................................98 Creating a Data Access Layer Using the Entity Framework...........................................101 Configuring the Database.....................................................................................................................101 About the Entity Framework.................................................................................................................101 Creating an Entity Model......................................................................................................................102 Domain Services............................................................................................................102 Understanding the Domain Service Life Cycle......................................................................................103 Creating a Domain Service...................................................................................................................104 Domain Operations........................................................................................................107 Query Operations..................................................................................................................................107 Insert/Update/Delete Operations..........................................................................................................109 Invoke Operations.................................................................................................................................111 Custom Operations...............................................................................................................................111 Decorating Entities........................................................................................................112 Metadata Classes.................................................................................................................................113 Controlling Client Entity Generation......................................................................................................113 Validation..............................................................................................................................................115 Presentation.........................................................................................................................................118 Miscellaneous Data Annotations..........................................................................................................118 Presentation Model Types.............................................................................................119 Creating the Presentation Model Class.................................................................................................120 Populating and Exposing Your Presentation Model Types....................................................................121 Updating Your Presentation Model Types.............................................................................................122 Sharing Code/Logic Across Tiers...................................................................................123 Inspecting the Generated Code in the Silverlight Project..............................................124 Encapsulating Logic in a Separate WCF RIA Services Class Library..............................125 Handling Server Errors..................................................................................................126 ix (cid:2) CONTENTS Handling Data Concurrency Violations...........................................................................127 Methods of Identifying Concurrency Violations....................................................................................127 Configuring the Entity Model to Check for Concurrency Violations......................................................128 Testing Your Solution...........................................................................................................................129 Resolving Conflicts in the Domain Service...........................................................................................130 Transactions..................................................................................................................131 WCF RIA Services Toolkit...............................................................................................132 Alternative Communication Technologies.....................................................................133 WCF Services........................................................................................................................................134 WCF Data Services...............................................................................................................................137 HTTP Requests.....................................................................................................................................138 Sockets.................................................................................................................................................139 Summary.......................................................................................................................139 (cid:2) Chapter 6: Implementing Summary Lists...........................................................141 Exposing a Summary Collection from the Server..........................................................142 Collections and Collection Views...................................................................................144 ObservableCollection<T>.....................................................................................................................144 Views....................................................................................................................................................144 Consuming Data from the Server..................................................................................146 Understanding the Domain Context......................................................................................................146 XAML-Based Approach.........................................................................................................................146 Code-Based Approach..........................................................................................................................148 Choosing the Right Approach...............................................................................................................149 Explicitly Specifying a Domain Service Host........................................................................................150 Using the BusyIndicator Control...........................................................................................................150 Displaying the Summary List.........................................................................................152 Retrieving the Data from the Server.....................................................................................................153 Configuring and Customizing a Control for Displaying Data.................................................................153 x