CYAN YELLOW MAGENTA BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS® THE EXPERT’S VOICE® IN SILVERLIGHT Companion eBook Available Pro Silverlight 4 in VB Pro Silverlight is a revolutionary browser plug-in that allows developers to create rich S client applications that run inside the browser. Like Adobe Flash, Silverlight sup- ports event handling, two-dimensional drawing, video playback, networking, and Pro animation. Unlike Flash, Silverlight is tailored to .NET developers and supports i pure Visual Basic code. l The most exciting part of Silverlight is its cross-platform muscle. Unlike ordi- v Matthew MacDonald, nary .NET applications, Silverlight applications run seamlessly in non-Microsoft author of browsers (such as Firefox, Safari, and Chrome) and on non-Microsoft platforms e Silverlight 4 Pro WPF in VB 2010 (such as Mac OS X). Essentially, Silverlight is a scaled-down, browser-hosted ver- Pro ASP.NET 4 in VB 2010 sion of .NET—and that’s made it the most hotly anticipated technology that r (with Adam Freeman and Microsoft has released in years. Mario Szpuszta) In this book, you’ll master Silverlight from the ground up. No Silverlight expe- l Beginning ASP.NET 4 in VB rience is required—but if you’ve worked with Silverlight 3, you’ll appreciate the i 2010 “What’s New” boxes that point out new features at the start of every chapter. By g Your Brain: The Missing the end of this book, you’ll have a solid understanding of the complete Silverlight Manual platform, and you’ll be able to build anything from a slick business front end to a h browser-based game. VB t in THE APRESS ROADMAP 4 Pro Introducing Pro Business Applications Silverlight 4 Silverlight 4 in VB with Silverlight 4 i n Pro Beginning Pro Windows Phone 7 ASP.NET in VB 2010 WPF in VB 2010 Development V Create cross-platform .NET applications Companion eBook for the browser B THIRD EDITION See last page for details on $10 eBook version THIRD EDITION M a c Matthew MacDonald SOURCE CODE ONLINE D www.apress.com ISBN 978-1-4302-3548-4 o n 55999 a l d Shelve in: .NET User level: 9 781430 235484 Intermediate–Advanced 7.5 x 9.25 spine = 1.6875" 896 page count Pro Silverlight 4 in VB Third Edition ■ ■ ■ Matthew MacDonald Pro Silverlight 4 in VB, Third Edition Copyright © 2010 by Matthew MacDonald All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-3548-4 ISBN-13 (electronic): 978-1-4302-3549-1 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: Fabio Claudio Ferracchiati Editorial Board: 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-Verlag New York, Inc., 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. 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 family 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.............................................................................................................225 ■ Chapter 8: Shapes and Geometries........................................................................................257 ■ Chapter 9: Brushes, Transforms, and Bitmaps.....................................................................293 ■ Chapter 10: Animation............................................................................................................335 ■ Chapter 11: Sound, Video, and Deep Zoom..........................................................................391 ■ Chapter 12: Styles and Behaviors...........................................................................................439 ■ Chapter 13: Templates and Custom Controls.......................................................................463 ■ Chapter 14: Browser Integration............................................................................................503 ■ Chapter 15: ASP.NET Web Services........................................................................................529 ■ Chapter 16: Data Binding........................................................................................................559 ■ Chapter 17: Data Controls......................................................................................................609 ■ Chapter 18: File Access............................................................................................................657 ■ Chapter 19: Multithreading....................................................................................................683 ■ Chapter 20: Networking..........................................................................................................705 ■ Chapter 21: Out-of-Browser Applications.............................................................................753 ■ Index.........................................................................................................................................793 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..............................................................................................................34 The Code-Behind Class.......................................................................................................38 Properties and Events in XAML...............................................................................................39 Simple Properties and Type Converters............................................................................40 Complex Properties.............................................................................................................42 Attached Properties.............................................................................................................44 Nesting Elements.................................................................................................................45 Events...................................................................................................................................48 The Full Eight Ball Example................................................................................................48 XAML Resources.......................................................................................................................49 The Resources Collection...................................................................................................50 The Hierarchy of Resources................................................................................................51 Accessing Resources in Code.............................................................................................53 Organizing Resources with Resource Dictionaries...........................................................54 Element-to-Element Binding..................................................................................................55 One-Way Binding................................................................................................................55 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...................................................................................................126 Mouse Cursors...................................................................................................................130 viii