Pro Silverlight 4 in C# ■ ■ ■ Matthew MacDonald Pro Silverlight 4 in C# Copyright © 2010 by Matthew MacDonald All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright the publisher. ISBN-13 (pbk): 978-1-4302-2529-2979-7 ISBN-13 (electronic): 978-1-4302-2980-3 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Ewan Buckingham Technical Reviewer: Fabio Claudio Ferracchiati Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editors: Anne Collett and Debra Kelly Copy Editor: Kim Wimpsett Compositor: Mary Sudul Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], 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. For my family ■ CONTENTS Contents at a Glance ■ Contents......................................................................................................................................vi ■ About the Author.....................................................................................................................xxii ■ About the Technical Reviewer..............................................................................................xxiii ■ Acknowledgments..................................................................................................................xxiv ■ Introduction............................................................................................................................xxv ■ Chapter 1: Introducing Silverlight.............................................................................................1 ■ Chapter 2: XAML.......................................................................................................................33 ■ Chapter 3: Layout......................................................................................................................61 ■ Chapter 4: Dependency Properties and Routed Events.......................................................107 ■ Chapter 5: Elements................................................................................................................141 ■ Chapter 6: The Application Model.........................................................................................197 ■ Chapter 7: Navigation.............................................................................................................227 ■ Chapter 8: Shapes and Geometries........................................................................................259 ■ Chapter 9: Brushes, Transforms, and Bitmaps.....................................................................295 ■ Chapter 10: Animation............................................................................................................339 ■ Chapter 11: Sound, Video, and Deep Zoom..........................................................................399 ■ Chapter 12: Styles and Behaviors...........................................................................................447 ■ Chapter 13: Templates and Custom Controls......................................................................471 ■ Chapter 14: Browser Integration............................................................................................513 ■ Chapter 15: ASP.NET Web Services.......................................................................................539 ■ Chapter 16: Data Binding.......................................................................................................569 ■ Chapter 17: Data Controls......................................................................................................619 ■ Chapter 18: File Access...........................................................................................................669 ■ Chapter 19: Multithreading....................................................................................................695 ■ Chapter 20: Networking..........................................................................................................717 ■ Chapter 21: Out-of-Browser Applications.............................................................................767 ■ Index.........................................................................................................................................809 v ■ CONTENTS Contents ■ Contents at a Glance...................................................................................................................v ■ About the Author.....................................................................................................................xxii ■ About the Technical Reviewer..............................................................................................xxiii ■ Acknowledgments..................................................................................................................xxiv ■ Introduction............................................................................................................................xxv ■ Chapter 1: Introducing Silverlight.............................................................................................1 Silverlight Design Tools.............................................................................................................1 Visual Studio vs. Expression Blend......................................................................................1 Understanding Silverlight Websites....................................................................................2 Creating a Stand-Alone Silverlight Project...............................................................................3 Creating a Simple Silverlight Page............................................................................................6 Adding Event-Handling Code..............................................................................................8 Testing a Silverlight Application........................................................................................10 Creating an ASP.NET-Hosted Silverlight Project..................................................................12 ASP.NET Controls That Render Silverlight Content........................................................15 Mixing ASP.NET Controls and Silverlight Content..........................................................16 Silverlight Compilation and Deployment..............................................................................16 Compiling a Silverlight Application..................................................................................16 Deploying a Silverlight Application...................................................................................18 Silverlight Core Assemblies................................................................................................20 Silverlight Add-on Assemblies...........................................................................................21 Assembly Caching...............................................................................................................22 The HTML Entry Page..............................................................................................................24 Sizing the Silverlight Content Region................................................................................26 Silverlight Parameters.........................................................................................................26 Alternative Content.............................................................................................................28 Creating a Friendly Install Experience..............................................................................28 vi ■ CONTENTS The Mark of the Web...........................................................................................................30 The Last Word..........................................................................................................................31 ■ Chapter 2: XAML.......................................................................................................................33 XAML Basics.............................................................................................................................34 XAML Namespaces.............................................................................................................35 The Code-Behind Class......................................................................................................38 Properties and Events in XAML..............................................................................................39 Simple Properties and Type Converters...........................................................................41 Complex Properties............................................................................................................42 Attached Properties............................................................................................................44 Nesting Elements................................................................................................................45 Events...................................................................................................................................48 The Full Eight Ball Example...............................................................................................49 XAML Resources......................................................................................................................50 The Resources Collection...................................................................................................50 The Hierarchy of Resources...............................................................................................51 Accessing Resources in Code.............................................................................................53 Organizing Resources with Resource Dictionaries..........................................................54 Element-to-Element Binding..................................................................................................56 One-Way Binding................................................................................................................56 Two-Way Binding...............................................................................................................57 The Last Word..........................................................................................................................59 ■ Chapter 3: Layout......................................................................................................................61 The Layout Containers............................................................................................................61 The Panel Background........................................................................................................63 Borders.................................................................................................................................65 Simple Layout with the StackPanel........................................................................................66 Layout Properties................................................................................................................68 Alignment............................................................................................................................69 Margins................................................................................................................................70 Minimum, Maximum, and Explicit Sizes..........................................................................72 The WrapPanel and DockPanel..............................................................................................74 The WrapPanel....................................................................................................................74 The DockPanel....................................................................................................................75 vii ■ CONTENTS The Grid....................................................................................................................................77 Fine-Tuning Rows and Columns.......................................................................................79 Nesting Layout Containers.................................................................................................80 Spanning Rows and Columns............................................................................................82 The GridSplitter...................................................................................................................83 Coordinate-Based Layout with the Canvas...........................................................................86 Layering with ZIndex..........................................................................................................88 Clipping...............................................................................................................................88 Custom Layout Containers.....................................................................................................90 The Two-Step Layout Process............................................................................................90 The UniformGrid................................................................................................................93 Sizing Pages..............................................................................................................................98 Scrolling with the ScrollViewer........................................................................................100 Scaling with the Viewbox..................................................................................................102 Full-Screen Mode..............................................................................................................104 The Last Word........................................................................................................................106 ■ Chapter 4: Dependency Properties and Routed Events.......................................................107 Dependency Properties.........................................................................................................107 Defining and Registering a Dependency Property.........................................................108 Dynamic Value Resolution...............................................................................................110 Attached Properties..........................................................................................................111 The WrapBreakPanel Example........................................................................................112 Routed Events.........................................................................................................................115 The Core Element Events.................................................................................................115 Event Bubbling..................................................................................................................118 Handled (Suppressed) Events..........................................................................................120 An Event Bubbling Example.............................................................................................120 Mouse Handling.....................................................................................................................123 Right-Clicks.......................................................................................................................123 Mouse Movements...........................................................................................................124 The Mouse Wheel.............................................................................................................124 Capturing the Mouse........................................................................................................126 A Mouse Event Example...................................................................................................127 Mouse Cursors..................................................................................................................130 viii ■ CONTENTS Keyboard Handling................................................................................................................131 Key Presses........................................................................................................................131 Key Modifiers.....................................................................................................................134 Focus..................................................................................................................................135 The Command Model............................................................................................................136 Building a Command........................................................................................................137 Connecting a Command..................................................................................................138 The Last Word........................................................................................................................140 ■ Chapter 5: Elements................................................................................................................141 The Silverlight Elements........................................................................................................141 Static Text...............................................................................................................................145 Font Properties..................................................................................................................146 Underlining.......................................................................................................................150 Runs...................................................................................................................................151 Wrapping Text...................................................................................................................152 Trimming Text...................................................................................................................153 Images.....................................................................................................................................153 Image Sizing......................................................................................................................154 Image Errors......................................................................................................................155 Content Controls....................................................................................................................155 The Content Property.......................................................................................................157 Aligning Content...............................................................................................................159 Buttons....................................................................................................................................160 The HyperlinkButton........................................................................................................160 The ToggleButton and RepeatButton.............................................................................161 The CheckBox....................................................................................................................161 The RadioButton...............................................................................................................162 Tooltips and Pop-Ups............................................................................................................163 Customized Tooltips.........................................................................................................163 The Popup.........................................................................................................................165 Items Controls........................................................................................................................167 The ListBox........................................................................................................................167 The ComboBox..................................................................................................................169 The TabControl.................................................................................................................170 ix ■ CONTENTS Text Controls..........................................................................................................................171 The TextBox.......................................................................................................................172 The PasswordBox..............................................................................................................174 The AutoCompleteBox.....................................................................................................174 The RichTextBox...............................................................................................................180 Range-Based Controls...........................................................................................................191 The Slider...........................................................................................................................191 The ProgressBar................................................................................................................192 Date Controls.........................................................................................................................192 The Last Word........................................................................................................................196 ■ Chapter 6: The Application Model.........................................................................................197 The Application Class............................................................................................................197 Accessing the Current Application..................................................................................198 Application Properties......................................................................................................198 Application Events.................................................................................................................199 Application Startup...........................................................................................................200 Initialization Parameters..................................................................................................200 Application Shutdown......................................................................................................204 Unhandled Exceptions.....................................................................................................204 Custom Splash Screens.........................................................................................................206 Binary Resources....................................................................................................................211 Placing Resources in the Application Assembly.............................................................211 Placing Resources in the Application Package...............................................................214 Placing Resources on the Web.........................................................................................215 Class Library Assemblies.......................................................................................................219 Using Resources in an Assembly.....................................................................................219 Downloading Assemblies on Demand............................................................................220 Supporting Assembly Caching.........................................................................................221 The Last Word........................................................................................................................225 ■ Chapter 7: Navigation.............................................................................................................227 Loading User Controls...........................................................................................................228 Embedding User Controls in a Page................................................................................228 Hiding Elements................................................................................................................230 Managing the Root Visual................................................................................................230 x ■ CONTENTS Retaining Page State.........................................................................................................232 Browser History.................................................................................................................233 Child Windows.......................................................................................................................233 Designing a ChildWindow................................................................................................235 Showing a ChildWindow..................................................................................................236 The Frame and Page..............................................................................................................238 Frames...............................................................................................................................238 URI Mapping.....................................................................................................................245 Forward and Backward Navigation.................................................................................246 Hyperlinks.........................................................................................................................247 Pages..................................................................................................................................248 Navigation Templates.......................................................................................................251 Custom Content Loaders......................................................................................................252 Authentication and Navigation.......................................................................................253 Creating a Custom Content Loader.................................................................................254 Using the Custom Content Loader..................................................................................256 The Last Word........................................................................................................................257 ■ Chapter 8: Shapes and Geometries........................................................................................259 Basic Shapes...........................................................................................................................259 The Shape Classes.............................................................................................................260 Rectangle and Ellipse........................................................................................................261 Sizing and Placing Shapes................................................................................................263 Sizing Shapes Proportionately with a Viewbox..............................................................265 Line.....................................................................................................................................267 Polyline..............................................................................................................................268 Polygon..............................................................................................................................269 Line Caps and Line Joins..................................................................................................272 Dashes................................................................................................................................274 Paths and Geometries............................................................................................................276 Line, Rectangle, and Ellipse Geometries.........................................................................277 Combining Shapes with GeometryGroup.......................................................................278 Curves and Lines with PathGeometry.............................................................................280 The Geometry Mini-Language.........................................................................................286 Clipping with Geometry...................................................................................................288 xi