Beginning Silverlight tài liệu học Silverlight cho người mới bắt đầu
this print for content only—size & color not accurate trim = 7.5" x 9.25" spine = 0.000" 000 page countBooks for professionals By professionals® Cyan MaGenTa yelloW BlaCkThe experT’s VoiCe® in silVerliGhTBeginning Silverlight 3 Robert LairLearn to build state-of-the-art Silverlight applications quickly and easilyLairSilverlight 3Companion eBook AvailableBeginningBeginning Silverlight 3Dear Reader,When Microsoft released the first version of Silverlight, it laid the foundation of creating rich user-interfaces for the web. However it wasn’t until Silverlight 2 that developers started to get really excited about the technology, primarily because of the ability to write Silverlight applications using the .NET framework. This allowed developers to start creating cross-platform, cross-browser, rich internet applications.Silverlight 3 further adds to the capabilities of Silverlight for the rich internet application developer, including a new navigation framework that allows devel-opers to create multi-page application scenarios in Silverlight, new controls that help developers create rich user-interfaces, as well as improvements in runtime performance, animation, caching and networking. In addition to these new capabilities, Silverlight 3 applications can now escape the browser, allowing users to install Silverlight applications locally. This provides a way for developers to create cross-platform applications, without having to recompile source for each platform. Beginning Silverlight teaches the fundamental concepts and techniques that lie at the heart of every successful Silverlight application through a number of step-by-step walk-through tutorials, that will give you hands on experience with the different topics and get you ready to start developing Silverlight applications for your own.Robert Lair THE APRESS ROADMAPPro Business Applications with Silverlight 3Pro Silverlight for the EnterprisePro Silverlight 3 in C#Silverlight 3 RecipesBeginning Silverlight 3Accelerated Silverlight 3ISBN 978-1-4302-2377-19 78143022377153 999Author ofBeginning Silverlight 2Pure ASP.NET: A Code-Intensive Premium ReferenceUS $39.99Shelve in Web DevelopmentUser level: Beginner-Intermediatewww.apress.comSOURCE CODE ONLINECompanion eBook See last page for details on $10 eBook version ■ ■ ■ Beginning Silverlight 3: From Novice to Professional Copyright © 2009 by Robert Lair 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-2377-1 ISBN-13 (electronic): 978-1-4302-2378-8 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: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Copy Editor: Katie Stence Compositor: Mary Sudul Indexer: John Collin 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 http://www.springeronline.com. For information on translations, please e-mail info@apress.com, or visit http://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 http://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 http://www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. To my beautiful wife Debi, whom I love more and more each day, and to my son Max, who has made me so proud. I love you guys more than anything on this earth. ■ CONTENTS iv Contents at a Glance Contents . vi About the Author .xi About the Technical Reviewer xii Acknowledgments . xiii Introduction . xiv ■Chapter 1: Welcome to Silverlight 3 1 ■Chapter 2: Introduction to Visual Studio 2008 13 ■Chapter 3: Layout Management in Silverlight 3 39 ■Chapter 4: Silverlight 3 Controls .65 ■Chapter 5: Data Binding and Silverlight List Controls .105 ■Chapter 6: Data Access and Networking .137 ■Chapter 7: Navigation Framework 153 ■Chapter 8: Local Storage in Silverlight 183 ■Chapter 9: Introduction to Expression Blend .213 ■Chapter 10: Styling in Silverlight 235 ■Chapter 11: Transformations and Animation 267 ■Chapter 12: Custom Controls 289 ■Chapter 13: Deployment 311 Index .323 ■ CONTENTS v Contents Contents at a Glance iv About the Author xii About the Technical Reviewer . xiii Acknowledgments . xiv ■Chapter 1: Welcome to Silverlight 3 1 The Evolution of the User Interface 1Rich Internet Application Solutions 3What Is Silverlight? 3Benefits of Silverlight 4Cross-Platform/Cross-Browser Support . 5Cross-Platform Version of the .NET Framework . 5XAML, a Text-Based Markup Language . 5Use of Familiar Technologies . 6Small Runtime and Simple Deployment . 6The Silverlight Development Environment .8New Features in Silverlight 3 .10Improved Performance . 11Summary .11■Chapter 2: Introduction to Visual Studio 2008 13 What Is Visual Studio? .13 ■ CONTENTS vi What’s New in Visual Studio 2008? .14 JavaScript IntelliSense and Debugging 14 Multi-Targeting Support . 28 Transparent IntelliSense Mode . 30 Building Your First Silverlight Application in Visual Studio 31 Try It Out: Hello World in Silverlight 3 . 31 Hosting Your Silverlight Application: Web Site or Web Application? 36 Summary .37 ■Chapter 3: Layout Management in Silverlight 3 39 Layout Management 39 The Canvas Panel 40 Try It Out: Using the Canvas Panel . 41 Filling the Entire Browser Window with Your Application 44 The StackPanel Control .45 Try It Out: Using the StackPanel Control . 45 Try It Out: Nesting StackPanel Controls 47 The Grid Control .49 Try It Out: Using the Grid Control 49 Try It Out: Nesting a Grid and Spanning a Column . 52 The WrapPanel Control 56 Try It Out: Using the WrapPanel Control . 56 The DockPanel Control 59 Try It Out: Using the DockPanel Control 60 Summary .63 ■Chapter 4: Silverlight 3 Controls .65 Setting Control Properties 65 Attribute Syntax 65 Element Syntax . 66 ■ CONTENTS vii Type-Converter-Enabled Attributes 66 Attached Properties 66 Nesting Controls Within Controls .67 Handling Events in Silverlight 68 Try It Out: Declaring an Event in XAML . 68 Try It Out: Declaring an Event Handler in Managed Code . 72 The Border Control .76 User Input Controls 80 Try It Out: Working with the TextBox Control 80 Try It Out: Working with the RadioButton and CheckBox Controls . 84 Extended Controls 87 Adding an Extended Control . 87 Try It Out: Using the GridSplitter . 88 AutoCompleteBox 90 ViewBox .92 Modal Windows .93 Try It Out: Using the Modal Child Window 95 Summary .103 ■Chapter 5: Data Binding and Silverlight List Controls .105 Data Binding 105 The Binding Class . 106 Try It Out: Simple Data Binding in Silverlight . 106 Element to Element Binding 114 Try It Out: Element to Element Binding . 114 The DataGrid Control 116 Try It Out: Building a Simple DataGrid 117 The Columns Collection 122 Try It Out: Building a DataGrid with Custom Columns 124 ■ CONTENTS viii The ListBox Control 130 Default and Custom ListBox Items 131 Try It Out: Building a ListBox with Custom Content 133 Summary .135 ■Chapter 6: Data Access and Networking .137 Data Access in Silverlight Applications .137 Accessing Data Through Web Services .138 Try It Out: Accessing Data Through a WCF Service 138 Accessing Services from Other Domains .149 Accessing Data Through Sockets 150 Summary .152 ■Chapter 7: Navigation Framework 153 Frame and Page Object .153 Try It Out: Creating a Silverlight Navigation Application . 153 Benefits of the Navigation Framework 164 Deep Linking . 164 The NavigationService Object 165 Try it Out: Using the NavigationService Object . 166 Passing Data to Navigation Pages .168 Try it Out: Passing Data to Navigation Pages . 169 Uri Mapping .172 Try it Out: Uri Mapping and the Navigation Framework . 173 Silverlight Navigation Application Template 175 Try it Out: Using the Silverlight Navigation Application Template 175 Using Multiple Frames .179 Try it Out: Using Multiple Frames . 180 Summary .182 [...]... interpreted in the client. Although Silverlight 1.0 works well for developers who are already familiar with client-side scripting, many developers have their eyes on the second release of Silverlight, version 2. Silverlight 1.0 is more or less in direct competition with Flash—some have called it Microsoft’s “Flash killer.” However, things really get exciting with Silverlight 2. Silverlight 2 and beyond contains... ■ WELCOME TO SILVERLIGHT 3 7 Figure 1-5. Silverlight runtime required logo When users click the icon in the logo, they are taken to a web page that walks them through the process of installing the Silverlight runtime. Once the runtime is finished installing, the Silverlight application is immediately available to the user, as shown in the example in Figure 1-6. Figure 1-6. Silverlight. .. The end result is astonishing Silverlight 2 is approximately 4MB in size. In Silverlight 3, even with the large amount of new features that have been added to the Silverlight runtime, the file size is still under 5MB. As for pushing the Silverlight runtime out to clients, Microsoft has provided a very easy detection mechanism. If the client does not have the proper Silverlight runtime installed,... Mapping and the Navigation Framework 173 Silverlight Navigation Application Template 175 Try it Out: Using the Silverlight Navigation Application Template 175 Using Multiple Frames 179 Try it Out: Using Multiple Frames 180 Summary 182 CHAPTER 1 ■ WELCOME TO SILVERLIGHT 3 9 • Silverlight 3 Software Development Kit: This SDK is a collection of samples, Silverlight QuickStarts, documentation,... controls that are used to develop Silverlight applications. • Silverlight Project Templates for Visual Studio 2008: This adds the Silverlight templates in Visual Studio. As an example, it will add the template that enable you to create a Silverlight project from the “Add New Project” in Visual Studio. 3. Expression Blend 3: The next thing to install for your Silverlight development environment... offered by Adobe. Microsoft retaliated by announcing Silverlight, formerly known as Windows Presentation Foundation Everywhere (WPF/E). Silverlight is the technology that many .NET developers have been waiting for. What exactly is Silverlight? And, what impact does Silverlight actually have on us as .NET developers? Well, I’m glad you asked. What Is Silverlight? As I explained in the previous section,... CONTENTS x Silverlight Styles 251 Try It Out: Using Styles As Static Resources 253 Defining Styles at the Application Level 259 Merged Resource Dictionaries 261 Silverlight Style Hierarchy 262 Inheriting Styles Using BasedOn 264 Summary 265 ■Chapter 11: Transformations and Animation 267 Introduction to Silverlight Animation 267 Silverlight Storyboards 268 Types of Animation in Silverlight. .. Creating Transformations in Silverlight 282 Transformation Types 283 Try It Out: Using Expression Blend to Transform Silverlight Objects 285 Summary 288 ■Chapter 12: Custom Controls 289 When to Write Custom Controls 289 Silverlight Control Toolkit 290 Silverlight Control Model 291 Parts and States Model 291 Dependency Properties 292 Creating Custom Controls in Silverlight 293 Implementing... xiv ■Chapter 1: Welcome to Silverlight 3 1 The Evolution of the User Interface 1 Rich Internet Application Solutions 3 What Is Silverlight? 3 Benefits of Silverlight 4 Cross-Platform/Cross-Browser Support 5 Cross-Platform Version of the .NET Framework 5 XAML, a Text-Based Markup Language 5 Use of Familiar Technologies 6 Small Runtime and Simple Deployment 6 The Silverlight Development Environment... Silverlight requires that a client runtime be installed on the client machine, it is vital that this runtime has a small footprint and downloads quickly. Microsoft worked very hard to get the installation size as small as possible. The developers clearly succeeded with Silverlight 1.0, as the download size is a tiny 1MB. For Silverlight 2, however, they had a harder chore ahead of them, since Silverlight . Silverlight 3 in C #Silverlight 3 RecipesBeginning Silverlight 3Accelerated Silverlight 3ISBN 978-1-4302-2377-19 78143022377153 999Author ofBeginning Silverlight. experT’s VoiCe® in silVerliGhTBeginning Silverlight 3 Robert LairLearn to build state-of-the-art Silverlight applications quickly and easilyLairSilverlight 3Companion