Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 963 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
963
Dung lượng
26,67 MB
Nội dung
MacDonald
Shelve in
.NET
User level:
Intermediate–Advanced
www.apress.com
SOURCE CODE ONLINE
RELATED
BOOKS FOR PROFESSIONALS BY PROFESSIONALS
®
Pro Silverlight5in VB
Silverlight is a revolutionary browser plug-in. Using it, you can create rich client appli-
cations that run inside the browser. Impressively, Silverlight applications have cross-
platform reach—they run seamlessly in non-Microsoft browsers (like Firefox, Safari,
and Chrome) and work on both the Windows and Mac OS operating systems. And
best of all, Silverlight applications support pure VB code and use a streamlined version
of the .NET Framework.
In this book, you’ll learn to:
• Develop rich applications that work on all desktop browsers
• Design modern user interfaces with Silverlight’s layout tools and fully-stocked
control toolkit
• Create cutting-edge effects with 2D drawing, 3D drawing, and animation
• Incorporate sound and video
• Query a database through an ASP.NET web service, and display the records
with data binding
• Master advanced features like multithreading and networking
• Create desktop-like applications that run in their own windows
This book is for .NET developers encountering Silverlight for the first time. No Silverlight
experience is required—but if you’ve worked with earlier Silverlight versions, 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.
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
iv
Contents at a Glance
About the Author xxvii
About the Technical Reviewer xxviii
Acknowledgments xxix
Introduction xxx
Chapter 1: Introducing Silverlight 1
Chapter 2: XAML 35
Chapter 3: Layout 63
Chapter 4: Dependency Properties and Routed Events 111
Chapter 5: Elements 143
Chapter 6: The Application Model 205
Chapter 7: Navigation 233
Chapter 8: Shapes and Transforms 265
Chapter 9: Brushes, Bitmaps, and Printing 315
Chapter 10: Animation Basics 349
Chapter 11: Advanced Animation 377
Chapter 12: Sound, Video, and Deep Zoom 411
Chapter 13: Silverlight 3D 467
Chapter 14: Styles and Behaviors 511
Chapter 15: Control Templates 539
Chapter 16: Multithreading 583
Chapter 17: Browser Integration 605
Chapter 18: Out-of-Browser Applications 631
Chapter 19: ASP.NET Web Services 679
CONTENTS
v
Chapter 20: Data Binding 709
Chapter 21: Data Controls 763
Chapter 22: File Access 825
Chapter 23: Networking 857
Index 905
xxx
Introduction
Silverlight is a framework for building rich, browser-hosted applications that run on a variety of
operating systems. Silverlight works its magic through a browser plug-in. When you surf to a web page
that includes Silverlight content, this browser plug-in runs, executes the code, and renders that content
in a specifically designated region of the page. The important part is that the Silverlight plug-in provides
a richer environment than the traditional blend of HTML and JavaScript that powers ordinary web
pages. Used carefully and artfully, you can create Silverlight pages that play video, have hardware-
accelerated 3D graphics, and use vector animations.
Understanding Silverlight
Silverlight uses a familiar technique to go beyond the capabilities of standard web pages: a lightweight
browser plug-in.
The advantage of the plug-in model is that the user needs to install just a single component to see
content created by a range of different people and companies. Installing the plug-in requires a small
download and forces the user to confirm the operation in at least one security dialog box. It takes a short
but definite amount of time, and it’s an obvious inconvenience. However, once the plug-in is installed,
the browser can process any content that uses the plug-in seamlessly, with no further prompting.
Figure 1 shows two views of a page with Silverlight content. At the top is the page you’ll see if you
don’t have the Silverlight plug-in installed. At this point, you can click the Get Microsoft Silverlight
picture to be taken to Microsoft’s website, where you’ll be prompted to install the plug-in and then sent
back to the original page. On the bottom is the page you’ll see once the Silverlight plug-in is installed.
INTRODUCTION
xxxi
Figure 1. Installing the Silverlight plug-in
INTRODUCTION
xxxii
Note At the time of this writing, Silverlight 4 is installed on an estimated 75 percent of Internet-connected
computers (including desktop and mobile devices). The share is higher if you consider only Windows operating
systems or Internet Explorer browsers. Although this is impressive, it pales in comparison to Flash, which has
version 10 or better installed on a staggering 96 percent of all web devices. (To get up-to-date statistics, refer to
www.riastats.com.)
Silverlight System Requirements
With any web-centric technology, it’s keenly important to have compatibility with the widest possible
range of computers and devices. And although Silverlight isn’t completely cross-platform, its
compatibility stacks up well on the majority of desktop computers.
Currently, Silverlight supports the following:
• Windows computers: Silverlight works on PCs with Windows 7, Windows Vista,
and Windows XP. The minimum browser versions that Silverlight supports are
Internet Explorer 6, Firefox 1.5, and Google Chrome 4.0. Silverlight will also work
in Windows 2000, but only with Internet Explorer 6. Other browsers, such as
Opera and Safari (for Windows), aren’t currently supported.
• Mac computers: Silverlight works on Mac computers with OS X 10.4.8 or newer,
provided they have Intel hardware (as opposed to the older PowerPC hardware).
The minimum browser versions that Silverlight supports are Firefox 2 and Safari 3.
Silverlight does not run on mobile Apple devices, which includes the iPhone, iPod
Touch, and iPad.
• Linux computers: Although Silverlight5 doesn’t work on Linux, the Mono team
has created an open source Linux implementation called Moonlight. The last
officially released version supports Silverlight 2 applications, although there is also
a released preview version that supports Silverlight 4. Progress is slow, and it’s
uncertain when (and if) there will be a Moonlight that supports Silverlight5.
• Windows Phone: Silverlight is also one of two development frameworks for
Windows Phone 7 (the other being the game-focused Microsoft XNA). However,
developing for Windows Phone raises some unique considerations, and there are
some subtle feature differences. To learn more about WP7 development, you can
refer to a dedicated book, like Pro Windows Phone 7 Development.
Installing Silverlight requires a small-sized setup (around 6MB) that’s easy to download. That allows
it to provide an all-important “frictionless” setup experience, much like Flash.
Silverlight vs. Flash
The most successful browser plug-in is Adobe Flash, which is installed on more than 90 percent of the
world’s web browsers. Flash has a long history that spans more than ten years, beginning as a
INTRODUCTION
xxxiii
straightforward tool for adding animated graphics and gradually evolving into a platform for developing
interactive content.
It’s perfectly reasonable for .NET developers to create websites that use Flash content. However,
doing so requires a separate design tool and a completely different programming language
(ActionScript) and programming environment (Flex). Furthermore, there’s no straightforward way to
integrate Flash content with server-side .NET code. For example, creating Flash applications that call
.NET components is awkward at best. Using server-side .NET code to render Flash content (for example,
a custom ASP.NET control that spits out a Flash content region) is far more difficult.
That’s where Silverlight fits into the picture. Silverlight aims to combine the raw power and cross-
platform support of Flash with a first-class programming platform that incorporates the fundamental
concepts of .NET. As a result, developers can write client-side code for Silverlightin the same language
they use for server-side code (such as C# and VB), and they use many of the same abstractions (including
streams, controls, collections, generics, and LINQ). In short, for .NET developers Silverlight is both a
more convenient and a more powerful choice for rich Internet applications.
Silverlight vs. HTML5
When Silverlight was first created, it was intended as an all-purpose way to build rich web pages and a
competitor to Adobe Flash. However, in the several Silverlight versions since, the world has changed.
Although Adobe Flash is still supported by virtually every desktop computer, it’s been locked out of
popular Apple products like the iPhone and iPad. As a result, the mobile world is gravitating to different
solutions, including native applications (which are limited to just one operating system) or HTML5.
A broad consensus exists that HTML5 is the future of the Web, someday. However, the features that
HTML5 promises (when it’s fully adopted) still fall far short of the features that are available today in
Flash and Silverlight. For some applications, these shortcomings don’t matter. And, without doubt, the
capabilities of HTML5 will strengthen in the future. But in the meantime, developers are forced to
choose between features now, if they need them (in which case they’re likely to pick Silverlight) and the
broadest possible compatibility for all computers and mobile devices (in which case they’ll probably
prefer HTML5). This dilemma is also described as rich versus reach. Silverlight is the rich side of this
equation—it gives applications the most powerful and mature feature set. HTML5 is the reach, because
it embraces every modern desktop browser.
Because of this upheaval, many Silverlight developers are uncertain about exactly where their
favorite technology fits into the world of web development. And while the future is far from certain, here
are a few points to keep in mind:
HTML5 isn’t quite here yet: HTML5 support lags inin Internet Explorer. Many HTML5 features are
promised for the not-yet-released IE 10, some features are available in the relatively recent IE 9, but
IE 8 has no HTML5 smarts. This is a problem, because IE 8 is the best version of IE that can run on
the still-widespread Windows XP platform. For all these reasons, HTML5 remains currently has less
support than Silverlight, and this situation may take years to change.
Silverlight has features that have no HTML5 equivalent: Even in the browsers that offer the best
possible HTML5 support fall behind Silverlightin a few key areas. They can’t offer the same video
streaming features, the same hardware-accelerated graphics, or the same deep networking support.
Nor do they support file access, out-of-browser applications, or the ability to call Windows system
components. And it’s hard to imagine HTML5 ever duplicating some of Silverlight’s more
specialized features, like the PivotViewer control that fuses together data filtering, fluid animations,
and image scaling in one easy-to-use package (Chapter 21).
INTRODUCTION
xxxiv
Silverlight has a higher-level programming API: Features such as data binding, styles, and templates
may not be essential for building an application, but they are important for building one quickly
and efficiently. Many things that are possible in HTML5 are a manageability nightmare in all but the
most disciplined hands. The JavaScript language is notoriously lax in letting syntax errors slide, all
animation routines must be written by hand, and multithreading support is clumsy at best.
Silverlight has top-tier development tools: Thanks to Visual Studio, you can build a Silverlight
application just as easily as you build a desktop program. Add Expression Blend to the picture, and
you also have a way to define and customize rich graphical effects, like animations.
Silverlight offers ASP.NET integration: In particular, Silverlight makes it easy to query server-side
databases through a web service. This state of affairs has led some developers to speculate that even
when HTML5 does finally conquer the world, Silverlight will remain as a first choice for line-of-
business development inside closed company networks.
No one’s quite sure of Silverlight’s future. It may continue on as a first-choice platform for .NET
developers creating business applications, or it may gradually transition into a more specialized tool for
cutting-edge games and video players. One thing is settled, however: Silverlight will never replace HTML
as the main language for creating traditional, public websites—nor does it intend to.
Silverlight vs. Metro (and Windows 8)
When Microsoft announced Windows 8, complete with yet another programming model for rich client
applications, Silverlight developers paused. Some wondered if that technology—named Metro—would
be an eventual Silverlight replacement.
The answer is clearly “no.” Metro is designed to facilitate an entirely different sort of application: a
lightweight, touch-centric, data-consuming application that can run on the future generation of
Windows 8–powered tablets. In a very real sense, Metro is a competitor to native apps on the iPad, as
well as a potential successor to Microsoft’s other rich desktop programming framework, WPF (see the
next section). However, Metro applications have no ability to run on non-Microsoft platforms—or any
version of Windows other than Windows 8. For that reason, they are of little interest to Silverlight
developers.
In the future, the range of Silverlight applications may narrow, squeezed between cross-platform
HTML5 applications that are gradually growing more sophisticated and native Metro or iPad
applications for mobile devices. However, today Silverlight still occupies the very important space
between these other technologies.
Silverlight and WPF
One of the most interesting aspects of Silverlight is the fact that it borrows the model WPF uses for rich,
client-side user interfaces.
WPF is a toolkit for building rich Windows applications. WPF is notable because it not only
simplifies development with a powerful set of high-level features, it also increases performance by
rendering everything through the DirectX pipeline. To learn about WPF, you can refer to Pro WPF inVB
2010 (Apress).
Silverlight obviously can’t duplicate the features of WPF, because many of them rely deeply on the
capabilities of the operating system, including Windows-specific display drivers and DirectX technology.
However, rather than invent an entirely new set of controls and classes for client-side development,
INTRODUCTION
xxxv
Silverlight uses a subset of the WPF model. If you’ve had any experience with WPF, you’ll be surprised to
see how closely Silverlight resembles its big brother. Here are a few common details:
• To define a Silverlight user interface (the collection of elements that makes up a
Silverlight content region), you use XAML markup, just as you do with WPF. You
can even map data to your display using the same data-binding syntax.
• Silverlight borrows many of the same basic controls from WPF, along with the
same styling system (for standardizing and reusing formatting) and a similar
templating mechanism (for changing the appearance of standard controls).
• To draw 2D graphics in Silverlight, you use shapes, paths, transforms, geometries,
and brushes, all of which closely match their WPF equivalents.
• Silverlight provides a declarative animation model that’s based on storyboards
and works in the same way as WPF’s animation system.
• To show video or play audio files, you use the MediaElement class, as you do in
WPF.
Note WPF is not completely cut off from the easy deployment world of the Web. WPF allows developers to
create browser-hosted applications called XBAPs (XAML Browser Applications). These applications are downloaded
seamlessly, cached locally, and run directly inside the browser window, all without security prompts. However,
although XBAPs run in Internet Explorer and Firefox, they are still a Windows-only technology, unlike Silverlight.
The Evolution of Silverlight
Silverlight 1 was a relatively modest technology. It included 2D drawing features and media playback
support. However, it didn’t include the CLR engine or support for .NET languages, so developers were
forced to code in JavaScript.
Silverlight 2 was a dramatic change. It added the CLR, a subset of .NET Framework classes, and a
user interface model based on WPF. As a result, Silverlight 2 was one of the most hotly anticipated
releases in Microsoft’s history.
The versions of Silverlight since haven’t been as ambitious. Silverlight5 keeps the same
development model that was established inSilverlight 2 but adds a carefully selected group of features
and performance enhancements. They highlights include the following:
• Performance improvements: Silverlight5 starts faster, supports 64-bit browsers,
and provides cleaner, crisper text rendering at small sizes.
• Vector printing: Silverlight improves its printing model to use vector printing,
when possible (namely, if the print driver supports PostScript). The result is faster
printing with less memory overhead (Chapter 9).
[...]... Your Silverlight project is compiled into a DLL file named after your project For example, if you have a project named SilverlightApplication1, the vbc.exe compiler will create the file SilverlightApplication1.dll The project assembly is dumped into a Bin\Debug folder in your project directory, along with a few other important files: 18 CHAPTER 1 INTRODUCING SILVERLIGHT • A PDB file: This file contains... as debugging, code refactoring, and project source control Note Visual Studio 2010 includes full support for creating Silverlight 3 projects But to create Silverlight5 projects, you need the Silverlight5 Tools for Visual Studio 2010 And if you plan to use Expression Blend with Silverlight 5, you’ll (currently) need to use a beta version called the Expression Blend Preview for Silverlight5 You can... as playing a game, completing a survey, interacting with a product, or taking a virtual tour) You may use Silverlightenhanced pages to present content that’s already available in your website in a more engaging way or to provide a value-added feature for users who have the Silverlight plug -in Of course, it’s also possible to create a Silverlight- only website, which is a somewhat more daring approach... change the version of Silverlight that you’re targeting at any point after you’ve created it To do so, just double-click the My Project node in the Solution Explorer, and change the selection in the Target Silverlight Version list 4 4 Click OK to continue and create the project CHAPTER 1 INTRODUCING SILVERLIGHT Figure 1-1 Choosing not to include an ASP.NET website Every Silverlight project starts with... COMPATIBILITY IN SILVERLIGHT5 At this point, you might be wondering if older Silverlight applications can run on a computer that has only the latest version of the Silverlight plug -in (version 5) installed It’s a reasonable question, because Silverlight 5 introduces some subtle changes and bug fixes that can influence the way applications work— and even change its behavior However, Silverlight 5 prevents... differences from causing problems by using its quirks mode feature When the Silverlight 5 plug -in loads an application that was compiled for an earlier version of Silverlight, it automatically switches into a quirks mode that attempts to emulate the behavior of the appropriate Silverlight runtime environment For more detailed information about breaking changes between Silverlight 5 and Silverlight 4, you... is that Silverlight isn’t installed as widely as other web technologies such as Flash, and doesn’t support legacy clients such as those running the Windows ME or Windows 2000 operating system As a result, Silverlight doesn’t have nearly the same reach as ordinary HTML Many businesses that are adopting Silverlight are using it to distinguish themselves from other online competitors with cutting-edge... they aren’t abandoning their traditional websites 3 CHAPTER 1 INTRODUCING SILVERLIGHT Creating a Stand-Alone Silverlight Project The easiest way to start using Silverlight is to create an ordinary website with HTML pages and no serverside code Here’s how: 1 Select File New Project in Visual Studio, choose the Visual Basic Silverlight group of project types, and then select the Silverlight Application... experimenting, or take a look through Microsoft’s Expression Blend training videos at http://expression.microsoft.com/cc13 653 5.aspx Understanding Silverlight Websites You can create two types of Silverlight websites in Visual Studio or Expression Blend: • 2 An ordinary website with HTML pages: In this case, the entry point to your Silverlight application is a basic HTML file that includes a Silverlight. .. vantage point 7 CHAPTER 1 INTRODUCING SILVERLIGHT Figure 1-4 Viewing XAML pages You can start designing a XAML page by selecting a control in the Toolbox and then “drawing” it onto the design surface However, this convenience won’t save you from learning the full intricacies of XAML To organize your elements into the right layout containers, change their properties, wire up event handlers, and use Silverlight . MacDonald
Shelve in
.NET
User level:
Intermediate–Advanced
www.apress.com
SOURCE CODE ONLINE
RELATED
BOOKS FOR PROFESSIONALS BY PROFESSIONALS
®
Pro Silverlight 5 in VB
Silverlight.
service for Silverlight in Chapter 19.
ADDING SILVERLIGHT CONTENT TO AN EXISTING WEBSITE
A key point to keep in mind when considering the Silverlight