MacDonald Silverlight 4 in VB Companion eBook Available 7.5 x 9.25 spine = 1.6875" 896 page count THE EXPERT’S VOICE ® IN SILVERLIGHT Pro Silverlight 4 in VB Matthew MacDonald Create cross-platform .NET applications for the browser CYAN MAGENTA YELLOW BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® Pro Silverlight 4 in VB Silverlight is a revolutionary browser plug-in that allows developers to create rich client applications that run inside the browser. Like Adobe Flash, Silverlight sup- ports event handling, two-dimensional drawing, video playback, networking, and animation. Unlike Flash, Silverlight is tailored to .NET developers and supports pure Visual Basic code. The most exciting part of Silverlight is its cross-platform muscle. Unlike ordi- nary .NET applications, Silverlight applications run seamlessly in non-Microsoft browsers (such as Firefox, Safari, and Chrome) and on non-Microsoft platforms (such as Mac OS X). Essentially, Silverlight is a scaled-down, browser-hosted ver- sion of .NET—and that’s made it the most hotly anticipated technology that Microsoft has released in years. In this book, you’ll master Silverlight from the ground up. No Silverlight expe- rience is required—but if you’ve worked with Silverlight 3, you’ll appreciate the “What’s New” boxes that point out new features at the start of every chapter. By the end of this book, you’ll have a solid understanding of the complete Silverlight platform, and you’ll be able to build anything from a slick business front end to a browser-based game. Matthew MacDonald, author of Pro WPF in VB 2010 Pro ASP.NET 4 in VB 2010 (with Adam Freeman and Mario Szpuszta) Beginning ASP.NET 4 in VB 2010 Your Brain: The Missing Manual Shelve in: .NET User level: Intermediate–Advanced THE APRESS ROADMAP Pro Windows Phone 7 Development Pro Business Applications with Silverlight 4 Pro Silverlight 4 in VB Pro WPF in VB 2010 Introducing Silverlight 4 Beginning ASP.NET in VB 2010 www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version ISBN 978-1-4302-3548-4 9 781430 235484 55999 Pro THIRD EDITION THIRD EDITION www.it-ebooks.info www.it-ebooks.info Pro Silverlight 4 in VB Third Edition ■ ■ ■ Matthew MacDonald www.it-ebooks.info 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 orders-ny@springer-sbm.com, or visit www.springeronline.com. For information on translations, please e-mail rights@apress.com, 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. www.it-ebooks.info For my family www.it-ebooks.info www.it-ebooks.info v 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 ■ C hapter 20: Networking 705 ■ Chapter 21: Out-of-Browser Applications 753 ■ Index 793 www.it-ebooks.info ■ CONTENTS vi 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 www.it-ebooks.info ■ CONTENTS vii 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 www.it-ebooks.info ■ CONTENTS viii 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 www.it-ebooks.info [...]... Set in Silverlight 43 3 The Last Word 43 7 ■ Chapter 12: Styles and Behaviors 43 9 Styles 43 9 Defining a Style 44 0 xiv www.it-ebooks.info ■ CONTENTS Applying a Style 44 1 Dynamic Styles 44 2 Style Inheritance 44 2 Organizing Styles .44 4 Automatically Applying Styles by Type 44 4 Behaviors... .44 5 Getting Support for Behaviors 44 6 Triggers and Actions 44 7 Creating a Behavior 45 7 Finding More Behaviors 46 0 The Last Word 46 2 ■ Chapter 13: Templates and Custom Controls .46 3 Template Basics 46 3 Creating a Template 46 4 Reusing Control Templates 46 5 The ContentPresenter .46 6... Encoding 40 3 Encoding in Expression Encoder .40 4 Markers .40 6 VideoBrush .41 1 Video Effects 41 2 Webcam and Microphone Input 41 8 Accessing a Capture Device 41 8 Basic Webcam Support .42 0 Recording Audio Snippets 42 3 Deep Zoom 42 7 Creating a Deep Zoom Image Set 42 9 Using... 48 7 Creating the Solution 48 7 Starting the FlipPanel Class 48 8 Adding the Default Style with Generic.xaml 48 9 Choosing Parts and States 49 1 Starting the Default Control Template 49 3 The FlipButton Control 49 4 Defining the State Animations 49 5 Wiring Up the Elements in the Template 49 7 xv www.it-ebooks.info ■... Template Bindings 46 7 Setting Templates Through Styles 46 8 Reusing Colors 47 0 The Parts and States Model 47 0 Understanding States with the Button Control 47 2 Showing a Focus Cue 47 6 Transitions 47 7 Understanding Parts with the Slider Control 48 2 Creating Templates for Custom Controls 48 6 Planning the... Starting an Animation with Code 339 Configuring Animation Properties 340 Animation Lifetime 343 Simultaneous Animations 345 Controlling Playback 346 Animation Easing 348 Using an Easing Function . 349 Easing In and Easing Out 350 Easing Function Classes 351 Animation Types Revisited 3 54. .. MVP for Silverlight He’s the author of more than a dozen books about NET programming, including Pro WPF in VB 2010, Pro ASP.NET 4 in C# 2010, and Beginning ASP.NET in VB 2010 He’s also the author of Your Brain: The Missing Manual (Pogue Press, 2008), a popular look at getting the most from your squishy gray matter Matthew lives in Toronto with his wife and two daughters xxii www.it-ebooks.info ■ CONTENTS... then sent back to the original page On the bottom is the page you’ll see once the Silverlight plug -in is installed xxv www.it-ebooks.info ■ INTRODUCTION Figure 1 Installing the Silverlight plug -in ■ Note At the time of this writing, Silverlight 4 is installed on an estimated 53% of Internet-connected computers (The share is higher if you consider only Windows operating systems or Interent Explorer browsers.)... boxes that follow the introduction in each chapter They point out features that are new to Silverlight 4, so you can home in on its changes and enhancements BACKWARD COMPATIBILITY IN SILVERLIGHT 4 At this point, you might be wondering if existing Silverlight 2 and Silverlight 3 applications can run on a computer that has only the latest version of the Silverlight plugin (version 4) installed It’s a reasonable... lets you define what should happen and how long it should take The Silverlight plug -in handles the sticky details, like interpolating intermediary values and calculating the frame rate Media: Silverlight provides playback of a range of video standards, including highdefinition H.2 64 video and AAC audio Silverlight doesn’t use the Windows Media Player ActiveX control or browser plug -in instead, you . Inheritance 44 2 Organizing Styles 44 4 Automatically Applying Styles by Type 44 4 Behaviors 44 5 Getting Support for Behaviors 44 6 Triggers and Actions 44 7 Creating a Behavior 45 7 Finding More. APRESS ROADMAP Pro Windows Phone 7 Development Pro Business Applications with Silverlight 4 Pro Silverlight 4 in VB Pro WPF in VB 2010 Introducing Silverlight 4 Beginning ASP.NET in VB 2010 www.apress.com SOURCE. Silverlight 43 3 The Last Word 43 7 ■ Chapter 12: Styles and Behaviors 43 9 Styles 43 9 Defining a Style 44 0 www.it-ebooks.info ■ CONTENTS xv Applying a Style 44 1 Dynamic Styles 44 2 Style Inheritance