Laurent Bugnion Silverlight™ 4 U N L E A S H E D 800 East 96th Street,Indianapolis,Indiana 46240 USA Silverlight™ 4 Unleashed Editor-in-Chief Copyright ©2011 by Pearson Education Karen Gettman All rights reserved. No part of this book shall be reproduced,stored in a retrieval Executive Editor system,or transmitted by any means,electronic,mechanical,photocopying,recording, Neil Rowe or otherwise,without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every Development Editor precaution has been taken in the preparation of this book,the publisher and author Mark Renfrow assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein. Managing Editor ISBN-13: 978-0-672-33336-8 Kristy Hart ISBN-10: 0-672-33336-1 Project Editor Library of Congress Cataloging-in-Publication Data Andy Beaster Bugnion,Laurent. Silverlight 4 unleashed / Laurent Bugnion. Copy Editor p. cm. Keith Cline Includes index. Indexer ISBN 978-0-672-33336-1 1. Silverlight (Electronic resource) 2. Multimedia systems. 3. Application software— Brad Herriman Development. 4. Web site development. 5. Internet programming. 6. User interfaces Proofreader (Computer systems) I. Title. Jennifer Gallant QA76.575.B839 2011 006.7’6—dc22 Technical Editor 2010040175 Peter Bromberg Printed in the United States on America Publishing First Printing October 2010 Coordinator Trademarks Cindy Teeters All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of Book Designer this information. Use of a term in this book should not be regarded as affecting the Gary Adair validity of any trademark or service mark. Composition Warning and Disclaimer Gloria Schurick Every effort has been made to make this book as complete and as accurate as possi- ble,but no warranty or fitness is implied. The information provided is on an “as is” basis. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book or from the use of the programs accompanying it. Bulk Sales Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information,please contact U.S. Corporate and Government Sales 1-800-382-3419 [email protected] For sales outside of the U.S.,please contact International Sales [email protected] Contents at a Glance Foreword....................................................................................................xxiii Introduction....................................................................................................1 1 Three Years of Silverlight................................................................................7 2 Setting Up and Discovering Your Environment .........................................27 3 Extending Your Application with Controls .................................................49 4 Investigating Existing Controls ...................................................................75 5 Understanding Dependency Properties .....................................................103 6 Working with Data: Binding, Grouping, Sorting, and Filtering ...............129 7 Understanding the Model-View-ViewModel Pattern ................................157 8 Using Data Controls ..................................................................................187 9 Connecting to the Web .............................................................................215 10 Creating Resources, Styles, and Templates ................................................247 11 Mastering Expression Blend ......................................................................273 12 Sketching User Experience .........................................................................301 13 Creating Line-of-Business Applications .....................................................329 14 Enhancing Line-of-Business Applications and Running Out of the Browser .....................................................................365 15 Developing Navigation Applications and Silverlight for Windows Phone 7 ......................................................................................401 16 Using Effects and Recording Media ...........................................................435 17 New Transforms, Right Click, HTML Browser, WebBrowserBrush, and Isolated Storage...........................................................................................467 18 Drag and Drop, Full Screen, Clipboard, COM Interop, Duplex Polling, Notification Windows, and Splash Screens................................................503 19 Authentication, Event to Command Binding, Random Animations, Multitouch, Local Communication, and Bing Maps Control...................539 20 Building Extensible and Maintainable Applications .................................573 21 Optimizing Performance ...........................................................................609 22 Advanced Development Techniques..........................................................641 Conclusion..................................................................................................675 Index...........................................................................................................677 Table of Contents Foreword xxiii Introduction 1 1 Three Years of Silverlight 7 Discovering Silverlight 4.................................................................................7 Learning Silverlight Is Betting on the Future.......................................8 How Can They Be So Fast?....................................................................9 How About Compatibility with Older Versions?..................................9 Cross-Browser and Cross-Platform Compatibility........................................10 Windows and Macintosh....................................................................11 Linux....................................................................................................12 Alternatives to Silverlight.............................................................................12 In the Web Browser.............................................................................12 Out of the Browser..............................................................................13 Legacy Technologies............................................................................14 A Short History of Silverlight.......................................................................15 Silverlight 1.0.......................................................................................16 Silverlight 2..........................................................................................16 Silverlight 3..........................................................................................16 And Silverlight 4…..............................................................................17 Previewing the Future of Silverlight.............................................................17 Installing Silverlight 4 as a User...................................................................18 Opening a Silverlight 4 Application...................................................19 What to Do If Silverlight Is Not Installed?.........................................19 Exploring Silverlight 4 Demos......................................................................19 Deep Zooming the Matterhorn...........................................................19 Getting Involved Socially with Sobees................................................20 Navigating with Bing Maps Streetside and PhotoSynth.....................21 Visualizing Information with the Pivot Viewer..................................23 Drawing on the Web with Fantasia.....................................................25 How Can You Get Involved?........................................................................25 Summary.......................................................................................................26 Contents v 2 Setting Up and Discovering Your Environment 27 Installing Visual Studio.................................................................................27 Visual Studio 2010...............................................................................27 Visual Web Developer Express............................................................28 Installing the Silverlight Tools for Visual Studio................................28 Verifying the Installation....................................................................29 Inspecting the Application..................................................................31 Unpacking an XAP File........................................................................32 Using the Visual Studio Designer........................................................32 Implementing Hello Silverlight....................................................................33 Checking the Properties Editor...........................................................34 Adding Some Text................................................................................36 Using Design Time Width and Height................................................39 Saving the Application........................................................................40 Installing Expression Blend..........................................................................40 Creating a New Silverlight Application..............................................40 Opening Hello Silverlight....................................................................41 Adding an Effect..................................................................................42 Creating a Pulse Animation................................................................44 Triggering the Storyboard....................................................................45 Testing the Application.......................................................................46 Summary.......................................................................................................47 3 Extending Your Application with Controls 49 Extending XAML...........................................................................................49 Mapping a Prefix to a CLR Namespace...............................................49 Why Is a Prefix Not Always Needed?..................................................52 Adding a Namespace to Any Element.................................................52 Defining Your Own URI and Mapping CLR Namespaces...................52 What’s a Control?.........................................................................................53 User Controls.......................................................................................53 Custom Controls.................................................................................63 Summary.......................................................................................................73 4 Investigating Existing Controls 75 Reviewing the Basics.....................................................................................75 Show Me Some Code!...................................................................................76 Changes in Existing Controls.......................................................................77 Mouse Wheel Support.........................................................................77 Localizing for Right-to-Left Languages...............................................77 Getting a Control Template’s Current State.......................................78 vi Silverlight 4 Unleashed AddingSelectedValueandSelectedValuePath................................79 AddingCommandandCommandParameter.............................................79 Presenting and Editing Text with the RichTextBox.....................................84 Zooming with the Viewbox...........................................................................85 Opening a ChildWindow................................................................................87 Finding More Information............................................................................90 Where to Find Additional Controls?............................................................90 Do You Really Need a Control?...........................................................90 The Silverlight Toolkit.........................................................................91 Installing the Silverlight Toolkit.........................................................92 Third-Party Providers.........................................................................101 Summary.....................................................................................................102 5 Understanding Dependency Properties 103 InheritingDependencyObject.....................................................................104 Threading...........................................................................................104 Accessing a Dependency Property’s Value........................................105 Using a DependencyObjectas Data Item...........................................105 Registering Dependency Properties............................................................108 Defining Metadata.............................................................................109 Initializing Dependency Objects.......................................................111 Understanding Attached Properties............................................................114 Using Attached Properties for Values................................................114 Registering an Attached Property......................................................115 Using Custom Attached Properties in XAML...................................118 Implementing an Attached Behavior................................................118 Building on Attached Behaviors with Blend Behaviors....................122 Adding a New Property with Snippets.......................................................123 Installing the Snippets for Silverlight...............................................123 Using the Snippets.............................................................................124 Calculating a Dependency Property’s Value..............................................124 Getting the Property’s Base Value.....................................................126 Reading the Local Value....................................................................126 Summary.....................................................................................................127 6 Working with Data: Binding,Grouping,Sorting,and Filtering 129 Diving into Data Bindings..........................................................................130 Understanding a Binding’s Elements................................................130 Understanding the Namescope.........................................................132 Setting the Source..............................................................................133 Refining the Path...............................................................................136 Contents vii Flowing in Two Directions................................................................138 Converting the Values.......................................................................138 Changing the Format........................................................................139 Handling Special Cases......................................................................141 Property Trigger.................................................................................142 Validating Input.................................................................................142 Using the Visual Studio Binding Dialog.....................................................146 Using the Expression Blend Binding Dialog..............................................148 Debugging Data Bindings...........................................................................149 Checking the Output Tab..................................................................149 Creating a Test Converter..................................................................150 Grouping, Filtering, and Sorting Data........................................................151 Working with the CollectionViewSource........................................151 Using a PagedCollectionView...........................................................154 Binding Directly to the Source..........................................................154 Summary.....................................................................................................155 7 Understanding the Model-View-ViewModel Pattern 157 About Design Patterns................................................................................157 Separating the Concerns.............................................................................158 Why Is Separation Good?..................................................................158 Classic Separation Patterns................................................................158 History of MVVM........................................................................................159 Developing Expression Blend............................................................160 Presentation Model for WPF and Silverlight....................................160 Architecture of MVVM................................................................................160 Translating to Silverlight...................................................................161 Two Kinds of View-models................................................................162 Binding the View to the View-model.........................................................163 Understanding the Data Context......................................................163 Inheriting the Data Context.............................................................164 Binding to the View...........................................................................164 Building a Sample Application...................................................................168 The Model’s Interface........................................................................168 Building a CustomerViewModel..........................................................170 Calling the Service in the MainViewModel.........................................172 Binding to Results..............................................................................174 Testing the Application.....................................................................177 Bridging the Separation..............................................................................178 Implementing a ViewModelBaseClass..............................................178 Using Commands..............................................................................180 Sending Messages..............................................................................183 viii Silverlight 4 Unleashed Using an MVVM Framework......................................................................184 What Could Be Better?...............................................................................184 Summary.....................................................................................................184 8 Using Data Controls 187 Filtering and Paging with the PagedCollectionView................................188 Preparing the Sample.........................................................................188 Building the PagedCollectionView...................................................190 Filtering Data.....................................................................................192 Paging Through Data.........................................................................194 Optimizing Data Handling................................................................195 Implementing Custom Sorting.........................................................195 Adding a DataPagerControl......................................................................196 Customizing the Display...................................................................197 Validating Data Input.................................................................................197 Using Interface-Based Validation......................................................198 Validating with Data Annotations....................................................201 Validating Before or After the Data Is Set.........................................203 Validating on the Client and on the Server......................................203 Reviewing the DataGrid..............................................................................204 Using the DataGridwith Automatic Columns.................................204 Choosing Between DataGridandListBox.......................................208 Editing Data in the DataForm.....................................................................208 Adding a Description.........................................................................210 Validating the Input..........................................................................211 Committing Changes Manually........................................................211 Defining Fields Manually..................................................................212 Getting More Information................................................................213 Making a Simple Property Editor......................................................213 Summary.....................................................................................................214 9 Connecting to the Web 215 Getting Information from Cross-Domain Servers......................................216 Checking Whether a Policy File Exists..............................................216 Working Around Cross-Domain Restrictions....................................217 Placing Simple Calls....................................................................................218 Informing the User............................................................................218 Learning with a Sample.....................................................................219 Downloading Strings.........................................................................219 Detecting Errors, Checking the Result..............................................223 Opening a Resource for Reading.......................................................224 Uploading a String.............................................................................225 Contents ix Opening a Resource for Writing........................................................227 Accessing Headers..............................................................................231 Sending Complex Messages........................................................................231 Posting a File to the Server with HttpWebRequest............................231 Discovering the New Networking Stack.....................................................234 Using the Client HTTP Stack.............................................................235 Using Other HTTP Methods..............................................................236 Using the CookieContainer...............................................................236 Handling Responses....................................................................................237 Handling XML Responses..................................................................238 Handling JSON Responses.................................................................238 Communicating with WCF........................................................................239 Setting Up a Service...........................................................................239 Connecting the Client Application...................................................241 Updating the Code on the Server.....................................................244 Publishing the Service.......................................................................244 Summary.....................................................................................................245 10 Creating Resources,Styles,and Templates 247 Working with Resources in XAML.............................................................248 Using Local Resources........................................................................248 Merging Dictionaries.........................................................................249 From the Same Assembly..................................................................250 From a Different Assembly................................................................251 Resolving Resources...........................................................................254 Working with Resources in Blend..............................................................256 Merging a Resource Dictionary.........................................................256 Creating New Resources....................................................................257 Selecting a Resource for a Property...................................................257 Using the Resources Panel.................................................................257 Cleaning Up Unused Resources..................................................................259 Using the Pistachio Tool....................................................................259 Styling a Control.........................................................................................260 Using Implicit Styles..........................................................................261 Creating a Hierarchy of Styles...........................................................263 Creating a New Style in Blend..........................................................264 Templating a Control..................................................................................265 Copying a Template in Blend............................................................265 Creating a Custom Easing Function.................................................269 Making a Control in Blend...............................................................270 Applying a Theme.......................................................................................271 Summary.....................................................................................................272