Free Essay

Silverlight Tutorial

In:

Submitted By ssv26
Words 43350
Pages 174
www.it-ebooks.info

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.

www.it-ebooks.info

Contents at a Glance
 About the Author.................................................................................................. xiv  About the Technical Reviewer .............................................................................. xv  Acknowledgments ............................................................................................... xvi  Introduction ........................................................................................................ xvii  Chapter 1: Welcome to Silverlight 5 .......................................................................1  Chapter 2: Introduction to Visual Studio 2010......................................................11  Chapter 3: Layout Management in Silverlight ......................................................29  Chapter 4: Silverlight Controls..............................................................................59  Chapter 5: Data Binding and Silverlight List Controls ..........................................89  Chapter 6: Silverlight Toolkit ..............................................................................129  Chapter 7: Data Access and Networking ............................................................157  Chapter 8: Navigation Framework ......................................................................175  Chapter 9: Isolated Storage in Silverlight...........................................................205  Chapter 10: System Integration and Device Support.........................................237  Chapter 11: Introduction to Expression Blend ....................................................259  Chapter 12: Styling in Silverlight........................................................................281  Chapter 13: Transformations and Animations ....................................................315  Chapter 14: Custom Controls ..............................................................................337  Chapter 15: Printing in Silverlight ......................................................................357  Chapter 16: Deployment .....................................................................................373  Index ...................................................................................................................393

iv

www.it-ebooks.info

Introduction
There are many ways you can learn a new technology such as Silverlight. For starters, Microsoft has gotten better and better with the documentation that is released with its programming technologies, and it has also supplemented that documentation with employee blogs and technology sites such as www.silverlight.net. There are widely used forums that are available where many experts participate. Training can also be purchased if you have the cash flow to justify it. While all of these are great options, many people still resort to purchasing a book on the technology. But with so many books on the market, how do you know what book is best? My philosophy on learning a new technology is that there is no better way than to actually try it out for yourself. That is why I have written Beginning Silverlight 5 in C# focusing on a number of step-bystep, walk-through tutorials that will give you hands-on experience with the different topics and get you started developing Silverlight applications on your own.

Who Should Read This Book
This book is written for application developers who want to get started with Silverlight. It assumes that you have some experience developing applications using technologies related to Microsoft Visual Basic, ASP, or .NET and have worked with Microsoft Visual Studio. You should be familiar with the JavaScript, C#, and XML languages.

How This Book Is Organized
Each chapter focuses on a particular area of Silverlight and contains one or more “Try It Out” exercises that allow you to apply what you have learned. Here is a summary of what each chapter includes: • Chapter 1, “Welcome to Silverlight 5,” gives you an introduction to rich interactive (or Internet) applications (RIAs) and Silverlight. You will also learn about the tools used in developing Silverlight-enabled applications. Chapter 2, “Introduction to Visual Studio 2010,” introduces Visual Studio 2010 and the important new features offered in this version. In this chapter, you will build your first Silverlight application. Chapter 3, “Layout Management in Silverlight,” discusses Silverlight’s flexible layout management system, which lets you specify how controls will appear in your applications. It describes Silverlight’s layout management controls in depth.





xvii

www.it-ebooks.info

 INTRODUCTION



Chapter 4, “Silverlight Controls,” introduces the common controls that are provided with Silverlight. You will continue to work with these controls throughout the book. Chapter 5, “Data Binding and Silverlight List Controls,” looks at the Silverlight controls that display lists of data and how to bind data to these controls. You’ll see that these controls are flexible and can show data in unique ways. Chapter 6, “Silverlight Toolkit,” discusses the Silverlight Toolkit, an open-source project that extends the Silverlight control set to include a number a controls, themes, and frameworks that will help you be more productive as a Silverlight developer. Chapter 7, “Data Access and Networking,” describes how data access in Silverlight applications works differently than it does in traditional applications. It then explores mechanisms for accessing data in Silverlight applications, focusing on the use of web services. Chapter 8, “Navigation Framework,” looks at building Silverlight applications that allow the user to navigate through different pages, creating an experience similar to browsing through different pages of a web site. Chapter 9, “Isolated Storage in Silverlight,” covers localized storage in Silverlight, which is handled by its isolated storage feature. You’ll learn how to store userspecific data for your application and have that data persist over browser instances. Chapter 10, “System Integration and Device Support,” covers how Silverlight applications can support notifications, integrate with legacy COM applications and libraries, access a user’s web camera and microphone, and be enabled as a drop target. Chapter 11, “Introduction to Expression Blend,” gets you started with Microsoft Expression Blend, which lets you edit XAML documents visually. Chapter 12, “Styling in Silverlight,” describes how you can control the styles of your Silverlight application’s user-interface elements. You’ll learn about defining style properties inline using both Visual Studio and Expression Blend, as well as how to use Silverlight styles. Chapter 13, “Transformations and Animation,” covers creating animations in Silverlight. You’ll see how Expression Blend helps you create complex animations and transformations. Chapter 14, “Custom Controls,” explains the basics of creating custom controls in Silverlight. First, it covers when it is appropriate to write custom controls in Silverlight, and then it describes how to build a custom control that has several different states. Chapter 15, “Printing in Silverlight,” discusses how to add printing functionality to Silverlight applications.













• •







xviii

www.it-ebooks.info

 INTRODUCTION



Chapter 16, “Deployment,” covers the deployment and configuration of Silverlight applications as well as how to enable out-of-browser support.

By the time you finish this book, you will have a firm foundation in Silverlight, and you will be able to create your own Silverlight-enabled applications.

xix

www.it-ebooks.info

CHAPTER 1

Welcome to Silverlight 5
This chapter introduces Silverlight, a Microsoft cross-browser, cross-platform plug-in that allows you to create rich interactive (or Internet) applications (RIAs) for the Web. It begins with a brief look at the evolution of user interfaces, and then provides an overview of Silverlight. You’ll learn how Silverlight fits into RIA solutions, the benefits it brings to developers, and the tools involved in developing Silverlightenabled applications.

The Evolution of the User Interface
Software user interfaces are constantly evolving and improving. I remember back when I was still working with an early version of Windows and looking at Mac OS with envy. Then I remember seeing Linux systems with radical new desktop interfaces. More recently, I found myself looking again at the Mac OS X Dock (shown in Figure 1-1) and wanting that for my Windows XP machine—to the point where I purchased a product that mimicked it. I was dedicated to Windows through it all, but I was envious of some of the user experiences the different environments offered.

Figure 1-1. The Mac OS X Dock feature The evolution of the user interface continues in the Windows operating system. Perhaps the most prominent difference between Windows 7 and previous versions of Windows is user-interface improvements. Microsoft was very intent on improving the richness of the operating system. One example is the new taskbar, where large icons replace the text descriptions and now when the user places the cursor over an icon Windows will display a window thumbnail, as shown in Figure 1-2.

Figure 1-2. Windows 7 taskbar Another user-interface improvement in Windows 7 is the Aero Snap. This feature allows you to easily maximize your window by dragging it to the top of the screen, but it also allows you to drag the

1 www.it-ebooks.info CHAPTER 1  WELCOME TO SILVERLIGHT 5

window to the left or right edge of the screen to tile the window to 50% of the left or right side of the screen as shown in Figure 1-3.

Figure 1-3. Windows 7 Aero Snap feature These features reflect how operating systems have evolved over the years, but the same evolution pertains to all areas of development, and for just about all industries. It is now expected that applications not only contain necessary features, but that they also have slick user interfaces. However, while rich applications are now expected, it is not necessarily the priority for developers. Over the last decade, there has been a struggle to find the right balance of “rich” and “reach” in applications. If you consider standard desktop applications, the applications are installed on individual client machines. They allow for very rich and responsive user interfaces and additional features, such as offline support. The performance of the application depends on the machine on which it is installed. While desktop applications have a very rich experience, they have very small reach. The application needs to have a code base for each target platform, and every machine needs to have the application installed and maintained. In contrast, we have web applications, which are HTML-focused programs designed to run within a browser and across platforms. For the Microsoft-based developer, this has recently meant developing with ASP.NET and building web services to offer services over the Internet. The focus of most of the logic and code has been placed on the server for the benefit of application performance. The price of this approach has been a poor user interface. These applications had excellent reach, but they were not very rich, as shown in Figure 1-4. Between these two extremes, there is a clear gap between the technologies.

2 www.it-ebooks.info CHAPTER 1  WELCOME TO SILVERLIGHT 5

Figure 1-4. Rich and reach application comparison To fill this gap, a new development approach has surfaced, as shown in Figure 1-5. This new approach is termed RIA (Rich Internet Applications), which is defined as a web application that has the features and functionality found in traditional desktop applications. There are a number of RIA technologies, including Microsoft’s Silverlight.

Figure 1-5. RIA fills the gap between rich and reach applications

3 www.it-ebooks.info CHAPTER 1  WELCOME TO SILVERLIGHT 5

Rich Internet Application Solutions
The concept of RIA has been around for quite some time, but the term “rich Internet application” was first used in 2002 in a Macromedia white paper. Before then, the terms “remote scripting” and “X Internet” were used to describe the concept. Today, many different solutions fit the description of RIAs, but there is one consistent characteristic: all RIA solutions involve a runtime that runs on the client machine and, architecturally, sits between the user and the server. In recent years, the technology most commonly used in RIAs has been Flash. When Flash was introduced, it brought to the Web rich user experiences never seen before. However, due to the lack of tools allowing Microsoft .NET developers to integrate Flash into their applications, to those developers Flash just seemed like a tool for adding some pretty effects to a web page, but nothing functional. Then a wonderful thing happened when Adobe purchased Macromedia. All of the sudden, Flash was married to some of the development tools 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, all RIAs have one characteristic in common: a client runtime that sits between the user and the server. In the case of Microsoft’s RIA solution, Silverlight is this client runtime. Specifically, Silverlight is a cross-platform, cross-browser plug-in that renders user interfaces and graphical assets on a canvas that can be inserted into an HTML page. The markup used to define a Silverlight canvas is called Extensible Application Markup Language (XAML, pronounced “zammel”). XAML is an XML-based language that is similar to HTML in some ways. Like HTML, XAML defines which elements appear, as well as the layout of those elements. However, unlike HTML, XAML goes far beyond simple element definition and layout. Using XAML, you can also specify timelines, transformations, animations, and events. The following is an example of a Silverlight canvas defined in XAML: Figure 1-6 shows this canvas in Microsoft Expression Blend, the design tool used to edit and create XAML for Silverlight applications. You can see that this XAML simply defines a rectangle on a canvas, as well as the properties associated with that rectangle, including its name, location, size, color, and border.

4 www.it-ebooks.info CHAPTER 1  WELCOME TO SILVERLIGHT 5

This simple example is just intended to give you an idea of what XAML looks like. You’ll learn more about XAML in upcoming chapters. For now, let’s continue by looking at the benefits of Silverlight.

Figure 1-6. A basic XAML canvas in Microsoft Expression Blend

Benefits of Silverlight
Naturally, Silverlight offers all of the same benefits of RIAs, but there are a few features that set it apart from other RIA solutions, including the following: • • • • • • • It offers cross-platform/cross-browser support. It provides a cross-platform version of the .NET Framework. XAML is a text-based markup language. Silverlight uses familiar technologies. Silverlight offers an Out of Browser and Full Trust option. Silverlight is the main development platform for Windows Phone 7. It’s easy to deploy the Silverlight runtime to clients.

Let’s take a closer look at each of these benefits.

5 www.it-ebooks.info CHAPTER 1  WELCOME TO SILVERLIGHT 5

Cross-Platform/Cross-Browser Support
When ASP.NET was released a number of years ago, one of the benefits touted was cross-browser support. Developers would need to have only one code base, and that code base would work in all modern browsers. For the most part, this is true. No matter which browser you are using, the application will function. However, to receive all of the bells and whistles offered by the ASP.NET controls, you must use the latest version of Internet Explorer. If you are using any other browser, you actually get a downgraded version of the web site, which contains fewer features. Validation controls are a prime example. If you are using a browser that ASP.NET recognizes as an “upscale” browser, you can take advantage of client-side validation. If you are using any other browser, the validation controls still function, but they require a postback to the server to do the validation. So, although ASP.NET is cross-browser capable, users can get different experiences, depending on which browser they are using. With Silverlight, this changes. Microsoft is once again pulling out the term “cross-browser,” and also adding “cross-platform”—and this time they mean it. As a developer, you can create a Silverlight application and rest assured that it will run exactly the same on all supported platforms and browsers. Currently, two platforms are supported. Naturally, the first is Windows-based platforms, and the second is Mac OS platforms. As for browser support, Internet Explorer, Firefox, Safari, and Google Chrome are currently covered. This leaves one large platform unsupported: Linux. Although Microsoft does not have plans to support Linux, others do. Moonlight is an open-source implementation of Silverlight, targeted primarily at Linux-based operating systems. Moonlight is part of the Mono project, an open-source initiative to develop and run .NET client and server applications on Linux, Solaris, Mac OS X, Windows, and Unix. Although Moonlight brings Silverlight features to Linux, the project lags behind the aggressive Microsoft release cycles.

Cross-Platform Version of the .NET Framework
Silverlight 1.0 was released by Microsoft in the summer of 2007, but this version supported only Ecma languages that are 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 its own cross-platform version of the .NET Framework, which means it has its own version of the common language runtime (CLR), the full type system, and a .NET Framework programming library you can use in Visual Studio 2010 to build rich user experiences in the browser.

Use of Familiar Technologies
Microsoft is very good at creating tools that make application development easy. The Visual Studio integrated development environment (IDE) has been around for quite some time, and although new features are continually added to the tool, the environment itself has remained remarkably consistent. Silverlight development is no different. At the core of developing Silverlight applications is Visual Studio 2010, the latest version in Visual Studio’s long history. This gives Silverlight a distinct advantage, because developers do not need to learn how to use a new development environment. In addition to Visual Studio, Microsoft has a suite of tools called Expression Studio. Included in this suite is Microsoft Expression Blend, which is used to edit and create XAML for Silverlight applications. While Expression Blend looks completely different, it still has many of the same elements as Visual Studio. In addition, Expression Blend works off of the same project as Visual Studio. This means that as

6 www.it-ebooks.info CHAPTER 1  WELCOME TO SILVERLIGHT 5

you make changes in each of the editors—opening a project in Visual Studio, and then opening the same project in Expression Blend to edit the XAML—the edited files will request to be refreshed when opened again in the other tool.

Small Runtime and Simple Deployment
Because 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 1 MB. For Silverlight 2, however, they had a harder chore ahead of them because Silverlight 2 contains its own .NET Framework and object library. Microsoft went to each .NET Framework team and allocated it a size to fit its portion. The result is astonishing—Silverlight 2 is approximately 4 MB in size. In Silverlight 5, even with the large amount of new features that have been added to the Silverlight runtime, the file size is still about 6 MB for the 32-bit version and just over 12 MB for the 64-bit version. 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, it will display a logo, similar to the one shown in Figure 1-7.

Figure 1-7. Silverlight runtime required logo When users click the icon in the logo, the Silverlight runtime will start to download. Once the runtime is finished installing, the Silverlight application is immediately available to the user.

The Silverlight Development Environment
In the past, setting up an environment to work with Microsoft’s latest and greatest has been relatively straightforward, typically involving only the setup of the latest version of Visual Studio and the appropriate software development kit (SDK). However, with Silverlight, the situation is quite a bit different due to the introduction of many new tools. Let’s look at the tools involved in setting up a Silverlight development environment.

7 www.it-ebooks.info CHAPTER 1  WELCOME TO SILVERLIGHT 5



Visual Studio 2010: As noted, this is the latest version of Microsoft’s IDE (shown in Figure 1-8). For your Silverlight environment, you should install Visual Studio 2010, which automatically installs Microsoft .NET Framework 4. Chapter 2 covers Visual Studio 2010 in more depth.

Figure 1-8. Microsoft Visual Studio 2010 • Silverlight Tools for Visual Studio 2010: This is a package that adds the necessary items to Visual Studio to teach it how to handle Silverlight projects. The package includes a number of items, some of which are listed here: • • Silverlight 5 Runtime: Required on every computer that you want to be able to view a Silverlight-enabled web application. Silverlight 5 Software Development Kit: This SDK is a collection of samples, Silverlight QuickStarts, documentation, and controls that are used to develop Silverlight applications. Silverlight Project Templates for Visual Studio 2010: This adds the Silverlight templates in Visual Studio. As an example, it will add the template that enables you to create a Silverlight project from the Add New Project dialog box in Visual Studio.



8 www.it-ebooks.info CHAPTER 1  WELCOME TO SILVERLIGHT 5



Expression Blend 4 Preview for Silverlight 5: The next thing to install for your Silverlight development environment is Expression Blend (shown in Figure 1-9). Expression Blend is a design tool for building XAML-based interfaces, including Windows Presentation Foundation (WPF) and Silverlight. Expression Blend is not required for creating Silverlight solutions, but it provides a richer designer than does Visual Studio 2010. Expression Blend is covered in detail in Chapter 11.

Figure 1-9. Microsoft Expression Blend preview for Silverlight 5 • Silverlight 5 Toolkit. The Silverlight Toolkit is an open-source CodePlex project whose goal is to develop additional controls for Silverlight applications. Controls within the toolkit are assigned a status that describes their maturity as controls, and the controls are supported by the open-source community. You can download the toolkit for Silverlight 5 at http://silverlight/codeplex.com. The Toolkit is discussed in Chapter 6.

Summary
In this chapter, you looked at the evolution of user interfaces in applications, as well as the history of RIAs. I then introduced Silverlight, talked about the benefits it brings to developers today and how it fits into RIA solutions. Finally, you learned about the tools involved in developing Silverlight-enabled applications. Now it is time to get your hands dirty and start building some Silverlight applications! In the next chapter, I will provide an introduction to Microsoft Visual Studio 2010, one of the primary tools used to build Silverlight applications.

9 www.it-ebooks.info CHAPTER 2

Introduction to Visual Studio 2010
The previous chapter mentioned the tools required to develop Rich Internet Applications (RIAs) that utilize the Silverlight technology. At the core of all of these tools is Microsoft’s flagship development product, Visual Studio. This chapter provides an introduction to Visual Studio 2010, the latest version. You will learn about some of the new features that are particularly helpful for developers building RIAs with Silverlight, and you will have an opportunity to create your first Silverlight application using Visual Studio 2010. Let’s get started with a brief introduction to the Visual Studio IDE.

What Is Visual Studio?
Any developer who has developed applications using technologies related to Microsoft Visual Basic, ASP, or .NET has used some version of Visual Studio on a regular basis. This is because Visual Studio is Microsoft’s primary development product. Whether you are developing desktop applications, web applications, mobile applications, web services, or just about any other .NET solution, Visual Studio is the environment you will be using. Visual Studio is an IDE that allows .NET developers to implement a variety of .NET solutions within the confines of one editor. An IDE is a software application that contains comprehensive facilities to aid developers in building applications. Visual Studio fits this description for a number of reasons. First, Visual Studio offers a very rich code-editing solution. It includes features such as source code colorcoding and code completion. Second, it offers an integrated debugger, which allows you to place breakpoints in your source code to stop execution at any given point, as well as step through the source line by line, analyzing the state of objects and fields at any given point in the execution. Add to these features rich support for application deployment, installation, and integration with database services, and you can understand how Visual Studio is an extremely valuable tool for developers.

 Note This book assumes a basic understanding of Visual Studio. If you’re new to Visual Studio, I recommend that you get started with a book devoted to the subject, such as Introducing .NET 4.0: With Visual Studio 2010, by Alex Mackey (Apress, 2009).

11 www.it-ebooks.info CHAPTER 2  INTRODUCTION TO VISUAL STUDIO 2010

THE HISTORY OF VISUAL STUDIO
Visual Studio has quite a history. The first version was called “Visual Studio 97,” which was most commonly known for Visual Basic 5.0. In 1998, Microsoft released Visual Studio 6.0. That version included Visual Basic 6.0, as well as Microsoft’s first web-based development tool, Visual InterDev 1.0, which was used to develop ASP applications. Next came the introduction of Microsoft .NET and ASP.NET 1.0, prompting Visual Studio.NET. As Microsoft was enhancing and releasing new versions of Microsoft .NET and ASP.NET, it also continued enhancing Visual Studio by releasing Visual Studio 2003 and then Visual Studio 2005. In addition, Microsoft has introduced a line of free development tools known as the Visual Studio Express tools, as well as the Visual Studio Team System, which can be used by large programming teams to build enterprise-level systems. Microsoft released Visual Studio 2008 under the code name Orcas and added a number of features, such as some enhanced JavaScript debugging as well as some IDE improvements. As for Visual Studio 2010, Microsoft started from the ground up and developed the IDE on top of WPF to make a number of new features possible.

What’s New in Visual Studio 2010?
Microsoft has introduced a variety of new features in Visual Studio 2010, many of which are geared toward helping developers build RIAs with Silverlight and related Microsoft technologies, such as the Windows Communication Foundation (WCF), ADO.NET Data Services, and Ajax. Let’s look at some of the new features in Visual Studio 2010 that are particularly helpful to Silverlight application developers.

Support for Multiple Monitors
Today it is common for developers to have multiple monitors attached to their development workstations and, unfortunately, previous versions of Visual Studio never took advantage of this extra real estate. Developers could use the extra screens for other applications, but often it would be nice to be able to view more than one source file at once. Visual Studio 2010 adds support for multiple monitors by allowing developers to pull source files, windows, and more out of the Visual Studio primary IDE and move them to other monitors. Take, for example, Figure 2-1. Visual Studio 2010 is open on the left monitor, and we are viewing the MainPage.xaml. However, I would like to inspect the class that my UI is bound to at the same time. To do so, I can simply drag the class out of the docked position in the IDE and move it to the second monitor. I could do the same thing for the toolbox, the properties window, or any window within Visual Studio 2010. A very nice new feature!

12 www.it-ebooks.info CHAPTER 2  INTRODUCTION TO VISUAL STUDIO 2010

Figure 2-1. Multimonitor support

Zoom Support for Source Editor
Because Visual Studio 2010 has been almost completely rewritten in Windows Presentation Foundation (WPF), a number of user-interface enhancements have been added. One of these enhancements is the ability to zoom in and out of the source code editor. This feature allows you to simply use your mouse scroll wheel to zoom in to your source code. This is especially useful during presentations; however, there are times where developers could benefit from the ability to zoom in and out. For example, if you want to see a bigger portion of your source, you can zoom out and see your source from a greater distance, as shown in Figure 2-2.

13 www.it-ebooks.info CHAPTER 2  INTRODUCTION TO VISUAL STUDIO 2010

Figure 2-2. Zooming out in the Source Editor

Improved IntelliSense
Every developer who has used Visual Studio has become dependent on IntelliSense. When it is gone, you really notice how much you take it for granted and you find how dependent you really have become on it. Well, as great as IntelliSense is, in Visual Studio 2010 it just got better. The addition of partial string matching allows IntelliSense to more intelligently display entries based on what the developer has typed. For example, if the developer types OC, IntelliSense will display entries such as ObservableCollection, as shown in Figure 2-3, (notice that the letters OC are the capital letters of the Pascal-Cased object). In addition, if you type collect, ObservableCollection will also appear because the build is contained in the object name.

14 www.it-ebooks.info CHAPTER 2  INTRODUCTION TO VISUAL STUDIO 2010

Figure 2-3. Improved IntelliSense in Visual Studio 2010 In addition to the partial string matching, the performance of IntelliSense has been dramatically improved. This allows developers to code without delays in IntelliSense and keep their rolls rolling.

Add References Performance
In Visual Studio 2008 and previous versions, developers noticed that when you open the Add Reference dialog (shown in Figure 2-4), it takes quite a bit of time for it to fully display the listing of components, especially for the COM components. In Visual Studio 2010, the Add Reference dialog has undergone many performance improvements, which will help prevent developers from having to wait on the Visual Studio IDE.

15 www.it-ebooks.info CHAPTER 2  INTRODUCTION TO VISUAL STUDIO 2010

Figure 2-4. Add Reference dialog

Reference Highlighting
When you click anywhere within a symbol (class name, variable, object, field, and so on), all instances of that item are highlighted throughout the current document, as shown in Figure 2-5. This is not a simple text matching; the editor is smart enough to understand the scope of the different variables. So even if you have two items with identical names that belong to different objects, they are not both highlighted. You can easily navigate between the different highlighted instances as well by pressing Ctrl+Shift+Up to move to the next instance, or Ctrl+Shift+Down to move to the previous instance.

16 www.it-ebooks.info CHAPTER 2  INTRODUCTION TO VISUAL STUDIO 2010

Figure 2-5. Highlight references

Box Selection
Another cool feature of Visual Studio 2010 is box selection. Consider the situation where you would have the following source: public public public public string string string string Nickname { get; set; } Notes { get; set; } Card1 { get; set; } Card2 { get; set; }

Let’s say that you wanted to change all of these to be private properties instead of public. In Visual Studio 2010, you can hold down the Alt key and drag a box around just the public declarations, as shown in Figure 2-6.

17 www.it-ebooks.info CHAPTER 2  INTRODUCTION TO VISUAL STUDIO 2010

Figure 2-6. Box selection in Visual Studio 2010 Better yet, once you have this box selected you can edit all of the lines at once. With the selection made, simply type private and it will automatically replace public in each of the lines, as shown in Figure 2-7.

18 www.it-ebooks.info CHAPTER 2  INTRODUCTION TO VISUAL STUDIO 2010

Figure 2-7. Editing a box selection in Visual Studio 2010

Call Hierarchy Window
The Call Hierarchy window displays two lists regarding a given member: first, all calls to the member (incoming) and then all calls from the member (outgoing). Within each of those lists, you can then drill into each member and see its incoming and outgoing calls, forming a very useful call hierarchy. To open the Call Hierarchy window, simply right-click on any method, property, or constructor and select View Call Hierarchy. An example of the Call Hierarchy window is shown in Figure 2-8.

19 www.it-ebooks.info CHAPTER 2  INTRODUCTION TO VISUAL STUDIO 2010

Figure 2-8. Call Hierarchy window

Code Generation
Visual Studio 2008 introduced the ability to generate a method stub from a given call in the source. Consider the following line of code calling the method NewMethod, which did not exist, and passing it an integer and a string: Person.NewMethod(123, "Bob"); If you type this, Visual Studio will provide you with the option to Generate method stub for ‘NewMethod’. By selecting this, Visual Studio will automatically create the following method stub in the Person class: class Person { internal static void NewMethod(int p, string p_2) { throw new NotImplementedException(); } } Visual Studio 2010 expands on this code generation functionality by now allowing you to automatically generate classes, structs, and enumerations.

Extension Manager
Managing extensions and controls has always been a stressful point with Visual Studio 2008 and previous versions of Visual Studio. In Visual Studio 2010, the Extension Manager has been introduced. It allows you to easily browse an online library for different extensions as well as manage the extensions

20 www.it-ebooks.info CHAPTER 2  INTRODUCTION TO VISUAL STUDIO 2010

that are installed in your current development environment. You can view the Extensions Manager by selecting Extensions Manager from the Tools menu. The Extension Manager is shown in Figure 2-9.

Figure 2-9. Extension Manager

Building Your First Silverlight Application in Visual Studio
The best way to explore the Visual Studio IDE is to get your hands dirty and play around with it. Let’s build a Silverlight application.

Try It Out: Hello World in Silverlight 5
In this exercise, you’ll build the Hello World Silverlight 5 application. I personally hate the Hello World sample, but it is used often because it is so simple and provides a good introduction. Who am I to break with tradition? Let’s get started. 1. Start Visual Studio 2010, and select File ➤ New ➤ Project from the main menu.

21 www.it-ebooks.info CHAPTER 2  INTRODUCTION TO VISUAL STUDIO 2010

2.

In the New Project dialog box, select Visual C# as the project type, and in the list under that type, choose Silverlight. Select Silverlight Application as the template, and name the project HelloWorld, as shown in Figure 2-10. Then click OK.

Figure 2-10. Creating a new Silverlight project 3. Visual Studio will display the New Silverlight Application dialog box, informing you that your Silverlight application needs to be hosted in an HTML web page. It offers the choices of hosting the Silverlight application in a web site or within a project. For this exercise, select Web Application Project and stick with the default name of HelloWorld.Web, as shown in Figure 2-11. Then click OK. See the next section for more information about choosing whether to use a web site or Web Application project for your own Silverlight applications.

22 www.it-ebooks.info CHAPTER 2  INTRODUCTION TO VISUAL STUDIO 2010

Figure 2-11. The New Silverlight Application dialog box 4. Visual Studio will now create the base project for you. Notice that there are two projects created within your solution: one called HelloWorld.Web and one called HelloWorld, as shown in Figure 2-12.

23 www.it-ebooks.info CHAPTER 2  INTRODUCTION TO VISUAL STUDIO 2010

Figure 2-12. The default Silverlight project created in Visual Studio 2010 5. Visual Studio has already opened the MainPage.xaml file, which is where you will start working. Let’s begin by adding a TextBlock control, which will display our “Hello World!” message. Add the TextBlock within your Canvas object, as follows: 6. Save the project and run it by pressing F5. If you see the Debugging Not Enabled dialog box, select “Modify the Web.config to enable debugging” and click OK. The result should be as shown in Figure 2-13.

24 www.it-ebooks.info CHAPTER 2  INTRODUCTION TO VISUAL STUDIO 2010

Figure 2-13. Your first Silverlight application in Visual Studio 2010 7. I know this isn’t very interesting, so let’s change things up a bit by setting the display message in the MainPage.xaml.cs code behind. In the code behind, you will notice a constructor for your Page class, which contains one method called InitializeComponent(). Under that method, change the Text property of your TextBlock as follows (the line shown in bold): namespace HelloWorld { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); this.HelloMessage.Text = "Hello Universe!"; } } 8. }

Rebuild the application, and run it again. Your result should look like Figure 2-14.

25 www.it-ebooks.info CHAPTER 2  INTRODUCTION TO VISUAL STUDIO 2010

Figure 2-14. The final result from our first Silverlight application in Visual Studio 2010 9. Close the application.

There you go! You have built your first Silverlight application. Of course, this application is extremely simple, but you did get an idea of how things work in Visual Studio 2010.

Hosting Your Silverlight Application: Web Site or Web Application?
In Visual Studio 2010, should you use a web site project or a Web Application project to host your Silverlight application? The main difference between a web site project and a Web Application project is how the files are compiled and deployed. Each has its advantages and disadvantages. In the end, the choice pretty much comes down to user preference. Let’s take a quick look at each approach.

Using a Visual Studio Web Site
A Visual Studio web site is nothing more than a group of files and folders in a folder. There is no project file. Instead, the site simply contains all the files under the specific folder, including all text files, images, and other file types. A Visual Studio web site is compiled dynamically at runtime. An assembly will not be created, and you won’t have a bin directory. The following are some advantages of using a Visual Studio web site: • You don’t need a project file or virtual directory for the site.

26 www.it-ebooks.info CHAPTER 2  INTRODUCTION TO VISUAL STUDIO 2010



The site can easily be deployed or shared by simply copying the folder containing the site.

The following are some disadvantages of this approach: • • There is no project file that you can double-click to open the site in Visual Studio. Rather, you must browse to the folder after opening Visual Studio. By default, all files within the site’s directory are included in the web site project. If there are files within the site’s directory that you do not want to be a part of the web site, you must rename the file, adding the extension .exclude.

Using a Visual Studio Web Application Project
A Visual Studio Web Application project is the more traditional type of web project used prior to Visual Studio 2005. When Microsoft developers introduced the “web site” concept, they did not take into account the many developers who were comfortable with the project-based solution approach. To accommodate those developers, Microsoft announced the Visual Studio 2005 Web Application project as an add-on to Visual Studio 2005. In Visual Studio 2008 and 2010, this project type is once again a part of Visual Studio. The following are some of the advantages of using a Web Application project: • • • All of the code files are compiled into a single assembly, placed in the bin directory. You can easily exclude files from a project, because all files within the project are defined within the project file. It’s easier to migrate from older versions of Visual Studio.

A disadvantage is that it can be more difficult to share your solution with others, if that is your intent. In the end, both approaches have their pros and cons. You need to determine which one is more suitable for your application, depending on your specific purpose and goals. For more information about these project types, refer to the MSDN documentation.

Summary
This chapter introduced Visual Studio 2010 and some of the new features offered in this version, including support for multiple monitors and managing extensions. In addition, you built your very first Silverlight application. In the next chapter, you are going to start to dive into some of the Silverlight controls, beginning with the layout management controls. These controls enable you to lay out your Silverlight applications.

27 www.it-ebooks.info CHAPTER 3

Layout Management in Silverlight
The previous chapter provided an overview of Microsoft Visual Studio 2010, one of the primary tools used in developing Silverlight applications. In this chapter, you are going to start to dive into some Silverlight development by looking at the layout management controls. As you learned, Silverlight applications consist of a number of Silverlight objects that are defined by XAML. Layout management involves describing the way that these objects are arranged in your application. Silverlight includes five layout management controls: Canvas, StackPanel, Grid, WrapPanel, and DockPanel. You will take a look at each of these in-depth. By the end of this chapter, you should have a good understanding of when to use which layout control.

Layout Management
Silverlight provides a very flexible layout management system that lets you specify how controls will appear in your Silverlight application. You can use a static layout as well as a liquid layout that allows your layout to automatically adjust as your Silverlight application is resized in the browser. Each of the five layout controls provided in Silverlight has its advantages and disadvantages, as summarized in Table 3-1. Let’s begin by looking at the most basic layout control: the Canvas panel. Table 3-1. Layout Control Pros and Cons

Control
Canvas

Description
Based on absolute position of controls. Based on horizontal or vertical “stacks” of controls.

Pros
Very simple layout.

Cons
Requires that every control have a Canvas.Top and Canvas.Left property attached to define its position on the canvas. The layout is limited to stacks of items. Spacing is limited to adding margins to the individual controls and to adjusting the alignment (with the VerticalAlignment and HorizontalAlignment properties).

StackPanel

Allows for a quick dynamic layout. Nesting StackPanel controls can provide some interesting layouts.

Continued

29 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

Control
Grid

Description
Mimics using table elements in HTML to lay out controls.

Pros
The most flexible and powerful layout control. You can define just about any type of layout using the Grid control. Very similar to the StackPanel, except the WrapPanel automatically wraps items to a second row or column so that it is ideal for layouts containing an unknown number of items. Provides an easy way to create a basic layout, consuming the entire application space in vertical or horizontal panels.

Cons
Grid definitions can get somewhat complex at times. Nesting Grid components can be confusing.

WrapPanel

Based on horizontal or vertical “stacks” of controls wrapping to a second row or column when the width or height is reached. Layout is based on “docked” horizontal or vertical panels.

Limited control of layout because wrapping is automatic when items reach the maximum width or height.

DockPanel

Layout is limited to horizontal or vertical “fill” panels, often used in conjunction with other nested layout controls.

The Canvas Panel
The Canvas panel is a basic layout control that allows you to position Silverlight objects using explicit coordinates relative to the canvas location. You can position an object within the Canvas panel by using two XAML attached properties: Canvas.Left and Canvas.Top. Figure 3-1 shows how the object’s position is affected by these properties.

Figure 3-1. The XAML attached properties Canvas.Top and Canvas.Left allow you to position the Canvas.

30 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

The objects within a Canvas panel have no layout policies placed on them by the layout control and will not resize automatically when your application is resized within the browser.

Try It Out: Using the Canvas Panel
Let’s try out a quick example of using the Canvas panel. 1. Open Visual Studio 2010, and create a new Silverlight application called CanvasPanel. Allow Visual Studio to create a Web Application project to host the application. When the project is created, you should be looking at the MainPage.xaml file. If you do not see the XAML source, switch to that view so that you can edit the XAML. Within the main Grid element, add a Canvas element. Assign it a Width property of 300 and a Height property of 300. To see the Canvas panel in the application, also set the background color to green. The following XAML adds this Canvas: 3. At this point, your Silverlight application doesn’t look that exciting. It contains only a single green rectangle positioned at the very center of your application, as shown in Figure 3-2.

2.

Figure 3-2. Default Canvas with a green background

31 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

4.

Let’s add a button to this Canvas panel. Add the following code to place the button, which has the label Button1, a Width property of 100, and a Height property of 30. (The Button control is covered in detail in Chapter 4.)

5.

Figure 3-3 shows the button within the canvas.

Figure 3-3. Single button within the canvas 6. Let’s add another button to the Canvas, but this time position it below and a bit to the right of the first button by setting its Canvas.Top and Canvas.Left as attached properties. Give this button the label Button 2, as follows:

32 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

7.

At this point, you now have two buttons within the canvas, but at different locations, as shown in Figure 3-4. This is still not very exciting, but this is about as cool as it gets with the Canvas.

Figure 3-4. Two buttons positioned relative to the canvas 8. Go ahead and run the solution to see the end result as it will appear in the browser. The output is shown in Figure 3-5.

33 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

Figure 3-5. The canvas and two buttons as seen in a browser

Filling the Entire Browser Window with Your Application
By default, in a new Silverlight project, the root UserControl object is set to a width of 400 and a height of 300. In some cases, you might want to set the width and height of your Silverlight application within the browser. At other times, however, you will want your Silverlight application to take up the entire window of your browser, and to resize as the browser is resized. This is done very easily within Silverlight. When you want the width and height to be set to 100%, simply omit the element’s Height and Width attributes. As an example, the following source has been adjusted for the Canvas panel and the Silverlight application to take up the entire browser:

34 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

With the omission of the Height and Width declarations for UserControl and Canvas, when you run the Silverlight application, you will see that the canvas takes up 100% of the browser window, as shown in Figure 3-6. It will resize as the browser resizes.

Figure 3-6. Silverlight application taking up the entire browser As you’ve seen, the Canvas panel is a simple layout control. It can be used very effectively in a fixed layout. However, in most cases, you will want to use a static layout for your applications. The StackPanel control provides a more fluid layout control.

The StackPanel Control
The StackPanel provides developers with a quick layout option for positioning objects. The StackPanel control allows you to position Silverlight objects in more of a flow layout, stacking objects either horizontally or vertically. Figure 3-7 shows the basic concept of this layout control.

35 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

Figure 3-7. The StackPanel control orientations

Try It Out: Using the StackPanel Control
To better understand the StackPanel control, let’s run through an exercise. 1. 2. In Visual Studio 2010, create a new Silverlight application named StackPanel and allow Visual Studio to create a web site project to host the application. When the project is created, you should be looking at the MainPage.xaml file. If you do not see the XAML source, switch view so that you can edit the XAML. Within the main Grid element, add a StackPanel control and also three buttons with the labels Button 1, Button 2, and Button 3. Give all three buttons a width of 100 and a height of 30. The following XAML adds the StackPanel control and buttons (the new code is highlighted in bold in all the exercises): 3. At this point, your application should appear as shown in Figure 3-8. Notice that the buttons are stacked vertically. This is because the default stacking orientation for the StackPanel control is vertical.

36 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

Figure 3-8. The StackPanel control with its default orientation 4. Change the orientation of the StackPanel control to be horizontal by setting the Orientation property to Horizontal, as follows: 5. With this simple change, the buttons are now stacked horizontally, as shown in Figure 3-9.

37 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

Figure 3-9. The StackPanel control with horizontal orientation 6. Notice that all the buttons are touching each other, which is unattractive. You can easily space them out by using their Margin property. In addition, you can center the buttons by setting the StackPanel control’s HorizontalAlignment property to Center. Other options for HorizontalAlignment include Left, Right, and Stretch (which stretches the content to the left and right). Make the following changes to adjust the buttons: 7. After you have made these changes, your buttons are spaced out nicely in the center of the application, as shown in Figure 3-10.

38 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

Figure 3-10. The StackPanel control with buttons spaced apart and centered

Try It Out: Nesting StackPanel Controls
Microsoft designed the control framework so that any object can be contained within another object. One way you can enhance your layout is by nesting a layout control within another layout control. In this example, you will nest a StackPanel control within another StackPanel control, but realize that you can nest any layout control within any other layout control to get the exact layout functionality you are seeking. 1. In Visual Studio 2010, create a new Silverlight application named NestedStackPanel and allow Visual Studio to create a Web Application project to host the application. In the MainPage.xaml file, add the following items: • • • A StackPanel control to the root Grid with its Orientation property set to Horizontal and the HorizontalAlignment property set to Center. Within that StackPanel, add two buttons, with the labels Button Left and Button Right. In between the two buttons, add another StackPanel with Orientation set to Vertical and VerticalAlignment set to Center.

2.

39 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

• •

Within that nested StackPanel, include three buttons with the labels Button Middle 1, Button Middle 2, and Button Middle 3. All buttons should have a Margin property set to 5, a Height set to 30, and a Width set to 100.

Here is what the updated source looks like: The cool result of this code is shown in Figure 3-11.

40 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

Figure 3-11. Nested StackPanel controls 3. Run the application to see the results.

As you can see from these two exercises, the StackPanel control is a very useful layout option, and you will probably use it often in your Silverlight applications. By nesting Silverlight controls, you have a lot of flexibility when designing your applications. However, in the event that you want more control over the positioning of items in your application, without needing to resort to the absolute positioning used by the Canvas control, the Grid control might be just the layout option you need.

The Grid Control
The Grid control provides more fine-tuned layout in Silverlight applications. As a comparison, you can think of using the Grid layout control as similar to using table elements to position items in HTML, only more flexible. With the Grid control, you can define rows and columns, thus creating grid cells, and then add objects to individual cells in the grid or to multiple cells by using spanning. To specify in which cell to place an object, you use the Grid.Column and Grid.Row attached properties. Note that these properties are base zero, so the top-left cell is row 0 and column 0. Figure 3-12 illustrates the row and column locations for the grid.

41 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

Figure 3-12. Row and column grid cell locations in the Grid control layout For most developers, the Grid control most likely will be the layout option of choice because of its flexibility. At the same time, the Grid control is significantly more complex than the others, as you’ll see in the following exercises.

Try It Out: Using the Grid Control
Let’s try out a simple Grid panel with four buttons: 1. In Visual Studio 2010, create a new Silverlight application named GridPanel and allow Visual Studio to create a Web Application project to host the application. For this example, you are going to need a bit more space in which to work. In the MainPage.xaml file, start out by changing the UserControl’s DesignWidth to 600 and DesignHeight to 400, as follows:

2.

42 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

3.

Notice that, by default, a Grid control is already added to the page. To better see what is going on, turn on the display of grid lines by setting the ShowGridLines property to true. The following code shows these additions. Keep in mind that because you have not designated a size for the grid, it will automatically take up the entire size of the parent, and in this case, the entire Silverlight application.

4.

Next, define the rows and columns in the Grid control. You do this using the XAML property elements Grid.RowDefinitions and Grid.ColumnDefinitions. Add the following XAML to your new grid:

5.

Notice that for the center row and column, you are setting the Height and Width properties to "*". The asterisk tells the row and column to take up all available space. As the Grid control is resized with the browser window, those columns will be resized to take up all the space not consumed by the fixedsized columns. After you have added these row and column definitions, your canvas should appear as shown in Figure 3-13.

43 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

 Note In the previous source, you are setting the height and width of the rows and columns to fixed pixel-based values. You can also set the height and width using star sizing, which indicates that the value will be expressed as a weighted proportion of the available space. As an example, if you had two rows, and the height of the first row was set to * and the height of the second row was set to 2*, the first row would take up a third of the available space, while the second row would take up two thirds of the available space.

Figure 3-13. Grid with columns and rows 6. You can now add objects to the different grid cells. Place a button in each of the four corner cells, giving the buttons the corresponding labels Top Left, Top Right, Bottom Left, and Bottom Right. To place the buttons, add the following code:

44 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

7. After the buttons are added, your application should look like Figure 3-14.

45 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

Figure 3-14. The grid with buttons in the four corners

Try It Out: Nesting a Grid and Spanning a Column
Next, you will nest another Grid control in the center cell of the Grid control you just added. This will make the application layout somewhat complex, but it will also serve to show how Grid panels are defined using XAML. 1. In the MainPage.xaml within the GridPanel project, add the following items: • • • 2. A Grid control positioned at Grid.Column=1 and Grid.Row=1 Three RowDefinition and two ColumnDefinition elements Buttons in the four corners of the new Grid control, as you just did in the outer Grid panel

The source code should look like the following:

46 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

At this point, your application should look like Figure 3-15. Now, this is a pretty cool layout.

Figure 3-15. Nested grid with buttons

48 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

3.

Notice that you have not placed anything in the two columns in the middle row of the new grid. Here, you’re going to add a button that spans these two columns, so the button will appear in the center of the row. To do this, add the new button to the Grid control with the Grid.ColumnSpan attached property set to 2. The source changes to the innermost Grid control are as follows:

49 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

4.

Now that you have added the button to the center column, your application should look like Figure 3-16. Notice how the button spans the two columns and appears in the center. For experienced HTML developers who are used to laying out their forms with tables, this approach should be very comfortable because it closely mimics using the colspan attribute for a tag.

Figure 3-16. Final application with a nested grid and buttons In this example, you saw how to create a relatively complex layout using the Grid control. As you can see, this is a very powerful and flexible layout tool for your Silverlight applications.

The WrapPanel Control
The WrapPanel control was first released in Silverlight 3 via the Silverlight Toolkit. It is very similar to the StackPanel control with one major difference: when items in a WrapPanel will not fit within the width or height of the control, they automatically wrap to a new row (if horizontal orientation) or column (if vertical orientation). This makes the WrapPanel ideal for laying out an unknown number of items as they will automatically wrap to take up the entire space of the control. As an example, if you look at Figure 3-17 you will see how the WrapPanel handles placing six items when set to horizontal and vertical orientations. Horizontally, the WrapPanel places the items one after the other to the right, until no other items can fit within the width of the control. At that time, it starts to place the items in a new row directly below the first row. The same is true for vertical orientation except the items are stacked below the previous item until new items cannot fit within the height of the control, at which time they are placed directly to the right of the previous row.

50 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

Figure 3-17. The WrapPanel control orientations

Try It Out: Using the WrapPanel Control
In this exercise, you will explore the WrapPanel control and how it can be used to display an unknown number of items in stacks vertically and horizontally. Let’s get started. 1. Open Visual Studio 2010, and create a new Silverlight application called WrapPanel. Allow Visual Studio to create a Web Application to host the application. When the project is created, the file MainPage.xaml is automatically created and opened in the XAML designer. You will add two rows to the root Grid control and then place a WrapPanel in the first row and a button with the label Add New Item in the second row. The WrapPanel is not part of the core Silverlight control set, but rather it is part of the Silverlight Toolkit. Because of this, you need to make certain you have the Toolkit downloaded and installed. For more information on the Toolkit, please see Chapter 6. To get the proper XML namespace added for the WrapPanel, add it by double-clicking on the control from the Toolbox in Visual Studio. That way Visual Studio will automatically add the Xml namespace to the page. When the WrapPanel is first added, you will notice that it has some properties set that you might not want set. 5. In this case, you want the WrapPanel to take up all the available space of the top row of your Grid. You do not need the HorizontalAlignment, Margin,

2.

3.

4.

51 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

VerticalAlignment, or Width property set, so you can either delete these properties manually in the source or use a feature in Visual Studio 2010 to assist. In the designer, right-click on the WrapPanel and select Reset Layout ➤ All as shown in Figure 3-18.

Figure 3-18. Resetting elementlLayout with Visual Studio 2010 6. You will then notice that the WrapPanel definition is reduced to the following source: 7. Once the WrapPanel has been added, your XAML source should appear as follows:

52 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

8. Now you need to add the code behind the button click event. Right-click on addItem_Click in the XAML and choose “Navigate to Event Handler.” This takes you to the code behind of MainPage.xaml. Add the following code within the addItem_Click event handler: private void addItem_Click(object sender, RoutedEventArgs e) { Rectangle newRect = new Rectangle(); newRect.Width = 50; newRect.Height = 50; newRect.Margin = new Thickness(5); newRect.Fill = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0)); wrapPanel1.Children.Add(newRect); } 9. You can now test the application. Once the application appears, start pressing the Add New Item button and watch the items appear horizontally as well as wrap to a new row when a new item cannot fit within the width of the control. (See Figure 3-19.)

53 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

Figure 3-19. Completed horizontal WrapPanel 10. At this point, you can then go into the XAML designer for MainPage.xaml, add the property Orientation="Vertical" to the WrapPanel, and test the application once again. This time you will notice that the items appear vertically and wrap to new columns once they reach the maximum height, as shown in Figure 3-20.

Figure 3-20. Completed vertical WrapPanel

54 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

The DockPanel Control
The DockPanel control was first released in Silverlight 3 via the Silverlight Toolkit. It provides the ability to dock controls in all four directions: top, bottom, right, and left. Consider Figure 3-21, which is a possible layout with the DockPanel control involving five controls. The first two controls are docked in the left panel; the third control is docked in the top-center panel; the fourth control is docked in the bottom-center panel; and the fifth control is docked in the right panel.

Figure 3-21. Possible layout with the DockPanel To achieve this layout without the DockPanel would involve nested layout controls or a fairly complex Grid control. The point is that for certain situations the DockPanel can definitely be a very effective control.

Try It Out: Using the DockPanel Control
In this exercise, you will explore the DockPanel control and how it can be used to lay out controls docked in different directions. 1. Open Visual Studio 2010, and create a new Silverlight application called DockPanel. Allow Visual Studio to create a Web Application to host the application. When the project is created, the file MainPage.xaml is automatically created and opened in the XAML designer. You will add a DockPanel to the root Grid and then add buttons that are docked in different positions. Just like you did with the WrapPanel in the previous section, to get the proper XML namespace added for the DockPanel, add it by double-clicking on the control from the Toolbox in Visual Studio. That way, Visual Studio will automatically add the Xml namespace and assembly reference to the page.

2.

3.

55 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

Once the panel has been added, you can then modify the tag how you would like. 4. The default dock behavior is to dock the control left. However, if you want to change that, you can use the Dock extended property to change this behavior. As an example, to dock a control to the right, you would add the property controls:DockPanel.Dock="Right" to the control. (Note that the xmlns attribute is included, which is required.) When you are finished adding the controls, your XAML should look like the following: 6. The result of this code should appear as shown in Figure 3-22.

5.

Figure 3-22. Buttons placed in the DockPanel 7. Notice that the last button placed in the DockPanel automatically fills the remaining space. This is the default behavior of the DockPanel. However, if you

56 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

do not want the DockPanel to do this, simply add the LastChildFill property set to False to the DockPanel. 8. Once you have added this property, the result should appear as shown in Figure 3-23.

Figure 3-23. Buttons placed in the DockPanel without LastChildFill 9. The order in which you place the controls in the DockPanel determines how they are docked with the other controls. For example, notice that button labeled Bottom Button is docked around the left and right button, because they were added earlier in the DockPanel. However, if you add another button to the first button in the DockPanel and dock it to the top, it will occupy the entire width of the control.

57 www.it-ebooks.info CHAPTER 3  LAYOUT MANAGEMENT IN SILVERLIGHT

10. Once you have added this control, the result should appear as shown in Figure 3-24.

Figure 3-24. Buttons placed in the DockPanel with Top Dock

Summary
In this chapter, we explored the five layout controls that are available in Silverlight. We looked at the Canvas, StackPanel, Grid, WrapPanel, and DockPanel controls. In the next chapter, we will take an indepth look at the form controls that come bundled with Silverlight.

58 www.it-ebooks.info CHAPTER 4

Silverlight Controls
For those who have worked with Silverlight 1.0, one of the first observations you most likely made was the lack of common controls such as the Button, TextBox, and ListBox. In fact, Silverlight 1.0 provided only two basic controls: Rectangle and TextBlock. From these, developers were expected to implement all of the rich controls they needed. As you can imagine, it was quite a bit of work to create all of the form controls using just these two base controls. Since then, Microsoft’s vision of Silverlight has gone beyond basic animations to spark up your applications and into the realm of feature-rich user interfaces (UIs). To this end, Silverlight includes a strong base of controls you can use within your Silverlight applications. In this chapter, you will first look at the Silverlight controls in general by examining control properties and events. You will then take a brief tour of some of the more common form controls included in Silverlight. This chapter provides a high-level introduction to these common Silverlight controls. You will continue to work with the controls throughout the remainder of the book, so you will see more specific usage scenarios later.

Setting Control Properties
The most straightforward and simple way to set a property is by using attribute syntax. However, in some cases, you will use element syntax.

Attribute Syntax
Most properties that can be represented as a simple string can be set using attribute syntax. Setting an attribute in XAML is just like setting an attribute in XML. An XML element contains a node and attributes. Silverlight controls are defined in the same way, where the control name is the node and the properties are defined as attributes. As an example, you can easily use attribute syntax to set the Width, Height, and Content properties of a Button control. The following control definition includes a Button element, and Width, Height, and Content attributes and 100, 30, and “Click Me” attribute values:

Element Syntax
Element syntax is most commonly used when a property cannot be set using attribute syntax because the property value cannot be represented as a simple string. Again, this is very similar to using elements in XML. The following is an example of setting the background color of a button:

59 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

Type-Converter-Enabled Attributes
Sometimes when defining a property via an attribute, the value cannot be represented as a simple string—rather, it is converted to a more complex type. A common usage of a type-converter-enabled attribute is Margin. The Margin property can be set as a simple string, such as in the following example: When you set the Margin property in this fashion, the left, right, top, and bottom margins are all set to 15 pixels. What if you want to set the top margin to 15 pixels, but you want the other three margins to be 0? To do that, you set the Margin property as follows: In this case, Silverlight takes the string "0,15,0,0" and converts it into a more complex type. The string is converted to four values: left margin = 0, top margin = 15, right margin = 0, and bottom margin = 0. This type-conversion concept is not new to Silverlight. For those familiar with Cascading Style Sheets (CSS), the same sort of structure exists. As an example, when you are defining a border style, within the simple string value for a border, you are actually setting the thickness, color, and line style. The following border assignment in CSS sets the border thickness to 1 pixel, the line style to be solid, and the color to #333333 (dark gray): border: 1px solid #333333;

Attached Properties
In Chapter 3, you learned how to set a control’s position within a Canvas panel by using attached properties. An attached property is a property that is attached to a parent control. In the example in Chapter 3, you specified the Button control’s position within the Canvas object by setting two attached properties: Canvas.Top and Canvas.Left. These two properties reference the Button control’s parent, which is the Canvas.

60 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

Nesting Controls Within Controls
When you first look at the controls included in Silverlight, you will probably feel pretty comfortable, because they seem to be what would be expected. However, when you dig a bit deeper into the control features, you will find that the controls are much more flexible and powerful than they first appear. One of the key features of controls in Silverlight is the ability to put just about anything within a control. A Button control can contain a StackPanel, which can contain an Ellipse control and a TextBlock control. There really are few limitations as to what the contents of a control can be. Figure 4-1 shows an example of a standard Silverlight Button control containing a StackPanel, a nested StackPanel, an Ellipse, a TextBlock, and a ListBox.

Figure 4-1. A Button control with nested controls The following code was used to produce the control in Figure 4-1:

61 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

As the code shows, the example simply nests additional content within the Button control. As you can imagine, this can be a very powerful feature.

Handling Events in Silverlight
As with other Microsoft programming frameworks, Silverlight provides an event mechanism to track actions that take place within Silverlight applications. Two types of actions are tracked within Silverlight: • Actions that are triggered based on some input from the user. Input actions are handled and “bubbled” up from the browser to the Silverlight object model.

62 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS



Actions that are triggered based on a change of state of a particular object, including the object’s state in the application. These actions are handled directly from the Silverlight object model.

Event handlers are methods that are executed when a given event is triggered. You can define event handlers either in the XAML markup itself or in managed code. The following exercises demonstrate how to define event handlers in both ways.

Try It Out: Declaring an Event in XAML
Let’s get started by defining event handlers within the XAML markup: 1. Open Visual Studio 2010, and create a new Silverlight project called EventHandlers0000000000000000000000000000000000000000000. Allow Visual Studio to create a Web Application project to host the application. When the project is created, you should be looking at the MainPage.xaml file. If you do not see the XAML source, switch to that view so that you can edit the XAML. Within the root Grid of the Silverlight page, add grid row and column definitions (as explained in Chapter 3) to define four rows and two columns, as follows: 3. Next, add a Button control to the upper-left grid cell and a TextBlock control in the upper-right cell.

2.

63 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

4. Add the Click property to the button. When you type Click=, Visual Studio prompts you with the option of automatically creating a new event handler, as shown in Figure 4-2. When the option is displayed, simply press Enter, and Visual Studio will complete the Click property, as follows:

Figure 4-2. Visual Studio’s automatic creation of an event handler 5. In addition, Visual Studio automatically adds an event handler called Button_Click to the code-behind class for the Silverlight application, as follows:

64 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { } } 6. For this example, you will change the Text property within the TextBlock. To do this, you first need to give the TextBlock a name so that you can access it from the code behind. Add the following code: 7. Now change the Text property of the TextBlock within the Button_Click event, as follows: private void Button_Click(object sender, RoutedEventArgs e) { txtXAMLEventText.Text = "Thank you for clicking!"; } 8. Run the application, and click the XAML Event button. The text to the right of the button changes to “Thank you for clicking!”. Figures 4-3 and 4-4 show the application before and after clicking the XAML Event button.

65 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

Figure 4-3. The TextBlock before the button is clicked 9. Now that you have seen how to define an event handler in the XAML markup, in the next exercise you will continue by adding another event handler using managed code.

Figure 4-4. The TextBlock after the button is clicked

66 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

Try It Out: Declaring an Event Handler in Managed Code
Let’s continue with the project named EventHandlers from the previous exercise. You’ll add another button and wire up its event handler using managed code. 1. Add another Button and TextBlock in the second row of the Grid, as follows: 2. To reference the new Button control in managed code, you must give it and the TextBlock control a name, as shown in the following snippet:

67 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

3. Your page should now appear as shown in Figure 4-5.

Figure 4-5. The updated Silverlight page 4. Next, you need to add the event handler for the Managed Event button Click event. Right-click the Silverlight page and select View Code. This switches to the code behind of the page. From here, you will use the standard CLR language-specific syntax for adding event handlers. Because you are using C#, the syntax is to use the += operator and assign it to a new EventHandler. Visual Studio will help you with this.

5.

68 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

6.

After the InitializeComponent() method call in the Page constructor, start typing "this.btnManaged.Click +=". At this point, Visual Studio displays the message “new RoutedEventHandler(btnManaged_Click); (Press TAB to insert),” as shown in Figure 4-6. Press Tab to complete the event handler definition.

Figure 4-6. Visual Studio assisting with wiring up an event handler in managed code 7. Visual Studio once again prompts you for the name of the event handler. Go ahead and press Tab again to accept the default name. At this point, your source should look like this: public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); this.btnManaged.Click += new RoutedEventHandler(btnManaged_Click); } void btnManaged_Click(object sender, RoutedEventArgs e) { throw new NotImplementedException(); }

69 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

} 8.

private void Button_Click(object sender, RoutedEventArgs e) { txtXAMLEventText.Text = "Thank you for clicking!"; }

Now the only thing left to do is add the code to the event handler. You will notice that, by default, Visual Studio added code to automatically throw a NotImplementedException. Remove that line, and replace it with the following line to change the TextBlock control’s text. void btnManaged_Click(object sender, RoutedEventArgs e) { txtManagedEventText.Text = "Thank you for clicking"; }

9.

Run the application, and click the Managed Event button. You will see the text for the second TextBlock is updated to say “Thank you for clicking”, as shown in Figure 4-7.

Figure 4-7. The result of the managed code event handler This exercise demonstrated how to wire up an event handler using C# and managed code. In the remainder of the chapter, we will take a tour of additional form controls in Silverlight.

70 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

Multiple Mouse Click Support
One of the new features in Silverlight 5 is support for multiple clicks of the mouse, on both the right and left mouse buttons. You can use this feature to add support for double-clicking and triple-clicking (or however many clicks you would like to support). This is done with the addition of a new property on the MouseButtonEventArgs object called ClickCount. This property is now populated with the number of clicks that the user has made. Let’s walk through a quick example to demonstrate this feature.

Try It Out: Multiple Click Support
Let’s get started by defining event handlers within the XAML markup: 1. 2. Create a new Silverlight application in Visual Studio called MultiClick. Allow Visual Studio to create a Web Application project to host the application. In the MainPage.xaml file, add a Rectangle and a TextBlock to the LayoutRoot Grid as shown here: 3. In the code behind for the MainPage.xaml file, add the following code to the event handler created for the MouseLeftButtonDown event. If the event wasn’t automatically added, add it. private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this.ClickCount.Text = "You clicked " + e.ClickCount.ToString() + " times"; } 4. That’s all there is to it. Now you can run the application. When the rectangle displays, double-click on it. You will see the message indicate that the mouse was clicked two times, as shown in Figure 4-8.

71 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

Figure 4-8. Mutliclick support functionality

The Border Control
The Border control provides a way to add a border and background to any one control in Silverlight. Even though a border is applied to only one control, you can always place a border around a StackPanel or Grid and, as a result, include many controls within a border. The syntax to add a Border control to your Silverlight project is very simple, as you can see from the following example:

72 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

Figure 4-9 shows the results.

Figure 4-9. Using the Border control Another feature of the Border control is the ability to round the corners of the border using the CornerRadius property. Here is how the preceding example could be modified to provide a Border control with a CornerRadius property of 10: The border with rounded corners is shown in Figure 4-10. You can declare a background color for your border using the Background property. Like the BorderBrush property, the Background property can be set to either a color or a brush type. Here is an example of setting a border with a background color of silver:

73 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

Figure 4-10. Border control with a CornerRadius property of 10 Figure 4-11 shows the result of adding the background color.

74 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

Figure 4-11. Border control with its background set to silver The following is an example of a more complex Border control that contains a gradient for the border and background, which is accomplished by using a Brush object:

75 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

3" /> 4" />

Figure 4-12 shows the border with the gradient applied.

Figure 4-12. Border control with gradient brushes for the border and background

User Input Controls
One of the most common controls in applications is a text box, which is the standard control for collecting basic string input from the user. Also ubiquitous are check boxes and radio buttons, which allow users to select from a list of choices—more than one choice in the case of check boxes, and a single

76 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

choice in the case of radio buttons. Silverlight provides the TextBox, CheckBox, and RadioButton for these standard controls. The following exercises also gives you a chance to work with the Ellipse and Rectangle controls.

Try It Out: Working with the TextBox Control
This exercise demonstrates the use of the TextBox control in Silverlight by creating a simple application that requests the red, green, and blue values to fill an ellipse with a given color. The resulting application appears as shown in Figure 4-13.

Figure 4-13. Sample application using TextBox controls 1. In Visual Studio, create a new Silverlight application named TextBoxControl. Allow Visual Studio to create a Web Application project to host your application. In the MainPage.xaml file, within the root Grid element, add three RowDefinition items, as follows:

2.

77 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

3. Add three TextBox and TextArea controls contained in a horizontal-oriented StackPanel to the first row, a Button control to the second row, and an Ellipse control to the third row. In addition, place a TextBlock in the third row to stack on top of the Ellipse control for error-reporting purposes. Name each of the TextBox controls, as well as the Button control and the TextBlock. These additions are shown in the following code:

78 www.it-ebooks.info 4

CHAPTER 4  SILVERLIGHT CONTROLS

4. Now add the Click event to the Button control. Do this in the code behind, as explained earlier in the section “Try It Out: Declaring an Event in XAML.” public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); this.btnTry.Click += new RoutedEventHandler(btnTry_Click); } void btnTry_Click(object sender, RoutedEventArgs e) { throw new NotImplementedException(); } } 5. When the button is clicked, the application changes the Fill property of the Ellipse control, which expects a SolidColorBrush. You can create the SolidColorBrush using the Colors.FromArgb() method, which accepts four arguments: one for opacity, and one byte each for the red, green, and blue values. You get the red, green, and blue values from the TextBox controls using the Text property. void btnTry_Click(object sender, RoutedEventArgs e) { this.ellipse.Fill = new SolidColorBrush( Color.FromArgb( 255,

79 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

)); } 6.

byte.Parse(this.txtRed.Text), byte.Parse(this.txtGreen.Text), byte.Parse(this.txtBlue.Text)

Because the values for red, green, and blue must be an integer from 0 to 255, you can either validate them using Silverlight validation or take the easy way out and just wrap your code in a try/catch block, and then report the error using the TextBlock. You’ll go with the latter approach here. To keep things clean, you will make sure the error message is cleared if all works correctly. Here is the updated code: void btnTry_Click(object sender, RoutedEventArgs e) { try { this.ellipse.Fill = new SolidColorBrush( Color.FromArgb( 255, byte.Parse(this.txtRed.Text), byte.Parse(this.txtGreen.Text), byte.Parse(this.txtBlue.Text) )); this.lblColor.Text = ""; } catch { this.lblColor.Text = "Error with R,G,B Values"; } }

7.

Build and run the application to see what you get. Type 255, 0, and 0 in the Red, Green, and Blue text boxes, respectively, and then click the Try Color button. You should see the ellipse turn red. Just for the fun of it, leave one of the values blank or enter a value other than 0 through 255 to see the error message.

Now that we have taken a quick look at the TextBox control, let’s turn our attention to two other common controls: CheckBox and RadioButton.

Try It Out: Working with the RadioButton and CheckBox Controls
The following exercise gives you a first look at the RadioButton and CheckBox controls. You will build a simple survey, as shown in Figure 4-14.

80 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

Figure 4-14. Sample application using the RadioButton and CheckBox controls 1. Create a new Silverlight application in Visual Studio, and call it CheckBoxRadioButton. Allow Visual Studio to create a Web Application project to host the application. In the MainPage.xaml file, divide the root Grid into two rows. In each row, place a StackPanel with vertical orientation and a Margin property set to 10.

2.

81 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

3. The top row will be used to demonstrate the use of the RadioButton control, and the bottom row will feature the CheckBox control. Let’s begin with the RadioButton. The RadioButton control allows users to select only one selection out of a number of RadioButton controls that share the same group name. This is set using the RadioButton’s Grouping property. Although you could simply type in each of the color choices for the radio buttons as text using the Content property, I thought it would be less boring to use colored rectangles instead. As we discussed earlier in the section “Nesting Controls Within Controls,” one of the benefits of Silverlight controls is that you can nest just about anything within the different controls. This is just another example of that flexibility. Place five RadioButton controls in the first StackPanel, each with a Rectangle control of a different color. For the group name, use FavoriteColor. To make the content of the RadioButton controls display as left-justified, set the HorizontalAlignment property to Left for each one. Here is the code: 7. Next, do the same for the CheckBox controls in the bottom row, except here, just go the boring route and supply the choices as text. In addition, CheckBox controls are left-justified by default, and they do not need to be grouped. Here is the code for the CheckBox portion:

4.

5.

6.

82 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

8. Go ahead and run the solution to see the result as it will appear in the browser. The output is shown in Figure 4-14 Notice that, as you would expect, you are able to select only one radio button at a time, but you can click as many check boxes as you wish.

Extended Controls
When a Silverlight application is deployed, it goes into an .xap file. This file will need to be downloaded by every client that accesses the Silverlight application. A big benefit of Silverlight is that the size of this .xap file is kept very small. One reason this file can be small is that the most commonly used controls are included in the Silverlight Runtime, which is already present on every machine with Silverlight installed. However, Silverlight provides a number of controls beyond this commonly used set of controls. These controls are included in two separate assemblies: System.Windows.Controls.dll and System.Windows.Controls.Data.dll. These dynamic-link libraries (DLLs) will be included in the application .xap file only if the developer used a control from one of these extended control sets in that application.

Adding an Extended Control
When a developer uses a control from one of the other control libraries, an additional xmlns declaration is added in the UserControl definition. This xmlns has a prefix associated with it that will then be used to reference the individual controls. For example, if you add a DataGrid to your Silverlight application in Visual Studio, your source appears as follows: Notice the additional xmlns declaration pointing to the System.Windows.Controls namespace within the System.Windows.Controls.Data assembly.

83 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

 Tip To view which controls belong to which assemblies, first create a new Silverlight application and add a DataGrid and GridSplitter to the root Grid. Then select View ➤ Object Browser from the Visual Studio main menu. From the Object Browser’s Browse drop-down list (in the top-left corner), select My Solution and browse the listing for three assemblies: System.Windows, System.Windows.Controls.Data, and System.Windows.Controls. Within each of those assemblies, drill down to the System.Windows.Controls namespace to see all of the controls that reside in that assembly.

Now you will work through an exercise using one of the controls in the System.Windows.Controls assembly.

Try It Out: Using the GridSplitter
One of the controls that resides in the System.Windows.Controls assembly is the GridSplitter. This control provides the ability for a user to change the width of a column or row in an application. If used properly, the GridSplitter can greatly improve the appearance of your application, as well as the user experience. In the following exercise, you implement a simple GridSplitter: 1. Create a new Silverlight application in Visual Studio called GridSplitterControl. Allow Visual Studio to create a Web Application project to host the application. In the MainPage.xaml file, divide the root Grid into two columns. The first column should be 150 pixels in width, and the second should take up the remainder of the application. To be able to see what is going on in the grid, set ShowGridLines to True. Also, add two TextBlock controls to the application: one in the first column and one in the second column. Your source should appear as follows: 3. At this point, your Silverlight application should look like Figure 4-15.

2.

84 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

Figure 4-15. The setup for the GridSplitter example 4. Notice that you cannot see all of the text in the second column. Let’s add a GridSplitter control to the application so that users can resize the two columns to be able to view all the text in both columns. Within the XAML, place the cursor just below the TextBlock definitions you added. Then, in the Visual Studio Toolbox, double-click the GridSplitter control. This adds the xmlns to the System.Windows.Controls assembly, and it will also add the GridSplitter to the application. Remove all of the properties set by default except the Name property, and then set the Background property of the GridSplitter to LightGray. The source appears as follows:

5.

85 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

 Tip As discussed earlier in this section, because the GridSplitter belongs in the Silverlight SDK control library instead of the standard controls library, you must add a references to the sdk control namespace and assembly. The entry in the UserControl definition is the following: xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> Because you named the reference “sdk,” that is the prefix used in defining the GridSplitter ().

6.

Run the application. It should look similar to Figure 4-16. Notice that you can now click and drag the GridSplitter to resize the two Grid columns.

Figure 4-16. The completed GridSplitter application

86 www.it-ebooks.info CHAPTER 4  SILVERLIGHT CONTROLS

As you can see, it’s quite easy to gain the rich functionality of a grid splitter in your application with the Silverlight GridSplitter control.

Summary
In this chapter, you took a brief look at some of the common form controls that are provided with Silverlight. The chapter was meant only as an introduction to the controls. You will be looking at the more advanced capacities of these controls in the upcoming chapters. In the next chapter, you will look at the Silverlight list controls: ListBox and DataGrid.

87 www.it-ebooks.info CHAPTER 5

Data Binding and Silverlight List Controls
The previous chapter focused on a few of the form controls contained in Silverlight. In this chapter, you will look at two controls that are made to display lists of data: ListBox and DataGrid. These controls are typically bound to data through a technique known as data binding, which I’ll explore first.

Data Binding
Through data binding, UI elements (called targets) are “bound” to data from a data source (called the source), as illustrated in Figure 5-1. When the data sources change, the UI elements bound to those data sources update automatically to reflect the changes. The data can come from different types of sources, and the target can be just about any UI element, including standard Silverlight controls.

Figure 5-1. Data binding in Silverlight

89 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

Data binding simplifies application development. Because changes are reflected automatically, you do not need to manually update the UI elements. Also, by using data binding, you are able to separate the UI from the data in your application, which allows for a cleaner UI and easier maintenance.

The Binding Class
Data binding in Silverlight is accomplished by using the Binding class. The Binding class has two components—the source and target—and a property that defines the way the two are bound, called the binding mode. The source is the data that is to be bound, the target is a property of the control that the data is to be bound to, and the mode defines how the data is passed between the source and the target (one-way, one-time, or two-way). You’ll see how this works in the upcoming exercise. To define the binding of a control’s property, you use XAML markup extensions, such as {Binding }. For example, to bind the Text property of a TextBox to a data source’s FirstName element, you use the following XAML:

Try It Out: Simple Data Binding in Silverlight
To help explain data binding in Silverlight, let’s build a very simple application. The application will include a Book object that contains two properties: Title and ISBN. These properties will be bound to two TextBox controls. Figure 5-2 shows the end result of the example.

Figure 5-2. Simple data-binding example

90 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

1.

Create a new Silverlight application in Visual Studio 2010. Name the project BasicDataBinding, and allow Visual Studio to create a Web Application project to host your application. Edit the MainPage.xaml file to define two columns and six grid rows. Place a TextBlock in each row in column 1 and a TextBox in each row in column 2. Also, add some margins and some alignment assignments to improve the layout. The code for the page follows:

2.

91 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

3. Next, edit the code behind, MainPage.xaml.cs. Add a Loaded event handler for the application, which will fire when the application is loaded by the client. This is accomplished with the following source code: public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); this.Loaded += new RoutedEventHandler(Page_Loaded); } void Page_Loaded(object sender, RoutedEventArgs e) { } } 4. Now you need to add a class to define a Book object. Below the MainPage class, add the following class definition: namespace BasicDataBinding { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); this.Loaded += new RoutedEventHandler(Page_Loaded); } void Page_Loaded(object sender, RoutedEventArgs e) { } } public class Book { public string Title { get; set; }

92 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

} } 5.

public string ISBN { get; set; }

Now that you have Book defined, you need to create an instance of Book and set it to the LayoutRoot’s DataContext, as follows: void Page_Loaded(object sender, RoutedEventArgs e) { Book b = new Book() { Title = "Beginning Silverlight: From Novice to Professional", ISBN = "978-1430229889" }; this.LayoutRoot.DataContext = b; }

6.

When you set up binding definitions for different controls, the controls do not know where they are going to get their data. The DataContext property sets the data context for a control that is participating in data binding. The DataContext property can be set directly on the control. If a given control does not have a DataContext property specified, it will look to its parent for its data context. The nice thing about this model is that if you look in the preceding XAML for the page, you will see little indication of where the controls are getting their data. This provides an extreme level of code separation, allowing designers to design XAML UIs and developers to work alongside the designers, defining the specifics of how the controls are bound to their data sources. At this point, you can go ahead and start debugging the application. If all goes well, you will see the four text boxes populated with the data from the Book’s instance. (See Figure 5-2.) With the application running, change the book title in the first text box to just “Beginning Silverlight,” by removing the “From Novice to Professional.” You might expect that, because the third text box is bound to the same data, it will automatically update to reflect this change. However, a couple of things need to be done to get this type of two-way binding to work.

7.

8. 9.

10. One problem is that, currently, the Book class does not support notifying bound clients of changes to its properties. In other words, when a property changes in Book, the class will not notify the TextBox instances that are bound to the class of the change. You could take care of this by creating a change event for each property. This is far from ideal; fortunately, there is an interface that a class can implement that handles this for you. This interface is known as INotifyPropertyChanged. Let’s use it. 11. Modify the Book class definition to inherit from INotifyPropertyChanged. Notice that when you inherit from INotifyPropertyChanged, you need to add using System.ComponentModel. Luckily, Visual Studio will help you with this, as shown in Figure 5-3.

93 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

Figure 5-3. Visual Studio assists when you need to add the System.ComponentModel namespace. 12. Next, you can let Visual Studio do some more work for you. After adding the using System.ComponentModel statement, right-click INotifyPropertyChanged and select Implement Interface ➤ Implement Interface from the pop-up menu, as shown in Figure 5-4.

94 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

Figure 5-4. Visual Studio also assists in implementing the INotifiyPropertyChanged interface. 13. Now Visual Studio has added a new public event to your class: public class Book : INotifyPropertyChanged { public string Title { get; set; } public string ISBN { get; set; } public event PropertyChangedEventHandler PropertyChanged; } 14. Next, you need to create a convenience method that will fire the PropertyChanged event. Call it FirePropertyChanged, as shown in the following code: public class Book : INotifyPropertyChanged { public string Title { get; set; } public string ISBN { get; set; } public event PropertyChangedEventHandler PropertyChanged; void FirePropertyChanged(string property) { if (PropertyChanged != null)

95 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

{ }

PropertyChanged(this, new PropertyChangedEventArgs(property));

} }

15. Now you need to extend the simplified properties by adding private members and full get/set definitions to define the get and set operations, as shown in the following code. The get is just like a normal get operation, where you simply return the internal member value. For the set, you first set the internal member value, and then call the FirePropertyChanged method, passing it the name of the property. public class Book : INotifyPropertyChanged { private string _title; private string _isbn; public string Title { get { return _title; } set { _title = value; FirePropertyChanged("Title"); } } public string ISBN { get { return _isbn; } set { _isbn = value; FirePropertyChanged("ISBN"); } } public event PropertyChangedEventHandler PropertyChanged;

96 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

void FirePropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); }

} }

16. With this completed, your class is set up to notify bound clients of changes to the Title and ISBN properties. But you still need to take one more step. By default, when you bind a source to a target, the BindingMode is set to OneWay binding, which means that the source will send the data to the target, but the target will not send data changes back to the source. To get the target to update the source, you need to implement two-way (TwoWay) binding.

■ Note Earlier, I mentioned that there are three options for BindingMode. The third option is OneTime binding. In this mode, the values are sent to the target control property when the object is set to the DataContext. However, the values of the target property are not updated when the source value changes.

17. To change to two-way binding, add the Mode=TwoWay parameter when defining the {Binding} on a control, as follows: 18. Rebuild and run your application. Update any of the fields, and leave the focus on the control. You’ll see that the two-way binding is triggered, and the corresponding field is also updated, as shown in Figure 5-5.

Figure 5-5. Two-way binding in action Congratulations! You have just created a Silverlight application that allows for two-way data binding. We will now move on to look at data binding lists of data to the two list controls provided in Silverlight: DataGrid and ListBox.

98 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

Element-to-Element Binding
In addition to binding elements to data, you can bind them directly to other elements, which can significantly improve the readability and efficiency of your code. The syntax for binding to an element is very similar to binding to a data item—the only difference is that in the binding an ElementName is specified, which is very much like setting the ItemsSource to the Element. As an example, if you wanted to bind the IsEnabled property of a control to a check box’s IsChecked property, assuming the check box is named EnableButton, the binding syntax would be the following: IsEnabled="{Binding IsChecked, Mode=OneWay, ElementName=EnableButton}" Notice that the binding is the same as it would be when binding to a data source, except that we have added the ElementName=EnableButton. Let’s try this out in an exercise.

Try It Out: Element-to-Element Binding
To help explain element-to-element binding in Silverlight, let’s build a simple application. The application will include a button and a check box. When the check box is selected, the button is enabled, when the check box is deselected, the button is disabled. Let’s get started. 1. Create a new Silverlight application in Visual Studio 2010. Name the project ElementBinding, and allow Visual Studio to create a Web Application project to host your application. Edit the MainPage.xaml file to add a StackPanel to the root Grid. Place a ToggleButton and CheckBox named EnableButton within that StackPanel so that the ToggleButton appears above the CheckBox. Add a margin of 20 on the StackPanel and 5 on the ToggleButton and CheckBox to add some spacing between the controls. The code for the page follows: 3. Next, you need to bind the ToggleButton’s IsEnabled property to the CheckBox’s IsChecked property. You do this with one-way binding as described earlier in this chapter. Set the ElementName to EnableButton, which is the name you gave to your CheckBox. The updated source code should now look like the following: 4. That is it! No coding is required for this demo. Run the sample, and you will see that the ToggleButton is enabled, as shown in Figure 5-6.

Figure 5-6. Element binding example with ToggleButton enabled 5. Now deselect the Enable Button check box, and you will see that the ToggleButton is no longer enabled, as shown in Figure 5-7.

100 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

Figure 5-7. Element binding example with ToggleButton disabled

The DataGrid Control
The data grid type of control has been around for ages and has been the primary choice for developers who need to display large amounts of data. The DataGrid control provided by Silverlight is not just a standard data grid, however. It contains a great deal of rich user functionality that, in the past, has been present only in third-party data grid components. For example, the Silverlight DataGrid handles resizing and reordering of grid columns. Figure 5-8 shows an example of a simple DataGrid, where the columns were automatically generated. Notice how the column titled Male is a check box. The DataGrid control has built-in intelligence to automatically show Boolean data types as check-box cells.

101 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

Figure 5-8. A simple DataGrid example

Try It Out: Building a Simple DataGrid
Let’s run through a simple DataGrid example: 1. Create a new Silverlight application in Visual Studio 2010. Name the project SimpleDataGrid, and have Visual Studio create a hosting web application for you. Add the DataGrid to your application. To do this, simply add the DataGrid to the root Grid in your XAML, and set the Margin property to 10 to get some spacing around the grid. In addition, give the DataGrid the name grid. Note that, by default, the Grid’s AutoGenerateColumns property is set to true. If you were going to define the columns manually, you would want to set this property to false. However, because you want the grid to create the columns automatically, you can simply omit the property. The DataGrid definition follows:

2.

102 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

 Note Why use ? As discussed in Chapter 4, the DataGrid is contained in an assembly called
System.Windows.Controls.Data, which is not added to Silverlight applications by default. This way, if your

application does not need any of the extended controls, the file size of your Silverlight application can be smaller. However, to add a DataGrid to your application, you need to reference the new assembly and add an xmlns reference to the assembly in the UserControl definition: xmlns:sdk=http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk As you might expect by now, Visual Studio can do all the work for you. To use this functionality in Visual Studio, drag the DataGrid control from the Toolbox to add it to your application (or simply double-click on the DataGrid in the Toolbox). Visual Studio will add a new xmlns reference in the UserControl at the top of the .xaml page called data.

3.

Next, build the class that will be bound to the DataGrid. Call the class GridData for simplicity, and give it three properties: Name (string), Age (int), and Male (Boolean). Also for simplicity, create a static method that will return an ObservableCollection (which requires adding a using clause for System.Collections.ObjectModel) containing some sample data that will be bound to the grid. In addition, define the class directly in the MainPage.xaml.cs file. This is not really a good idea in the real world, but for the sake of an example, it will work just fine. Ideally, you want to define your classes in separate files or even in completely separate projects and assemblies. The code for the GridData class follows: namespace SimpleDataGrid { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } } public class GridData { public string Name { get; set; } public int Age { get; set; } public bool Male { get; set; } public static ObservableCollection GetData() { ObservableCollection data = new ObservableCollection();

103 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

data.Add(new GridData() { Name = "John Doe", Age = 30, Male = true }); data.Add(new GridData() { Name = "Jane Doe", Age = 32, Male = false }); data.Add(new GridData() { Name = "Jason Smith", Age = 54, Male = true }); data.Add(new GridData() { Name = "Kayli Jayne", Age = 25, Male = false }); } } } return data;

 Note When you are binding a collection of data to a DataGrid or ListBox, you might be tempted to use the List generic class. The problem with using the List class is that it does not have built-in change notifications for the collection. To bind a DataGrid and ListBox to dynamic data that will be updated, you should use the ObservableCollection generic class. The ObservableCollection class represents a collection of dynamic data that provides built-in notification when items in the collection are added, removed, or refreshed.

4.

Now that you have the XAML and the class defined, you can wire them up. To do this, first create an event handler for the Loaded event of the page, as follows: public partial class MainPage : UserControl { public MainPage()

104 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

{ }

InitializeComponent(); this.Loaded += new RoutedEventHandler(Page_Loaded);

void Page_Loaded(object sender, RoutedEventArgs e) { } } 5. When the page is loaded, you want to call GetData() from the GridData class and bind that to the DataGrid’s ItemsSource property, as follows: public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); this.Loaded += new RoutedEventHandler(Page_Loaded); } void Page_Loaded(object sender, RoutedEventArgs e) { this.grid.ItemsSource = GridData.GetData(); }

} 6.

Build and run the application. If all is well, you should see the DataGrid displayed. (See Figure 5-8.)

Let’s take a few moments and play around with this DataGrid to explore some of its features. First of all, if you click any of the column headers, you will notice that sorting is automatically available, as shown in Figure 5-9. Next, if you place your cursor at the edge of one of the columns, you can use the mouse to click and drag the column’s edge to resize the column, as shown in Figure 5-10. Again, this functionality is provided for free with the DataGrid’s rich client-side functionality. Finally, if you click and hold the mouse on one of the column headers, and then drag it left or right to another column header’s edge, you will see the column header move and a dark gray border appear between the columns. For instance, click and drag the Male column to the left of the Age column, as shown in Figure 5-11. When a dark-gray border shows up between the two columns, release the mouse, and you will see that the Male column now appears to the left of the Age column in the DataGrid.

105 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

Figure 5-9. Sorting in the DataGrid

Figure 5-10. Resizing columns in a DataGrid

106 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

Figure 5-11. Column reordering in action You’ll agree that this is pretty nice out-of-the-box functionality for simply defining a DataGrid with this code: Now that you have implemented a simple DataGrid example, let’s explore some of the additional options available.

The Columns Collection
In the previous example, you allowed the DataGrid to automatically generate columns based on the data to which it was bound. This is not a new concept—it has been around in data grid components since the initial release of ASP.NET. But what if you want to have some additional control over the columns that are created in your DataGrid? What if you want to add a column that contains some more complex information, such as an image? You can do this by first setting the AutoGenerateColumns property on the grid to false. Then you need to generate the columns manually. Columns are defined in a DataGrid using the Columns collection. The following is an example of setting the Columns collection in XAML. Notice that it sets the AutogenerateColumns property to False. If you neglect to do this, you will get all of the autogenerated columns in addition to the columns you define within the Columns collection.

107 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

You can place three types of columns within a Columns collection: a text column (DataGridTextColumn), a check-box column (DataGridCheckBoxColumn), and a template column (DataGridTemplateColumn). All of the column types inherit from type DataGridColumn. A number of notable properties apply to all three column types, as shown in Table 5-1. Table 5-1. DataGridColumn Properties

Property
CanUserReorder CanUserResize DisplayIndex Header IsReadOnly MaxWidth MinWidth Visibility Width

Description
Turns on and off the ability for the user to drag columns to reorder them Turns on or off the ability for the user to resize the column’s width with the mouse Determines the order in which the column appears in the DataGrid Defines the content of the column’s header Determines if the column can be edited by the user Sets the maximum column width in pixels Sets the minimum column width in pixels Determines whether or not the column will be visible to the user Sets the width of the column, or can be set to automatic sizing mode

DataGridTextColumn
The DataGridTextColumn defines a column in your grid for plain text. This is the equivalent to BoundColumn in the ASP.NET DataGrid. The primary properties that can be set for a DataGridTextColumn are the Header, which defines the text that will be displayed in the columns header, and the DisplayMemberBinding property, which defines the property in the data source bound to the column. The following example defines a text column with the header Name and is bound to the data source’s Name property:

108 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

DataGridCheckBoxColumn
As you would expect, the DataGridCheckBoxColumn contains a check box. If you have data that you want to display as a check box in your grid, this is the control to use. Here is an example of the DataGridCheckBoxColumn that contains the header Male? and is bound to the data source’s Male property:

DataGridTemplateColumn
If you want data in your grid column that is not plain text and is not a check box, the DataGridTemplateColumn provides a way for you to define the content for your column. The DataGridTemplateColumn contains a CellTemplate and CellEditingTemplate, which determine what content is displayed, depending on whether the grid is in normal view mode or in editing mode. Note that while you get features such as automatic sorting in the other types of DataGrid columns, that is not true of the DataGridTemplateColumn. These columns will need to have additional logic in place to allow for sorting. Let’s consider an example that has two fields: FirstName and LastName. Suppose that when you are in normal view mode, you want the data to be displayed side by side in TextBlock controls. However, when the user is editing the column, you want to display two TextBox controls that allow the user to edit the FirstName and LastName columns independently. Now that we have covered the basics of manually defining the grids in a DataGrid, let’s try it out.

109 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

Try It Out: Building a DataGrid with Custom Columns
I thought it would be fun to build a DataGrid that contains a list of starting hands in poker. If you have ever watched poker on TV, you most likely heard the players refer to things like “pocket rockets” and “cowboys.” These are simply nicknames they have given to starting hands. The DataGrid you are going to build in this example will look like Figure 5-12.

Figure 5-12. DataGrid with custom columns 1. 2. Create a new Silverlight application called DataGridCustomColumns. Allow Visual Studio to create a web site project to host the application. After the project is loaded, right-click the DataGridCustomColumns project and select Add New Item. In the Add New Item dialog box, select Code File for the template, and name the class StartingHands.cs, as shown in Figure 5-13. Click the Add button to add the class to the project.

110 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

Figure 5-13. Adding a new class to the Silverlight project 3. Now define the StartingHands class. The class will contain four properties: Nickname (string), Notes (string), Card1 (string), and Card2 (string). Also, create a static method in the class called GetHands(), which returns an ObservableCollection of StartingHands instances. The code follows: using using using using using using using using using using using System; System.Net; System.Windows; System.Windows.Controls; System.Windows.Documents; System.Windows.Ink; System.Windows.Input; System.Windows.Media; System.Windows.Media.Animation; System.Windows.Shapes; System.Collections.ObjectModel;

namespace DataGridCustomColumns { public class StartingHands

111 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

{

public public public public

string string string string

Nickname { get; set; } Notes { get; set; } Card1 { get; set; } Card2 { get; set; }

public static ObservableCollection GetHands() { ObservableCollection hands = new ObservableCollection(); hands.Add( new StartingHands() { Nickname = "Big Slick", Notes = "Also referred to as Anna Kournikova.", Card1 = "As", Card2 = "Ks" }); hands.Add( new StartingHands() { Nickname = "Pocket Rockets", Notes = "Also referred to as Bullets.", Card1 = "As", Card2 = "Ad" }); hands.Add( new StartingHands() { Nickname = "Blackjack", Notes = "The casino game blackjack.", Card1 = "As", Card2 = "Js" }); hands.Add( new StartingHands() { Nickname = "Cowboys", Notes = "Also referred to as King Kong", Card1 = "Ks", Card2 = "Kd" }); hands.Add( new StartingHands() { Nickname = "Doyle Brunson",

112 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

});

Notes = "Named after poker great Doyle Brunson", Card1 = "Ts", Card2 = "2s"

} } 4. }

return hands;

Now that the class is built, in the MainPage.xaml file, add a DataGrid named grdData to the root Grid by double-clicking the DataGrid control in the Toolbox. Add a 15-pixel margin around the DataGrid for some spacing, and set the AutoGenerateColumns property to False. The code follows:

5.

Next, define the columns in the DataGrid. To do this, add the DataGrid.Columns collection, as follows:

6.

Referring back to Figure 5-12, the first column in the Grid contains the two cards in the hand. To build this, you use a DataGridTemplateColumn. Within the DataGridTemplateColumn, add a CellTemplate containing a Grid with two columns, each containing a Border, Rectangle, and TextBlock, which will overlap each other. Bind the two TextBlock controls to the Card1 and Card2 properties from the data source. Enter the following code: 7. Again, referring back to Figure 5-12, the next two columns contain the nickname of the starting hand and notes about the starting hand. To implement this, use two DataGridTextColumn columns. Set the Headers of the columns to Nickname and Notes accordingly:

114 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

8. Finally, wire up the controls to the data source. To do this, navigate to the MainPage.xaml.cs file and add an event handler to the Page Loaded event. Within that Loaded event, simply set the DataGrid’s ItemsSource property equal to the return value of the StartingHands.GetHands() static method. Here’s the code: public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); this.Loaded += new RoutedEventHandler(Page_Loaded); } void Page_Loaded(object sender, RoutedEventArgs e) { this.grdData.ItemsSource = StartingHands.GetHands(); } } 9. Compile and run your application. If all goes well, your application should appear, as shown earlier in Figure 5-12.

This completes our DataGrid with custom columns example. Naturally, in a real-world application, you would be getting the data for these hands from an external data source, such as a web service or an XML file. We will be looking at that in Chapter 6. Now, let’s take a look at the ListBox control.

The ListBox Control
In the past, the list-box type of control has been considered one of the common controls in programming—no more special than a drop-down list. However, in Silverlight, this has all changed. The ListBox is perhaps one of the most flexible controls used to display lists of data. In fact, referring back to ASP.NET controls, the Silverlight ListBox is more a cousin to the DataList control than the ASP.NET ListBox control. Let’s take a peek at this powerful control.

Default and Custom ListBox Items
If you wire up the ListBox to your Person data from the earlier DataGrid example, you will see that, by default, the ListBox really is just a standard ListBox:

115 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

One additional property you might have noticed in this ListBox definition is DisplayMemberPath. If you are defining a simple text-based ListBox, the ListBox needs to know which data member to display. Because the Person class contains three properties (Name, Age, and Male), you need to tell it that you want the Name to be displayed. Figure 5-14 shows the results.

Figure 5-14. A simple default ListBox However, the ListBox control can contain much more than plain text. In fact, if you define a custom ItemTemplate for the ListBox, you can present the items in a more interesting way. Here’s an example using the same Person data:

116 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

Figure 5-15 shows how this custom ListBox appears in a browser.

Figure 5-15. A customized ListBox example

Try It Out: Building a ListBox with Custom Content
Let’s take the same data that displayed poker starting hands from the previous exercise and see what type of cool ListBox you can build with it. Figure 5-16 shows the custom ListBox you’ll create in this exercise.

117 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

Figure 5-16. The customized ListBox application 1. 2. Start out by creating a new Silverlight application called ListBoxCustom, and allow Visual Studio to create a hosting web site. You will use the same class that you built in the earlier DataGrid exercise. Right-click the Silverlight project, choose Add Existing Item, and browse to StartingHands.cs to add that class to the project. When you add the existing StartingHands.cs class, it is in a different namespace than your current project. You can reference that namespace by adding a using statement at the top of your Silverlight application, or you can just change the namespace, as follows: namespace ListBoxCustom { public class StartingHands { public string Nickname { get; set; } public string Notes { get; set; } public string Card1 { get; set; } public string Card2 { get; set; } ... } }

3.

118 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

4.

Next, you need to define the ListBox’s ItemTemplate. The ItemTemplate will contain a horizontal-oriented StackPanel including the grid to display the two cards. It will also include a nested vertical-oriented StackPanel that will contain two TextBlock controls to display the Nickname and Notes data. Here is the code:

5.

The only thing left to do is to wire up the ListBox to the data source. To do this, navigate to the MainPage.xaml.cs code behind, and add an event handler for

119 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

the Page Loaded event. Then, within that Loaded event handler, add the following code to set the ListBox’s ItemsSource to the return value from the StartingHands.GetHands() method, as you did earlier in the DataGrid example: public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); this.Loaded += new RoutedEventHandler(Page_Loaded); } void Page_Loaded(object sender, RoutedEventArgs e) { list.ItemsSource = StartingHands.GetHands(); }

} 6.

Run the application. If all goes well, you will see the ListBox shown in Figure 5-16.

As you can see, the ListBox control’s flexibility lets developers display lists of data in some very cool ways.

Data Binding and String Formatting
To simplify the process of creating bindings that require formatting, Silverlight allows you to format data directly in XAML. Adding string formatting is as simple as adding a StringFormat extension in the XAML markup of the data binding. The StringFormat extension supports the same formatting options as the String’s Format method. Consider the following XAML. There are four text boxes displayed, all bound to the same property in code behind. The difference, however, is that each TextBox change displays the data differently based on the binding’s StringFormat extension. The first TextBox shows the raw data, the second TextBox formats the data to three decimal places, the third TextBox shows the value in scientific notation, and the fourth TextBox shows the data as currency. The result is shown in Figure 5-17.

Figure 5-17. Using DataBinding with StringFormat As you can see from this example, you can easily change the format of data directly through the XAML.

Implicit Data Templates
In the earlier ListBox example, you created a DataTemplate to format poker starting hands. This was pretty straightforward because all of the objects contained in the ListBox were the same object. What if you had an instance where you had a collection of different objects, each requiring a different DataTemplate. This would become quite a bit of additional work. Implicit templates are data templates

121 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

that are associated with a specific data type. When the template is within the scope of a control that is trying to display the template’s data type, it will be rendered according to the implicit template. To see implicit templates in action, let’s take a look at an example.

Try It Out: Working with Implicit Data Templates
In this example, you will create a Silverlight application that will utilize implicit data templates. The application will contain a ListBox that displays different types of contacts, each of which will be displayed slightly differently. 1. Open Visual Studio 2010, and create a new Silverlight project called ImplicitTemplates. Allow Visual Studio to create a Web Application project to host the application. When the project is created, you should be looking at the MainPage.xaml file. If you do not see the XAML source, switch to that view so that you can edit the XAML. Within the root Grid of the Silverlight page, add a ListBox named ContactList. 3. Next you need to add the classes that will contain the data you will bind to your ListBox. To demonstrate the implicit data templates, you will display contact information for a company or for a person. To store this data, you will have a Company class and a Person class, both of which will inherit from the base Contact class. To add this class to the project, right-click on the Silverlight application project and select Add Class. When the Add New Item dialog appears, be sure that Class is selected and type in Contact.cs for the name of the class. Click Add to add the class to the project. Add the following class definitions to the class: using using using using using using using using using using using System; System.Net; System.Windows; System.Windows.Controls; System.Windows.Documents; System.Windows.Ink; System.Windows.Input; System.Windows.Media; System.Windows.Media.Animation; System.Windows.Shapes; System.Collections.Generic;

2.

4.

namespace ImplicitTemplates { public class Contact

122 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

{ }

public string PhoneNumber { get; set; }

public class Person : Contact { public string FirstName { get; set; } public string LastName { get; set; } } public class Company : Contact { public string Name { get; set; } }

} 5.

Next you need to create some sample data. To do this, you will add a new class called Contacts that will contain a single static method called GetContacts() that will return a List of contacts. Add the Contacts class to the Contact.cs file: using using using using using using using using using using using System; System.Net; System.Windows; System.Windows.Controls; System.Windows.Documents; System.Windows.Ink; System.Windows.Input; System.Windows.Media; System.Windows.Media.Animation; System.Windows.Shapes; System.Collections.Generic;

namespace ImplicitTemplates { public class Contact { public string PhoneNumber { get; set; } } public class Person : Contact { public string FirstName { get; set; } public string LastName { get; set; } } public class Company : Contact { public string Name { get; set; } } public class Contacts {

123 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

public static List GetContacts() { var contactList = new List() { new Company() { PhoneNumber = "123-456-7890", Name = "Company One" }, new Person() { PhoneNumber = "111-111-1111", FirstName="John", LastName="Doe" }, new Person() { PhoneNumber = "222-222-2222", FirstName="Jane", LastName="Doe" }, new Company() { PhoneNumber = "333-333-3333", Name = "Company Two" } }; } } } 6. Next you need to bind the contact data to the ListBox. In the code behind of the MainPage user control, add the following code that will set the ItemsSource of the ListBox to your GetContacts() method: namespace ImplicitTemplates { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { this.ContactList.ItemsSource = Contacts.GetContacts(); } } 7. } return contactList;

You can now press F5 to test the application. You will see the ListBox displays the type names of your different objects as shown in Figure 5-18.

124 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

Figure 5-18. ListBox with multiple object types 8. Now let’s implement the implicit data templates. In the MainPage.xaml file, you will first add a reference to your ImplicitTemplates namespace. You will give the reference the name model: 9. Now that you have the namespace referenced, you can add your implicit data templates. There are a number of places that you can define these templates, and as long as the control that needs them is in the scope of the templates, they will be used. In this case, you will define the templates at the UserControl level. To do this, add the UserContol.Resources tag above the root grid in the MainPage.xaml file. Then add the following template definitions. The result should be as shown next. Notice that you are using the namespace name

125 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

model as you define it in your namespace reference in step 8. 10. If you run the application again, you will see that the ListBox now uses the implicit templates to display the different items as shown in Figure 5-19.

Figure 5-19. ListBox with implicit templates

126 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

11. Now that you have the implicit templates working, let’s modify them to make the display more practical. Update the templates as shown here: 12. You are finished! If you run the application, you will see the finished application as shown in Figure 5-20.

127 www.it-ebooks.info CHAPTER 5  DATA BINDING AND SILVERLIGHT LIST CONTROLS

Figure 5-20. ListBox with finished implicit templates

Summary
In this chapter, you looked at how to bind lists of data to Silverlight controls. Then you focused on two controls typically bound to data: the DataGrid control and the ListBox control. You saw how these controls are flexible and can show data in unique ways. However, in all of these examples, the classes contained static data. In real-world examples, the data that you will normally list in a DataGrid or ListBox will come from some external data source, such as an XML file or a web service. In the next chapter, you will look at how to get data from these external data sources and how to use that data to bind to your Silverlight applications.

128 www.it-ebooks.info CHAPTER 6

Silverlight Toolkit
When the first version of Silverlight was released, it contained a whopping two controls: the TextBlock and the Rectangle. Silverlight developers had to build all other controls from these two. Luckily, each new version of Silverlight added a number of controls, to the point that the control set included out of the box in Silverlight is now quite impressive. However, a number of commonly used controls are still not included in Silverlight. One example of a control missing from the base Silverlight set is a tree-view control. Developers who want to create an application using a tree view are faced with a choice. They can build a tree-view control themselves, purchase a third-party control, or find a free or open-source tree-view control. Although all three options are completely valid, in this chapter we’ll focus on the last one—and the place we’ll find those controls is the Silverlight Toolkit. The Silverlight Toolkit is an open-source project containing a collection of Silverlight controls, components, and utilities. The toolkit contains full open-source code, unit tests, samples, and documentation. The latest toolkit, released at the time of the Silverlight 5 release, contains more than 30 Silverlight controls, including a number of themes and charting controls. In this chapter, we’ll look at a few of the different components of the Silverlight Toolkit in detail. Let’s get started!

Overview of the Silverlight Toolkit
Because the components in the Silverlight Toolkit are released at different times, they are at different levels of maturity. The components are divided into a number of quality bands that define the maturity levels of the different components.

Mature/SDK Quality Band
Controls that have reached a mature quality level are included in the Mature quality band. These controls are not only considered extremely stable, they are shipped in the Silverlight software development kit (SDK). As controls reach this quality level, they are added to the Silverlight SDK. At that point, they are still considered part of the Silverlight Toolkit, but they will be installed on workstations when developers install the Silverlight SDK. At the time of the Silverlight 5 release, the following ten controls were included in the mature quality band: • • • AutoCompleteBox Calendar ChildWindow

129 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

• • • • • • •

DataGrid DataPager DatePicker GridSplitter HeaderedItemsControl TabControl TreeView

Stable Quality Band
Stable controls that have not yet reached the Mature quality level are placed in the Stable quality band. This band includes the following controls: • • • • • • • DockPanel Expander HeaderedContentControl Label NumericUpDown Viewbox WrapPanel

Preview Quality Band
Controls that have not yet had enough testing to reach a stable quality belong to the Preview quality band. The following controls (as well as 11 themes) belong in this band: • • • • • • • • • • Accordion Charting ContextMenu DataForm DomainUpDown ImplicitStyleManager LayoutTransformer Rating TimePicker TimeUpDown

130 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT



11 Silverlight Themes

Experimental Quality Band
New additions to the Silverlight Toolkit get placed in the Experimental quality band. These are components that should be used with caution because they are still under development and have not been tested to the point to reach even a Preview quality level. Controls and components in this band include the following: • • • • • GlobalCalendar TransitioningContentControl TreeMap Drag-and-drop support for items controls BusyIndicator

Installing the Toolkit
You’ll find the toolkit on CodePlex at http://silverlight.codeplex.com. (See Figure 6-1.)

131 www.it-ebooks.info f

CHAPTER 6  SILVERLIGHT TOOLKIT

Figure 6-1. The Silverlight 5 Toolkit web site You can get the latest toolkit by clicking on the Downloads tab and selecting either the MSI install or a zip containing the source code. For the purpose of this book, just download and install the MSI; the content in this book is based on the December 2011 release of the Silverlight 5 toolkit as shown in Figure 6-2.

132 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

Figure 6-2. The MSI install for the Silverlight 5 Toolkit

Toolkit Controls
The Silverlight Toolkit contains a number of controls. We’ve discussed some of these earlier in the book—DataGrid (Chapter 5), GridSplitter (Chapter 4), and WrapPanel (Chapter 3). In this section, we’ll look at a few others.

Accordion
The Accordion control lets you include a collection of expandable and collapsible panels that allow you to show groups of content. Each accordion contains a header item and a content item. When the user clicks on the header item, it either expands or collapses the section, showing or hiding its content items. Accordion-like controls are used in many common applications, but probably the one that’s most familiar to developers is Visual Studio’s Toolbox, as shown in Figure 6-3. Each tab in the toolbox can be considered a header item, and the controls contained under each header can be considered the content items.

133 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

Figure 6-3. The Visual Studio Toolbox

Try It Out: Working with the Accordion Control
In this example, you will use the Accordion control to display a list of books, grouped by category. Figure 6-4 shows the result you’ll be working toward.

134 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

Figure 6-4. The finished Accordion example 1. Create a new Silverlight application in Visual Studio 2010 called AccordionControl. Allow Visual Studio to create a Web Application project to host the application. With the MainPage.xaml file selected, position the cursor in the source in the Layout Grid. Find and double-click on the Accordion control in the Toolbox. This will add the control to the page, as well as the proper namespace reference: xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" 3. After you’ve added the Accordion, right-click on the control in the design view and select Reset Layout ➤ All, as shown in Figure 6-5. Then Name the Accordion BookList, set its Width to 200, and specify a Margin of 10.

2.

135 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

Figure 6-5. Resetting the layout of the Accordion control 4. Switch to the code behind in the file MainPage.xaml.cs. You need to define the data you’ll be binding to the Accordion. For simplicity, define the data right in the code-behind file. Add two classes, one for Categories and one for Books. namespace AccordionControl { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } } public class BookCategory { public string CategoryName { get; set; } public List Books { get; set; } }

136 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

public class Book { public string Title { get; set; } } } 5. Next you need to populate the classes with some data. To do this, first wire up the Loaded event and insert the following code: public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); this.Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { List Library = new List(); BookCategory cat1 = new BookCategory() { CategoryName = "Silverlight", Books = new List() }; cat1.Books.Add(new Book() { Title = "Beginning Silverlight 5" }); cat1.Books.Add(new Book() { Title = "Pro Silverlight 5" }); Library.Add(cat1); BookCategory cat2 = new BookCategory() { CategoryName = "ASP.NET", Books = new List() }; cat2.Books.Add(new Book() { Title = "Pro ASP.NET 5" }) ; Library.Add(cat2);

} }

public class BookCategory { public string CategoryName { get; set; } public List Books { get; set; } } public class Book { public string Title { get; set; } } 6. Now you need to define the header and content items, using the ItemTemplate for the header and the ContentTemplate for the content. For the ItemTemplate, you’ll simply define a TextBlock that will display the BookCategory. For the ContentTemplate, define a ListBox control that will contain a list of TextBlocks,

137 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

each displaying a book Title. Switch back to the XAML view in Visual Studio, and add the following code: 7. Next you need to bind the Library data source to the Accordion control: void MainPage_Loaded(object sender, RoutedEventArgs e) { List Library = new List(); BookCategory cat1 = new BookCategory() { CategoryName = "Silverlight", Books = new List() }; cat1.Books.Add(new Book() { Title = "Beginning Silverlight 4" }); cat1.Books.Add(new Book() { Title = "Pro Silverlight 4" }); Library.Add(cat1); BookCategory cat2 = new BookCategory() { CategoryName = "ASP.NET", Books = new List() }; cat2.Books.Add(new Book() { Title = "Pro ASP.NET 4" }) ; Library.Add(cat2); this.BookList.ItemsSource = Library; } 8. Press F5 to run the solution. If things go as planned, you should see the Accordion displayed. If you click on ASP.NET, the Silverlight section will collapse and hide those books, while the ASP.NET section will expand and display its books, as shown in Figure 6-6.

138 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

Figure 6-6. Changing headers in the Accordion example

AutoCompleteBox
The AutoCompleteBox is another mature-band toolkit control that is now available through the Silverlight SDK. Its functionality is nothing new to users, because autocomplete text boxes have been around for many years. As you start typing in a text box, items that match what you are typing are displayed below. You can pick an item from the list instead of having to finish typing it yourself. Because the AutoCompleteBox is contained in the SDK, a reference to the SDK namespace is required to access the control: xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" To define an AutoCompleteBox in XAML is no different from defining other controls, such as Buttons: In the code behind, you can then easily add the items that are displayed when the user types by binding a collection to the ItemsSource property. For example, you can bind to a simple string array containing colors.

139 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

public MainPage() { InitializeComponent(); this.FavoriteColor.ItemsSource = new string[] { "aqua", "azure", "beige", "black", "blue", "brown", "cyan", "gold", "gray", "ivory", "lime", "magenta", "maroon", "navy", "olive", "orange", "pink", "purple", "red", "tan", "teal", "violet", "wheat", "white", "yellow" }; } When this control is displayed and a user starts to type in the text box, the colors matching the typed text are displayed below in a list, as shown in Figure 6-7.

Figure 6-7. An AutoCompleteBox You might have also noticed that many autocomplete text boxes automatically complete the text for you as you type. You need to enable this functionality because it is not set by default. This is done by setting the property IsTextCompletionEnabled to True:

140 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

Once this property has been set, you will see that the text automatically completes as you type, as shown in Figure 6-8.

Figure 6-8. The AutoCompleteBox with IsTextCompletionEnabled set to True

TabControl
The TabControl provides a way to separate your application’s user interface into different tabs. Using the control is very straightforward—you simply create a different TabItem for each tab, and define the content of the tab within the TabItem tags. That’s all there is to it. Consider the following example: This code creates two tabs and displays a TextBlock for each, as shown in Figure 6-9.

141 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

Figure 6-9. The TabControl example

ViewBox
The ViewBox is a control that was first offered through the toolkit and is now part of the standard Silverlight control library. As a result, you don’t need to define any additional namespace references. Any content placed within the ViewBox is automatically sized to fill the entire ViewBox. This can be ideal if you want to automatically position elements the way you want within the ViewBox. When you need items to change size, instead of changing each one individually, you can simply change the size of the ViewBox and all items are automatically resized to fit. As a quick example, let’s create an icon with some text underneath it, as shown in Figure 6-10.

142 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

Figure 6-10. An icon with a text label If you want to resize these two items without a ViewBox, you need to change the size of each item. However, by placing both items within a ViewBox, all you need to do is resize the ViewBox. To demonstrate this, place the same source for the icon and text in three different-sized ViewBox controls:

143 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

The result is shown in Figure 6-11. As you can see, the icon and text are resized to fit each ViewBox and the proportion and positioning is maintained.

Figure 6-11. Icon and text label in three ViewBox controls

Modal Windows
Another feature in the Silverlight Toolkit is the Modal Child Window. This provides functionality to pop up a window that disables the rest of the application until the window is closed, something that is very common in Windows desktop development. The Silverlight modal window’s visual appearance and content is defined by XAML, just like everything else in Silverlight, which gives you a lot of control.

REFACTORING THE CHILD WINDOW
Out of the box, the Child Window can operate only as a modal dialog, which means that it has to disable the content of the application while it is open. However, at times you might prefer to have the child window behave more like a standard window. Good news! The Child Window was developed out of the Silverlight Toolkit project on CodePlex, and as a result, you have access to the entire source code under the Ms-PL license. You can download the source from http://silverlight.codeplex.com and make any modifications you’d like, including refactoring the Child Window to operate not only as a modal dialog, but also as a standard floating and draggable window.

144 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

To show a modal dialog, let’s create an instance of the window and call its Show() method. The Show() method is an asynchronous call and it returns immediately, so you won’t be able to get the result from the dialog using this method. Instead, you’ll need to handle the Closed event from the window and check the DialogResult there: Confirm confirmDlg = new Confirm(); confirmDlg.Closed += new EventHandler(confirmDlg_Closed); confirmDlg.Show(); void confirmDlg_Closed(object sender, EventArgs e) { Confirm confirmDlg = (Confirm)sender; if (confirmDlg.DialogResult == true) { // User Clicked OK } else if (confirmDlg.DialogResult = false) { // User Clicked Cancel } } Note that the DialogResult is not a standard Boolean type; it is a nullable Boolean. Therefore, there are three possible values: true, false, and null. In C#, a nullable Boolean is specified with the syntax bool?. void confirmDlg_Closed(object sender, EventArgs e) { Confirm confirmDlg = (Confirm)sender; bool? Result = confirmDlg.DialogResult; } In addition to simply getting a true/false/null response from the Child Window, you can implement your own properties that can be passed from the dialog. To retrieve these property values, in the Closed()event handler, you cast the sender object to your child window’s type and simply access the property: void confirmDlg_Closed(object sender, EventArgs e) { Confirm confirmDlg = (Confirm)sender; string myPropValue = confirmDlg.MyProperty; } Let’s run through a quick exercise to see how to create a modal pop-up window in Silverlight.

Try It Out: Using the Modal Child Window
In this exercise, you’ll create a simple registration form that accepts a first name and last name. When someone presses the button to register, a modal window will appear with a terms-and-conditions notice that users must agree to before proceeding. You won’t fully code the registration form; you’ll just send a result to a TextBlock so that you can see what’s going on. Let’s get started. 1. Create a new Silverlight application in Visual Studio 2010 called ModalWindow. Allow Visual Studio to create a Web Application project to host the application.

145 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

2.

In the MainPage.xaml file, divide the root Grid into five rows and two columns. The height of the first four rows should be 40 pixels, and the fifth row should take up the remainder of the application. The width of the first column should be 150 pixels, and the second column should take up the remainder of the application. In addition, change the d:DesignWidth of the UserControl to 600.

3.

In the first row, add a TextBlock for a header with the Text “Register for a new Account” that spans both columns. In the second row, add a TextBlock in the first column with the Text “First Name”, and add a TextBox in the second column. Add some Margin and Padding to improve the appearance. 4. In the third row, add another TextBlock in the first column with the Text “Last Name”, and add a TextBox in the second column. Add some Margin and Padding to improve the appearance. In the fourth row, add a Button to the second column with the Text “Register. Finally, in the fifth row, add a TextBlock to the second column with the Text blank. Name the TextBlock “Result”. Your XAML should look like the following code, with the result as shown in Figure 6-12:

148 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

Figure 6-12. Modal window example with finished XAML layout 5. Now that you have the main form laid out, turn your attention to the child window. To add a child window to the project, right-click on the Silverlight project (ModalWindow) and select Add ➤ New Item. From the Add New Item dialog, select Silverlight Child Window and name the window Confirm.xaml, as shown in Figure 6-13.

149 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

Figure 6-13. Adding a Silverlight Child Window 6. When the Child Window has been added to the project, it will contain the following XAML by default: 7. Notice that two buttons have been added for you already: one for Cancel and one for OK. If you look at the code behind for the window, you’ll also see that some code is already present: namespace ModalWindow { public partial class Confirm : ChildWindow { public Confirm() { InitializeComponent(); } private void OKButton_Click(object sender, RoutedEventArgs e) { this.DialogResult = true; } private void CancelButton_Click(object sender, RoutedEventArgs e) { this.DialogResult = false; } } 8. }

Two event handlers, one for each button, have been wired up, but notice that the code is simply setting the DialogResult property on the window. In the property setter, it will automatically set the response and execute the dialog’s Close() method, so that’s all the code you need. For now, just leave the Child Window as is, but you do need to call it from the Silverlight application. Open the MainPage.xaml.cs code-behind file. Add the Button_Click event as well as the code to create an instance of the Child Window and execute the Show() method:

9.

151 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

private void Button_Click(object sender, RoutedEventArgs e) { Confirm confirmDlg = new Confirm(); confirmDlg.Show(); } 10. Now run the application, and click the Register button. You will see that the Child Window appears, as shown in Figure 6-14. You can drag the window, but notice that the main user interface for your application is inaccessible. Click OK or Cancel, and you’ll find that the Child Window closes and the application’s user interface is once again functioning.

Figure 6-14. The default Child Window 11. Very cool, but let’s not stop there. Let’s modify the Child Window to show that its content can be customized any way you like by editing the window’s XAML. To do this, open the Confirm.xaml file in XAML design mode. Change the Title of the window to “Terms and Conditions.” Let’s also change the height of the Window to 200 pixels and make the Text of the two buttons read “I Accept” and “I Do Not Accept.” Because you are changing the text, you must also adjust the

152 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

width of the buttons and the margins. (Note that you can just as easily put these two buttons in a horizontal StackPanel instead of spacing them using margins.) Finally, add two TextBlock controls to the first row of the root Grid for the header, and add one below it for the terms and conditions text. Your updated XAML should now be similar to the following: 12. Go ahead and run the application again, and then click the Register button to open the Child Window. Notice that the content changes are reflected, as shown in Figure 6-15. Keep in mind that the content of these window controls is completely customizable with XAML. You can add any controls you want with any layout you want.

153 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

Figure 6-15. The modified Child Window 13. Now let’s add code to retrieve results from the dialog. Open the MainPage.xaml.cs file, and within the Button_Click event handler, wire up another event handler for the Child Window’s Closed()event. In this new event handler, you need to get the Child Window’s instance, which is sent to the handler in the sender object. Once you have the window’s instance, you can retrieve the DialogResult property, which will contain true, false, or null. public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { Confirm confirmDlg = new Confirm(); confirmDlg.Closed += new EventHandler(confirmDlg_Closed);

154 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

}

confirmDlg.Show();

void confirmDlg_Closed(object sender, EventArgs e) { Confirm confirmDlg = (Confirm)sender; if (confirmDlg.DialogResult == true) { this.Result.Text = "Terms and Conditions Accepted"; } else if (confirmDlg.DialogResult == false) { this.Result.Text = "Terms and Conditions Not Accepted"; }

} }

14. Run the application. Click the Register button to display the Child Window, and then press the I Accept button in the Child Window. You’ll see that the Result TextBlock is updated to read “Terms and Conditions Accepted”, as shown in Figure 6-16.

155 www.it-ebooks.info CHAPTER 6  SILVERLIGHT TOOLKIT

Figure 6-16. Retrieving the DialogResult from a child window

Summary
In this chapter, we looked at the Silverlight Toolkit, an excellent resource with a number of rich controls that Silverlight developers can use. What’s even better is that the Toolkit provides the full source code, so developers can extend the controls and modify them to meet their specific needs. In the next chapter, we will look at data access and networking in Silverlight.

156 www.it-ebooks.info CHAPTER 7

Data Access and Networking
Data access in Silverlight applications works differently than it does in traditional applications. You’ll need to be aware of how it works and the limitations. In this chapter, you will look at what makes data access different, and then explore mechanisms for accessing data in a Silverlight application.

Data Access in Silverlight Applications
As discussed in Chapter 1, Rich Internet Applications (RIAs) bridge the gap between Windows-based smart clients and web-based applications. When moving to this type of environment, data access and networking can be confusing. In a Windows-based smart client, the application has access to the database at all times. It can create a connection to the database, maintain state with the database, and remain connected. On the other hand, a web application is what is known as a pseudo-conversational environment, which is, for the most part, a completely stateless and disconnected environment. When a client makes a request to the web server, the web server processes the request and returns a response to the client. After that response has been sent, the connection between the client and the server is disconnected, and the server moves on to the next client request. No connection or state is maintained between the two. In Silverlight applications, you have one additional layer of complexity. The application runs from the client’s machine. However, it is still a disconnected environment because it is hosted within a web browser. There is no concept of posting back for each request or creating a round-trip to the server for data processing. Therefore, data access is limited to a small number of options. In addition, a Silverlight application has a number of security restrictions placed on it to protect the users from the application gaining too much control over their machine. For instance, the Silverlight application has access to only an isolated storage space to store its disconnected data. It has no access whatsoever to the client’s hard disk outside its sandbox. Silverlight’s isolated storage is discussed in more detail in Chapter 9. What are your options for accessing data in a Silverlight application? The following main mechanisms are available: • The most common mechanism to access data from a Silverlight application is through web services, typically a Windows Communication Foundation (WCF) service. Silverlight applications can access data using ADO.NET Data Services, which provides access to data through a URI syntax. Silverlight also has built-in socket support, which allows applications to connect directly to a server through TCP sockets.

• •

157 www.it-ebooks.info CHAPTER 7  DATA ACCESS AND NETWORKING



Silverlight has out-of-the-box support for JavaScript Object Notation (JSON), as well as RSS 2.0 and Atom 1.0 syndication feed formats.

Of these mechanisms, I’ll explore accessing WCF services from Silverlight in depth, and then give you a high-level look at using sockets. For examples and more information on accessing other data services, refer to Pro Silverlight 5 in C# by Matthew MacDonald (Apress, 2012).

Accessing Data Through Web Services
One of the ways that a Silverlight application can access data is through web services. These can be ASP.NET Web Services (ASMX), Windows Communication Foundation (WCF) services, or representational state transfer (REST) services. Here, you will concentrate on using a WCF service, which is the preferred way of accessing data in a Silverlight application through web services.

Try It Out: Accessing Data Through a WCF Service
To demonstrate accessing data from a WCF service, you will build the same application that you built in Chapter 5 to try out the DataGrid. (For more information about any part of this exercise regarding the DataGrid, refer back to Chapter 5.) The difference will be that the application will get the data through a web service. As you’ll recall, this application displays common starting hands in poker and the nicknames that have been given to those starting hands. The UI will have three columns: the first column will display two images of the cards in the hand, the second column will display the nickname, and the third column will contain notes about the hand. The completed application is shown in Figure 7-1.

158 www.it-ebooks.info CHAPTER 7  DATA ACCESS AND NETWORKING

Figure 7-1. The poker starting hands application 1. Create a new Silverlight application in Visual Studio 2010. Call the application WCFService, and allow Visual Studio to create a Web Application project named WCFService.Web to host your application. Right-click the WCFService.Web project, and select Add ➤ New Item. Create a new class with the Class Diagram template, as you saw in Chapter 2, or create a new empty Code File. Name the new class StartingHands.cs, as shown in Figure 7-2.

2.

159 www.it-ebooks.info CHAPTER 7  DATA ACCESS AND NETWORKING

Figure 7-2. Adding the StartingHands.cs class to the project 3. Now you need to implement the StartingHands.cs class. It is very similar to the class used in Chapter 5’s DataGrid example. To save yourself some typing, you can copy the code from that project. As shown in bold in the following code, the only differences are the namespace and the return type of the GetHands() method. Instead of using an ObservableCollection, it will return a simple List.

 Note In a real-world example, the StartingHands.cs class would be doing something like retrieving data from a SQL Server database and executing some business logic rules on the data. For simplicity, this example just returns a static collection.

using using using using

System; System.Collections.Generic; System.Linq; System.Web;

160 www.it-ebooks.info CHAPTER 7  DATA ACCESS AND NETWORKING

namespace WCFService.Web { public class StartingHands { public string Nickname { get; set; } public string Notes { get; set; } public string Card1 { get; set; } public string Card2 { get; set; } public static List GetHands() { List hands = new List(); hands.Add( new StartingHands() { Nickname = "Big Slick", Notes = "Also referred to as Anna Kournikova.", Card1 = "As", Card2 = "Ks" }); hands.Add( new StartingHands() { Nickname = "Pocket Rockets", Notes = "Also referred to as Bullets.", Card1 = "As", Card2 = "Ad" }); hands.Add( new StartingHands() { Nickname = "Blackjack", Notes = "The casino game blackjack.", Card1 = "As", Card2 = "Js" }); hands.Add( new StartingHands() { Nickname = "Cowboys", Notes = "Also referred to as King Kong", Card1 = "Ks", Card2 = "Kd" }); hands.Add( new StartingHands()

161 www.it-ebooks.info CHAPTER 7  DATA ACCESS AND NETWORKING

{

Nickname = "Doyle Brunson", Notes = "Named after poker great Doyle Brunson", Card1 = "Ts", Card2 = "2s"

}); return hands;

} } 4.

}

Next, you need to add the WCF service that will call the StartingHands. GetHands() method. Right-click the WCFService.Web project, and select Add ➤ New Item. In the Add New Item dialog box, select the template named “Silverlight-enabled WCF Service” and name it StartingHandService.svc, as shown in Figure 7-3. Then click the Add button.

Figure 7-3. Adding the Silverlight-enabled WCF service

162 www.it-ebooks.info CHAPTER 7  DATA ACCESS AND NETWORKING

5.

This adds a service named StartingHandService.svc to the project with an attached code-behind file named StartingHandService.svc.cs. View that code behind. You will see that Visual Studio has already created the base WCF service, including a sample method called DoWork(), as follows: namespace WCFService.Web { [ServiceContract(Namespace = "")] [SilverlightFaultBehavior] [AspNetCompatibilityRequirements( RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class StartingHandService { [OperationContract] public void DoWork() { // Add your operation implementation here return; } // Add more operations here and mark them with [OperationContract] } }

6.

Replace the DoWork() method with a GetHands() method that returns a List collection, as follows: namespace WCFService.Web { [ServiceContract(Namespace = "")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class StartingHandService { [OperationContract] public List GetHands() { return StartingHands.GetHands(); } // Add more operations here and mark them // with [OperationContract] } } This method simply returns the results from calling the StartingHands.GetHands() method. Note that you need to add a using statement for System.Collections.Generic.

7.

Now that you have a Silverlight-enabled WCF service, you need to add a reference in your Silverlight project so that your Silverlight application can access the service. To do this, right-click References within the WCFService project in Solution Explorer and select Add Service Reference, as shown in Figure 7-4. This brings up the Add Service Reference dialog box.

163 www.it-ebooks.info CHAPTER 7  DATA ACCESS AND NETWORKING

Figure 7-4. Choosing to add a service reference 8. 9. In the Add Service Reference dialog box, click the Discover button, as shown in Figure 7-5. Visual Studio will find the StartingHandService.svc and populate the Services list in the Add Service Reference dialog box. Note that you might need to build the solution before Visual Studio will find your service. Expand the StartingHandService.svc node to show the StartingHandService. Click StartingHandService to see the GetHands() web method in the Operations listing, as shown in Figure 7-6. Enter StartingHandServiceReference in the Namespace field, and then click OK to continue.

164 www.it-ebooks.info CHAPTER 7  DATA ACCESS AND NETWORKING

Figure 7-5. Finding the services in the solution

165 www.it-ebooks.info CHAPTER 7  DATA ACCESS AND NETWORKING

Figure 7-6. Adding a service reference for StartingHandService 10. Open the Visual Studio Object Browser by selecting View ➤ Object Browser from the main menu. Navigate to the WCFService entry, and expand the tree. You will find WCFService.StartingHandServiceReference under your project. Within that, you will see an object named StartingHandServiceClient. Select this object to examine it, as shown in Figure 7-7.

166 www.it-ebooks.info CHAPTER 7  DATA ACCESS AND NETWORKING

Figure 7-7. Object Browser for StartingHandService 11. Look at the members listed on the right side of the Object Browser. There are a number of items that are added, but take specific note of the method named GetHandsAsync() and the event named GetHandsCompleted. You need to use both of these in order to call your web service from Silverlight. 12. Now it’s time to create the Silverlight application’s UI. Open the MainPage.xaml file in Visual Studio. Place the cursor within the root Grid, and double-click the DataGrid control in the Toolbox. Once the DataGrid has been added, rightclick on it in the designer and select Reset Layout ➤ All. This adds the following XAML: 13. Rename the DataGrid to grdData, and set the Margin to 15. Next, add the following Column definitions, which are from the previous DataGrid exercise in Chapter 5. The DataGrid contains three columns: one template column containing the two cards in the hand, and two text columns containing the nickname and notes about the hand.

167 www.it-ebooks.info CHAPTER 7  DATA ACCESS AND NETWORKING

168 www.it-ebooks.info CHAPTER 7  DATA ACCESS AND NETWORKING

14. Save the MainPage.xaml file, and navigate to the code behind for the application, located in the MainPage.xaml.cs file. Wire up the Loaded event handler for the page, as follows: public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); this.Loaded += new RoutedEventHandler(Page_Loaded); } void Page_Loaded(object sender, RoutedEventArgs e) { throw new NotImplementedException(); } } 15. Next, you need to call the WCF service. In Silverlight, web services can be called only asynchronously, so the browser’s execution is not blocked by the transaction. To do this, you need to get an instance of the service reference (commonly referred to as the web service proxy class) named StartingHandService, which you added earlier. Then wire up an event handler for the service’s GetHandsCompleted event, which you examined in the Object Browser (in step 8). This is the event handler that will be called when the service has completed execution. Finally, execute the GetHandsAsync() method.

■ Tip In a real-world scenario, you will want to present the user with a progress bar or animation while the service is being called because the duration of a web service call can be lengthy.

16. Within the Page_Loaded event handler, first obtain an instance of StartingHandService. Then, in the GetHandsCompleted event handler, bind the ItemsSource of the DataGrid to the result returned from the service call, as shown in the following code. Note that normally you want to check the result to make certain that the web service call was successful and alert the user accordingly in case of failure. using WCFService.StartingHandServiceReference; ... public partial class MainPage : UserControl { public MainPage() {

169 www.it-ebooks.info CHAPTER 7  DATA ACCESS AND NETWORKING

InitializeComponent(); this.Loaded += new RoutedEventHandler(Page_Loaded); } void Page_Loaded(object sender, RoutedEventArgs e) { StartingHandServiceClient service = new StartingHandServiceClient(); service.GetHandsCompleted += new EventHandler( service_GetHandsCompleted); service.GetHandsAsync(); } void service_GetHandsCompleted(object sender, GetHandsCompletedEventArgs e) { this.grdData.ItemsSource = e.Result; } } 17. Test your application. If all goes well, you should see the populated DataGrid, as shown earlier in Figure 7-1. This example demonstrated how to use the Silverlight-enabled WCF service provided in Visual Studio to allow your Silverlight application to access data remotely. As noted earlier in the chapter in the section “Data Access in Silverlight Applications,” this is one of the most common approaches to data access with Silverlight.

Accessing Services from Other Domains
In the previous example, the web service was on the same domain as your Silverlight application. What if you want to call a service that is on a different domain? If you attempt to access a service from a different domain in Silverlight, you will notice that it fails. This is because, by default, a Silverlight application cannot call services that are on a different domain, unless it is permitted to do so by the service host. Silverlight determines if it has permission to access a service on a certain domain by looking for one of two files in the root of the target domain: clientaccesspolicy.xml or crossdomain.xml. First, Silverlight looks for a file named clientaccesspolicy.xml in the domain’s root. This is Silverlight’s client-access policy file. If you are publishing your own services that you want to be accessible by Silverlight applications, this is the file you want to use, because it provides the most options for Silverlight application policy permissions. The following is a sample clientaccesspolicy.xml file:

170 www.it-ebooks.info CHAPTER 7  DATA ACCESS AND NETWORKING

The important elements are and . The element defines which domains are permitted to access the resources specified in the element. If Silverlight cannot find a clientaccesspolicy.xml file at the root of the domain from which you are attempting to access a service, it then looks for a file named crossdomain.xml in the root. This is the XML policy file that has been used to provide access for Flash applications to access cross-domain services, and Silverlight supports this file as well. The following is an example of a crossdomain.xml file: Again, even though Silverlight supports crossdomain.xml, using clientaccesspolicy.xml for Silverlight applications is the preferred and best practice.

Accessing Data Through Sockets
In the majority of cases, your Silverlight applications accesses data through web services. However, Silverlight provides another mechanism that, though rarely used, can be quite powerful. This mechanism is socket communications. In this section, you will look at a greatly simplified example of communicating with a server via sockets and TCP. The main purpose here is to give you a taste of using sockets in Silverlight so that you have a basic understanding of the process and can consider whether you would like to take this approach. If so, you can refer to a more advanced resource, such as Pro Silverlight 5 in C# by Matthew MacDonald (Apress, 2012). For this example, let’s assume you have a socket server running at the IP address 192.168.1.100 on port 4500. The socket server simply accepts text inputs and does something with them. In Silverlight, you want to connect to that socket server and send it text from a TextBox control. First, you make a connection to the socket server. To do this, create an instance of a System.Net.Sockets.Socket object for IP version 4 (AddressFamily.InterNetwork). The type will be Stream, meaning it will accept a stream of bytes, and the protocol will be TCP.

171 www.it-ebooks.info CHAPTER 7  DATA ACCESS AND NETWORKING

Socket socket; socket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); You need to execute the socket’s ConnectAsync() method, but first you must create an instance of SocketAsyncEventArgs to pass to the method, using a statement similar to the following: SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs() { RemoteEndPoint = new IPEndPoint( IPAddress.Parse("192.168.1.100"), 4500) }; This statement sets the target for the socket connection as 192.168.1.100 on port 4500. In addition, because this is an asynchronous connection, you need to receive notification when the connection has been established. To get this notification, you wire up an event handler to be triggered on the SocketAsyncEventArgs.Completed event. Once you have that wired up, you simply call the ConnectAsync() method, passing it your SocketAsyncEventArgs instance: socketArgs.Completed += new EventHandler(socketArgs_Completed); socket.ConnectAsync(socketArgs); The method for this event handler first removes the event handler, and then examines the response from the socket server. If it is successful, it sends a stream of bytes from your TextBox control to the socket server through your established connection. void socketArgs_Completed(object sender, SocketAsyncEventArgs e) { e.Completed -= socketArgs_Completed; if (e.SocketError == SocketError.Success) { SocketAsyncEventArgs args = new SocketAsyncEventArgs(); args.SetBuffer(bytes, 0, bytes.Length); args.Completed += new EventHandler(OnSendCompleted); socket.SendAsync(args); }

}

Once again, because the calls to the socket are asynchronous, you wire up another event handler called OnSendCompleted, which fires when your SendAsync() method is completed. This event handler will do nothing more than close the socket: void OnSendCompleted(object sender, SocketAsyncEventArgs e) { socket.Close(); } Although this seems pretty simple, it is complicated by client-access policy permissions. In the same way that a Silverlight application can call a web service on a separate domain only if it has the proper

172 www.it-ebooks.info CHAPTER 7  DATA ACCESS AND NETWORKING

client-access policy permissions, a Silverlight application can call a socket server only if that server contains the proper client-access policy permissions. The following is an example of a client-access policy for a socket server: Recall that when you’re using a web service, the client-access policy is contained in a file named clientaccesspolicy.xml, which is placed in the domain’s root. In a socket access situation, things are a bit more complex. Before Silverlight makes a socket request to a server on whatever port is requested by the application, it first makes a socket request of its own to the server on port 943, requesting a policy file. Therefore, your server must have a socket service set up to listen to requests on port 943 and serve up the contents of the client-access policy in order for Silverlight applications to be able to make a socket connection.

Summary
In this chapter, you focused on accessing data from your Silverlight applications through WCF services. I also discussed accessing data from different domains and cross-domain policy files. In addition, you looked at using sockets in Silverlight from a high level. In the next chapter, you will look at Silverlight’s Navigational Framework.

173 www.it-ebooks.info CHAPTER 8

Navigation Framework
The Navigation Framework is a feature in Silverlight that allows developers to implement a way to navigate through different pages within a Silverlight application, creating an experience similar to browsing through different pages of a web site. The framework also allows developers to create a history that can integrate with the browser, enabling users to navigate forward and backward through the history using the browser’s back and forward buttons. In this chapter, you will explore Silverlight’s Navigation Framework and try out a couple of examples involving the different aspects of the framework.

Frame and Page Object
The two main objects that are contained in the Navigation Framework are the Frame and Page objects. (See Figure 8-1.) The Frame object is very similar to a ContentPlaceHolder in ASP.NET master pages and is the placeholder for the different views to be loaded onto the page.

Figure 8-1. Frame and Page objects

175 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

Try It Out: Creating a Silverlight Navigation Application
This exercise demonstrates creating a Silverlight application with navigation support from scratch using the Navigation Framework. In the exercise, you will build a simple application that contains two HyperlinkButton elements and a Frame. Clicking the links loads one of two pages into the Frame. Let’s get started: 1. 2. Start Visual Studio 2010, and select File ➤ New ➤ Project from the main menu. In the New Project dialog box, select Silverlight as the project type and Silverlight Application as the template. Name the project NavAppFromScratch, as shown in Figure 8-2.

Figure 8-2. Creating a new Silverlight application 3. When the New Silverlight Application dialog appears, select the default to host the Silverlight application in a new ASP.NET web application named NavAppFromScratch.Web. Click OK to continue. By default, the MainPage.xaml file is created and opened for editing. You will start by editing that file. In the Grid definition, add ShowGridLines="True" so

4.

176 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

that you can see how your cells are laid out. You can turn this property off later so that your application is cleaner. 5. Next you want to define the Grid cells. You will simply have two rows: one for the links and one for the navigated content. 6. Now that you have the two rows, you want to add the HyperlinkButton elements that will be used to navigate to the different views. You will do this in a horizontal StackPanel. For the Click property, create an event handler called LinkClick: 7. The next step is to add support for the Navigation Framework in your project. First add a reference to System.Windows.Controls.Navigation.dll by rightclicking on the References folder in your Silverlight project and choosing Add Reference, as shown in Figure 8-3.

177 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

Figure 8-3. The Silverlight navigation application contents 8. When the Add Reference dialog appears, be sure the .NET tab is selected and then browse through the list until you find System.Windows.Controls.Navigation, as shown in Figure 8-4. Select the entry, and click OK to add the reference to the project.

178 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

Figure 8-4. The Silverlight navigation application references 9. When the assembly is added, you will see it appear under References in the Solution Explorer, as shown in Figure 8-5.

179 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

Figure 8-5. The Silverlight navigation application contents with reference 10. Now that you have added the reference to the Navigation Framework, you need to add the navigation objects to your application. You start by adding the XML namespace for System.Windows.Controls.Navigation to the UserControl definition:

180 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

11. Now add a Frame to the bottom row of the root Grid named ContentFrame. Set the HorizontalContentAlignment and VerticalContentAlignment to Stretch so that the Frame consumes the entire Grid cell. Also, give the Frame a 10-pixel Margin and a BorderThickness of 2 pixels: 12. Next, add the different views to the project. Right-click on the Silverlight project, and select Add ➤ New Item.

181 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

13. In the New Item dialog, select the Silverlight Page template, name the page View1.xaml, and click the Add button. (See Figure 8-6.)

Figure 8-6. Adding a Silverlight page 14. Once View1.xaml has been added, repeat steps 12 and 13 to add another Silverlight page named View2.xaml. 15. Open View1.xaml in design mode, and add the following XAML to the root Grid: 16. Open View2.xaml in design mode, and add the following XAML to the root Grid:

182 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

17. You now have the main page containing the Frame and the two views you will load into the Frame. Next, you need to actually load the views into the Frame. You do this on the click event of the two HyperlinkButtons you added in step 6. Although you can easily do this with two click event handlers, you will actually do it with one. Set the Tag property of the HyperlinkButton to be the page view source file. Then the click event handler will be able to retrieve the source file from the Tag: 18. Right-click on LinkClick in the Click attribute, and select Navigate to Event Handler to create the LinkClick event handler. Within the event, add the following code to retrieve the view’s source file: private void LinkClick(object sender, RoutedEventArgs e) { HyperlinkButton button = (HyperlinkButton)sender; string viewSource = button.Tag.ToString(); } 19. Now that you have the view’s source file, you can use the Frame’s Navigate method to navigate to the proper view: private void LinkClick(object sender, RoutedEventArgs e) { HyperlinkButton button = (HyperlinkButton)sender; string viewSource = button.Tag.ToString(); ContentFrame.Navigate(new Uri(viewSource, UriKind.Relative)); } 20. You are now ready to run the solution. Select Debug ➤ Start Debugging, or press F5 to run the application. Internet Explorer will open and the application will be displayed, as shown in Figure 8-7.

183 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

Figure 8-7. Testing the Silverlight navigation application 21. Click the View 1 HyperlinkButton at the top of the screen. The Content Frame navigates to the View1.xaml content, as shown in Figure 8-8.

184 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

Figure 8-8. Testing the Silverlight navigation application template View 1 22. Then click on the View 2 link to see similar results, as shown in Figure 8-9.

185 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

Figure 8-9. Testing the Silverlight navigation application template View 2 23. Notice that you can click the browser’s back button to navigate backward in history from View 2 to View 1, and back to the default.

Benefits of the Navigation Framework
While the functionality of the Navigation Framework might have been achieved in previous versions of Silverlight, the amount of work it required was significant and normally required you to purchase a third-party control or library. Clearly, having this functionally built into Silverlight is a major advantage. It reduces the amount of code required to achieve the same affects and produces much cleaner and more maintainable code. In addition, it provides a number of additional benefits, such as browser history support and deep linking.

186 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

Deep Linking
Another benefit of the Navigation Framework in Silverlight is deep-linking support. Deep linking is the ability to link to an application at a specific state. To illustrate deep linking, consider an application when it is loaded and the home page is displayed. When the user clicks on a link from the home page, the application navigates to the product listings page. The user can then click on a product to navigate to a page containing the details for that product. This application can be represented by the diagram shown in Figure 8-10.

Figure 8-10. Deep linking in Silverlight Let’s say you wanted to generate a link directly to the Product B Details page in the application. With the Navigation Framework, Silverlight allows developers to link to different states in their application.

The NavigationService Object
As you saw earlier in this chapter, you change different views using the Frame object’s Navigate method. There are times when you need to gain access to the Frame from within the page itself. For example, if you consider the diagram in Figure 8-11, you can easily navigate to View 1 from the Navigation Frame on the home page. However, if you want to navigate to Inner View 1 from the code behind on View 1, you need to get access to the Navigation Frame that is hosting View 1 in order to navigate to a different view.

Figure 8-11. NavigationService object in Silverlight

187 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

Luckily, the Navigation Framework contains an object that allows a view to access its Hosting Frame. That object is the NavigationService. Let’s explore the use of the NavigationService object by running through the following exercise.

Try It Out: Using the NavigationService Object
In this exercise, you will expand on the example you built earlier in the chapter. You add a button to the View 1 page and, on the click event of that button, you navigate to a new page called Inner View 1 using the NavigationService object. Let’s get started: 1. 2. Begin by opening the project NavAppFromScratch you just completed in the previous section. Open the XAML for View1.xaml, and modify the source to include a button under the TextBlock: 3. You now need to add the new view that you will navigate to using the NavigationService. Right-click on the Silverlight project, and choose Add ➤ New Item. Select Silverlight Page as the template, and name the file InnerView1.xaml. In the XAML for InnerView1.xaml, add a simple TextBlock: 5. Next, add the Button_Click event handler in the View1.xaml code behind, and add the following code: private void Button_Click(object sender, RoutedEventArgs e) { NavigationService.Navigate( new Uri("/InnerView1.xaml", UriKind.Relative)); }

4.

188 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

6.

You are now ready to run the solution. Select Debug ➤ Start Debugging, or press F5 to run the application. When Internet Explorer opens the application, click on the View 1 link at the top. The application should appear as shown in Figure 8-12.

Figure 8-12. Testing the NavigationService object 7. If you click on the Navigate To Inner View button, the application now shows the InnerView1.xaml content in the top frame, as seen in Figure 8-13.

189 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

Figure 8-13. Inner View 1 using NavigationService In this section, you learned how to use the NavigationService object to access the Navigation Frame from a Silverlight page. In the next section, you will learn how to pass data to navigation pages using another object contained in the Navigation Framework, the NetworkContext object.

Passing Data to Navigation Pages
In this section, you will discuss passing data to page views within a Navigation Framework solution. In HTML pages, data is passed to other pages using the QueryString. The same is true for pages within a Silverlight navigation application through the use of the NavigationContext object. As an example, if you want to retrieve the QueryString property ProductID, you use the following syntax: string productId = NavigationContext.QueryString["ProductID"].ToString(); Let’s explore how to use the NavigationContext object to pass data to views.

190 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

Try It Out: Passing Data to Navigation Pages
In this exercise, you will expand on the project you continued working on in the previous section. You pass some additional data to the InnerView1.xaml file, retrieve that data using the NavigationContext object, and then display the view content dependent on that data. 1. 2. Begin by opening the project NavAppFromScratch you were working on in the previous section. Open the XAML for View1.xaml, and modify the source to include a ComboBox under the Button: 3. Next open the code behind for View1.xaml, and edit the Button_Click event handler to pass the selected color in the query string of the URI passed to the Navigate method: private void Button_Click(object sender, RoutedEventArgs e) { string color = Color.SelectionBoxItem.ToString(); NavigationService.Navigate( new Uri(string.Format("/InnerView1.xaml?Color={0}", color), UriKind.Relative)); } 4. Open the InnerView1.xaml file, and add a second TextBlock below the existing TextBlock using a StackPanel: 5. Open the code behind for InnerView1.xaml, and retrieve the passed color using the NavigationContext object. Then add a switch statement to change the color of the TextBlocks, and edit the text for the second TextBlock: protected override void OnNavigatedTo(NavigationEventArgs e) { string color = NavigationContext.QueryString["Color"].ToString(); Brush b; switch (color) { case "Red": b = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0)); ViewHeader.Foreground = b; ViewColor.Foreground = b; ViewColor.Text = "(Red)"; break; case "Green": b = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0)); ViewHeader.Foreground = b; ViewColor.Foreground = b; ViewColor.Text = "(Green)"; break; default: b = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255)); ViewHeader.Foreground = b; ViewColor.Foreground = b; ViewColor.Text = "(Blue)"; break;

} } 6.

You are now ready to run the solution. Select Debug ➤ Start Debugging, or press F5 to run the application. When Internet Explorer opens the application, click on the View 1 link at the top. The application should appear, as shown in Figure 8-14.

192 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

Figure 8-14. Testing the navigation application passing data 7. Select Red in the combo box, and click the Navigate To Inner View button. You will see the content of the InnerView1.xaml displayed with red text and with the text “(Red)” displayed, as shown in Figure 8-15.

193 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

Figure 8-15. Navigation result with data passed In this section, you learned how to use the NavigationContext object to pass data to navigation views using the query string. In the next section, I will discuss URI Mapping and how it can be used to create user-friendly URIs to your navigation views.

URI Mapping
In the preceding examples, you might have noticed the URL changing as you navigated to different views in a frame. You might have also noticed that the URLs were not very pretty and contained some information you might not want to display. As an example, consider the following URL: http://www.domain.com/Catalog.aspx#ProductDetails.xaml?ID=4 For starters, this URL is not very pleasant to look at, and it’s not very user-friendly either. It also might contain information you prefer not to provide the user, such as the exact file name and the query string name. A much more appropriate URL would look like the following:

194 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

http://www.domain.com/Catalog.aspx#Product/4 This URL is much easier to read and is more user friendly. In addition, it doesn’t give away any details about your solution. You can obtain this URL using a feature known as URI Mapping. Let’s work through an example to further explore URI Mapping with the Navigation Framework.

Try It Out: URI Mapping and the Navigation Framework
In this example, you will work through implementing URI Mapping with the project you have been working with earlier in the chapter. 1. 2. Begin by opening the project NavAppFromScratch you were working on in the previous section. There are three views in your solution you will add a URI Mapping for: View1.xaml, View2.xaml, and InnerView1.xaml. For these views, you add simple URI maps that point them to View1, View2, and InnerView. Start by opening the App.xaml file and adding the xml namespace for the Navigation Framework: 3. Now that the namespace is added, you need to add the UriMapper section to the application resources: 4. Within the UriMapper section, you now need to add two UriMapping elements: one for View1.xaml and one for View2.xaml. Each mapping contains two attributes: the Uri attribute is the name representing the mapping that will appear in the browser address bar, and the MappedUri attribute represents the actual URI mapped to by the UriMapping: 5. Update MainPage.xaml to navigate to the views using the UriMappings:

195 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

6. Next, shift your attention to the InnerView1.xaml. If you recall in the previous section on passing data to a navigation view, you are passing the color to InnerView1.xaml via the QueryString. Because of this, you need that to be taken into account in your UriMapping. Open up the code behind for View1.xaml, and modify the Button_Click method so that it navigates to InnerView/{0}: private void Button_Click(object sender, RoutedEventArgs e) { string color = Color.SelectionBoxItem.ToString(); NavigationService.Navigate( new Uri(string.Format("InnerView/{0}", color), UriKind.Relative)); } 7. For the navigation to work, you need to add an additional UriMapping to the Application.Resources in the App.xaml file: 8. Next, in the MainPage.xaml, add the UriMapper property to the Navigation Frame object:

196 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

9.

You are now ready to run the solution. Select Debug ➤ Start Debugging, or press F5 to run the application. When Internet Explorer opens the application, click on the View 1 link at the top. Notice that the URL now reads as follows: NavAppFromScratchTestPage.aspx#

10. Now select Red, click on Navigate To Inner View, and once again inspect the URL: NavAppFromScratchTestPage.aspx#InnerView/Red As you saw in this example, UriMapping provides a way to create more user-friendly URL addresses and to keep application-specific information from appearing in your application.

URI ROUTING
In addition to URI Mapping, the Navigation Framework in Silverlight supports URI Routing. For example, if you place all of your navigation views in a subdirectory named Views, you can follow a naming convention that you set. Then set up URI routes such as the following:

This mapping will map all files within the Views directory to its file name minus the extension. For example, /Views/View1.xaml would map to View1 and /Views/AboutPage.xaml would map to AboutPage. As you can see, if you are able to set a naming convention that you can follow, URI Routing can really help you handle default mappings with the Navigation Framework.

Silverlight Navigation Application Template
Although you can use the Navigation Framework from within a standard Silverlight application, Visual Studio 2010 contains a project template that creates a base Silverlight Navigation Application.

Try It Out: Using the Silverlight Navigation Application Template
In this example, you will create a base Silverlight application with navigation support using the built-in Silverlight Navigation Application template included in Visual Studio 2010: 1. 2. Start Visual Studio 2010, and select File ➤ New ➤ Project from the main menu. In the New Project dialog box, select Silverlight as the project type and Silverlight Navigation Application as the template. Name the project NavTemplate, as shown in Figure 8-16.

197 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

Figure 8-16. The Silverlight Navigation Application project template 3. When the New Silverlight Application dialog appears, select the default to host the Silverlight application in a new ASP.NET web application named NavTemplate.Web. Click OK to continue. When the project is created by Visual Studio, you will notice that a number of pages have already been created for you, as shown in Figure 8-17. The base navigation project contains a main page called MainPage.xaml that hosts the navigation frame and has two navigation pages in the Views folder: About.xaml and Home.xaml. In addition, there is a ErrorWindow.xaml page view that is created.

4.

198 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

Figure 8-17. The base navigation project 5. Select Debug ➤ Start Debugging, or press F5 to run the application. Internet Explorer will open and the application will be displayed, as shown in Figure 8-18.

199 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

Figure 8-18. Creating a hosting application 6. Notice at the top right-hand corner of the application there are two links: home and about. Click the about button, and the navigation frame loads the AboutPage.xaml page into the white content box, as shown in Figure 8-19.

200 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

Figure 8-19. Testing the navigation application As you have seen, the Silverlight Navigation Application template can be used to give you a base application with navigation support to build on.

Using Multiple Frames
In all the examples you have worked through in this chapter, you have dealt only with a single Frame. However, there is no limit on the number of Frames you can include in your application. There are some restrictions, though. First of all, only one Frame can integrate with the browser. Because of this, if you use multiple Frames, you need to indicate what Frame will be integrated with the browser. This is done using the JournalOwnership property on the Frame object. Consider the following example: In the preceding example, the ContentFrame has full integration with the browser, but the BottomFrame does not. Let’s see this in action in the following exercise.

201 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

Try It Out: Using Multiple Frames
In this example, you will add a second Frame to the project you have been working on throughout this chapter: 1. 2. Begin by opening the project NavAppFromScratch you were working on in the earlier section. Add a new view to the project. Right-click on the Silverlight project, and choose Add ➤ New Item. Select Silverlight Page as the template, and name the file BottomView.xaml. In the XAML for BottomView.xaml, add a simple TextBlock: 4. With the new view created, you now need to edit the MainPage.xaml file to add a third row to the Grid and add a new Frame within that new row. The second Frame will not integrate with the browser, so set the JournalOwnership property to OwnsJournal:

3.

202 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

5. Next, view the code behind for MainPage.xaml, and add a Navigate call for BottomFrame: private void LinkClick(object sender, RoutedEventArgs e) { HyperlinkButton button = (HyperlinkButton)sender; string viewSource = button.Tag.ToString(); ContentFrame.Navigate(new Uri(viewSource, UriKind.Relative)); BottomFrame.Navigate(new Uri("/BottomView.xaml", UriKind.Relative)); } 6. You are now ready to run the solution. Select Debug ➤ Start Debugging, or press F5 to run the application. Click on the View 1 link at the top and the application will appear, as shown in Figure 8-20 with the second Frame at the bottom.

203 www.it-ebooks.info CHAPTER 8  NAVIGATION FRAMEWORK

Figure 8-20. Multiple navigation frames

Summary
In this chapter, you looked at the Navigation Framework in depth and saw how it can be used to build Silverlight applications that contain multiple page views. You explored the different objects within the Navigation Framework, such as the NavigationContext and NavigationService, as well as how to implement URI Mapping within your applications.

204 www.it-ebooks.info CHAPTER 9

Isolated Storage in Silverlight
Localized storage in Silverlight is handled by its isolated storage feature, which is a virtual file system that can be used to store application data on the client’s machine. As just a few examples, you might use local storage in your application to store user settings, undo information or shopping cart contents, or use it as a local cache for your commonly used objects. Implementations of this feature are really limited only by your imagination. In this chapter, you will explore Silverlight’s isolated storage. I will walk you through building a virtual storage explorer to view the directories and files contained within isolated storage for an application. In addition, you will look at the isolated storage quota and how to increase the quota size for your Silverlight applications.

 Note Silverlight allows developers to create out-of-browser applications with elevated security. With this elevated access comes the ability to access the client’s local hard drive. In this chapter, we are discussing the isolated storage features of Silverlight only. For more information on creating applications with elevated security, see Chapter 16.

Working with Isolated Storage
Storing application information has always been a challenge for developers of traditional web applications. Often, implementing such storage means storing information in cookies or on the server, which requires using a postback to retrieve the data. In the case of desktop applications, implementing storage for application information is significantly easier because developers have more access to the user’s hard drive. Once again, Silverlight bridges the gap between desktop applications and web applications by offering isolated storage. Using the Silverlight classes for working with isolated storage, you can not only store settings locally, but also create files and directories, as well as read and write files within isolated storage.

Using the Isolated Storage API
The classes for accessing isolated storage are contained within the System.IO.IsolatedStorage namespace. This namespace contains the following three classes: • IsolatedStorageFile

205 www.it-ebooks.info CHAPTER 9  ISOLATED STORAGE IN SILVERLIGHT

• •

IsolatedStorageFileStream IsolatedStorageSettings

You’ll look at each class to see what it represents.

IsolatedStorageFile
The IsolatedStorageFile class represents the isolated storage area, and the files and directories contained within it. This class provides the majority of the properties and methods used when working with isolated storage in Silverlight. As an example, to get an instance of the user’s isolated storage for a given application, use the static method GetUserStoreForApplication(), as follows: using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { //... } Once the storage instance has been retrieved, a number of operations are available, including CreateDirectory(), CreateFile(), GetDirectoryNames(), and GetFileNames(). Also, the class has properties, such as Quota and AvailableFreeSpace. The following example creates a directory in isolated storage called Directory1, and then it retrieves the total and available free space in isolated storage: using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { store.CreateDirectory("Directory1"); long quota = store.Quota; long availableSpace = store.AvailableFreeSpace; }

IsolatedStorageFileStream
The IsolatedStorageFileStream class represents a given file. It is used to read, write, and create files within isolated storage. The class extends the FileStream class, and in most cases, developers will use a StreamReader and StreamWriter to work with the stream. As an example, the following code creates a new file named TextFile.txt and writes a string to the file: using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { IsolatedStorageFileStream stream = store.CreateFile("TextFile.txt"); System.IO.StreamWriter sw = new System.IO.StreamWriter(stream); sw.Write("Contents of the File”); sw.Close(); }

206 www.it-ebooks.info CHAPTER 9  ISOLATED STORAGE IN SILVERLIGHT

IsolatedStorageSettings
The IsolatedStorageSettings class allows developers to store key/value pairs in isolated storage. The key/value pairs are user-specific and provide a very convenient way to store settings locally. The following example demonstrates storing the user’s name in IsolatedStorageSettings: public partial class MainPage : UserControl { private IsolatedStorageSettings isSettings = IsolatedStorageSettings.ApplicationSettings; public MainPage() { InitializeComponent(); this.Loaded += new RoutedEventHandler(Page_Loaded); this.cmdSave.Click += new RoutedEventHandler(cmdSave_Click); } void cmdSave_Click(object sender, RoutedEventArgs e) { isSettings["name"] = this.txtName.Text; SetWelcomeMessage(); } void Page_Loaded(object sender, RoutedEventArgs e) { SetWelcomeMessage(); } private void SetWelcomeMessage() { if (isSettings.Contains("name")) { string name = (string)isSettings["name"]; this.txtWelcome.Text = "Welcome " + name; } else { txtWelcome.Text = "Welcome! Enter Your Name and Press Save."; } } } The first time users access the application, they see the message “Welcome! Enter Your Name and Press Save.” They can then enter their name and click the Save Name button. The name is saved in local storage under the key/value pair called name. The next time the user accesses the application, his name will still be stored in local storage, and he will see the friendly welcome message, as shown in Figure 9-1.

207 www.it-ebooks.info CHAPTER 9  ISOLATED STORAGE IN SILVERLIGHT

Figure 9-1. Saving a user’s name with IsolatedStorageSettings Now that you have briefly looked at some of the key classes associated with Silverlight’s isolated storage, let’s try building an application that uses this storage.

Try It Out: Creating a File Explorer for Isolated Storage
In this example, you will create a file explorer that allows a user to navigate through an application’s virtual storage within Silverlight’s isolated storage. The file explorer will allow users to view, modify, and create new files within the given directories. Keep in mind that each Silverlight application has its own isolated storage, so the file explorer will be unique to the application. The end result will appear as shown in Figure 9-2.

208 www.it-ebooks.info CHAPTER 9  ISOLATED STORAGE IN SILVERLIGHT

Figure 9-2. The isolated storage file explorer demo

Creating the Application Layout
Let’s get started by setting up the application layout. 1. Create a new Silverlight application in Visual Studio 2010. Name it ISExplorer, and allow Visual Studio to create an ASP.NET Web Application called ISExplorer.Web to host your application. Next, you need to define the Grid layout. You will use the LayoutRoot grid that is already added by default, and then add two columns and three rows. Set the Width property of the first column to 1* and the Width of the second column to 3*. Set the Height for the rows to 75, *, and 30 from top to bottom.

2.

209 www.it-ebooks.info CHAPTER 9  ISOLATED STORAGE IN SILVERLIGHT

3. Next, double click on GridSplitter in the toolbox to add a GridSplitter (see Chapter 4 for details) to allow the user to resize the left and right columns. Set the Grid.RowSpan to 3 and HorizontalAlignment to Right. 4. Now you will start filling the Grid cells with controls. You will add quite a few controls, using nested StackPanel components to assist you in getting the desired layout. These controls were discussed in detail in Chapters 4 and 5, and you can refer back to those chapters for more information about any of the controls used here. Run your application. It should look like Figure 9-3.

5.

210 www.it-ebooks.info CHAPTER 9  ISOLATED STORAGE IN SILVERLIGHT

Figure 9-3. The grid layout of the file explorer application 6. In Grid.Row and Grid.Column (0,0), place a StackPanel that contains a couple cosmetic TextBlock controls that will serve as your application title, as follows (with some of the existing code omitted for brevity): ... { }; } 12. After wiring up the PrintPage event, call the Print() method, which essentially calls the PrintPage logic. The Print() method requires a document name be passed, so pass in “As Is” as the name of the printed document: { private void PrintAsIs(object sender, RoutedEventArgs e) PrintDocument doc = new PrintDocument(); doc.PrintPage += (s, args) => {

363 www.it-ebooks.info CHAPTER 15  PRINTING IN SILVERLIGHT

}; doc.Print("As Is"); } 13. Now you just need to add the logic to your PrintPage lambda expression. Because you’re just printing the content as you see it on the screen, simply set the PageVisual property to the LayoutRoot to tell Silverlight to print all of the XAML content contained in the application. The PageVisual property belongs to the PrintPageEventArgs class and is passed into the PrintPage event delegate: private void PrintAsIs(object sender, RoutedEventArgs e) { PrintDocument doc = new PrintDocument(); doc.PrintPage += (s, args) => { args.PageVisual = LayoutRoot; }; } doc.Print("As Is");

14. Press F5 now to test the application. When the application is displayed, click the Print As-Is button, which displays the Print dialog as shown in Figure 15-4.

364 www.it-ebooks.info CHAPTER 15  PRINTING IN SILVERLIGHT

Figure 15-4. Print dialog box for the SimplePrinting application 15. Select the desired printer, and click Print. If all goes well, the content that is printed should be just as you see on the screen in Figure 15-5.

365 www.it-ebooks.info CHAPTER 15  PRINTING IN SILVERLIGHT

Figure 15-5. Printed output from the SimplePrinting application

Printing Custom Content
It might not always be ideal to print application content just as it’s displayed on the screen. Fortunately, however, you can print custom content. Because you have to set the PageVisual property in order to print, you can simply set that to whatever content you’d like, including dynamically created content.

Try It Out: Implementing a Custom Print
1. You will continue working from the SimplePrinting project you created in the previous section. In the PrintFormatted delegate, add a new instance of the PrintDocument class, wire up the PrintPage event, and call the Print method: private void PrintFormatted(object sender, RoutedEventArgs e) { PrintDocument doc = new PrintDocument(); doc.PrintPage += (s, args) => { }; doc.Print("Formatted Print"); }

366 www.it-ebooks.info CHAPTER 15  PRINTING IN SILVERLIGHT

2.

Within the PrintPage event logic, you now need to wire up the PageVisual property. In the previous example, you simply set this to the LayoutRoot element, but in this case you want to customize the printed content. To do this, you’ll dynamically create content to set to the PageVisual property. You will create a StackPanel at runtime and add content to that StackPanel for each Contact in your Contacts collection. Add an instance of a StackPanel called customPrintPanel and then add a foreach statement that will step through each Contact in the Contacts collection. Then, within the foreach, create another StackPanel to contain the Contact information. Now add a Margin of 25 to surround the contact panel to prevent the content from appearing too close to the left margin of the printed page, as well as to keep the contacts from all stacking up together. Next insert the logic to add the contact panel to the customPrintPanel. Finally, outside the foreach, set the PageVisual to the customPrintPanel: private void PrintFormatted(object sender, RoutedEventArgs e) { PrintDocument doc = new PrintDocument(); doc.PrintPage += (s, args) => { StackPanel customPrintPanel = new StackPanel(); foreach (Contact c in Contacts) { StackPanel contactPanel = new StackPanel(); contactPanel.Margin = new Thickness(25); customPrintPanel.Children.Add(contactPanel); } args.PageVisual = customPrintPanel; }; doc.Print("Formatted Print"); }

3.

4.

Next you need to add the contact information to the contact panel. In this example, you’ll simply add a TextBlock for each of the contact attributes to display a plain text value: private void PrintFormatted(object sender, RoutedEventArgs e) { PrintDocument doc = new PrintDocument(); doc.PrintPage += (s, args) => { StackPanel customPrintPanel = new StackPanel(); foreach (Contact c in Contacts) {

367 www.it-ebooks.info CHAPTER 15  PRINTING IN SILVERLIGHT

StackPanel contactPanel = new StackPanel(); contactPanel.Margin = new Thickness(25); TextBlock name = new TextBlock(); name.Text = c.Name; contactPanel.Children.Add(name); TextBlock address = new TextBlock(); address.Text = c.Address; contactPanel.Children.Add(address); TextBlock city = new TextBlock(); city.Text = c.CityStateZip; contactPanel.Children.Add(city); TextBlock phone = new TextBlock(); phone.Text = c.Phone; contactPanel.Children.Add(phone); customPrintPanel.Children.Add(contactPanel); } args.PageVisual = customPrintPanel; }; doc.Print("Formatted Print"); } 5. Now press F5 to test the application. When the application shows up, click the Print Formatted button; when you see the Print dialog, select your printer and click Print. If all goes well, the printed output should appear as shown in Figure 15-6.

368 www.it-ebooks.info CHAPTER 15  PRINTING IN SILVERLIGHT

Figure 15-6. Custom print content

Additional Printing Customization
There might be times when you need even more control over the printing process. One such situation is when you want to provide a status notification to the user to indicate when the printing process is taking place, when it completes, and whether it was successful. Earlier in this chapter, we discussed two events, BeginPrint and EndPrint. You can use these events to create code that runs before and after the printing process takes place.

Try It Out: Handling the BeginPrint and EndPrint Events
You’ll continue with the example you’ve been working with throughout this chapter and add handling for the BeginPrint and EndPrint events in order to display a message to the user about the status of the printing process. 1. 2. Let’s keep working with the SimplePrinting project you created earlier. Start by opening the XAML for MainPage.xaml. Add a new TextBlock to the LayoutRoot StackPanel below the panel holding the buttons. Set the Foreground color to Red and the FontWeight to Bold. Name the TextBlock PrintStatus:

369 www.it-ebooks.info CHAPTER 15  PRINTING IN SILVERLIGHT

3. Move to the MainPage.xaml.cs file. You’ll add to your Print Formatted functionality, so your coding will take place within the PrintFormatted event (the click event you added to your button). Below the PrintDocument instantiation, add two lambda expressions to handle the BeginPrint and EndPrint events: private void PrintFormatted(object sender, RoutedEventArgs e) { PrintDocument doc = new PrintDocument(); doc.BeginPrint += (s, args) => { }; doc.EndPrint += (s, args) => { }; doc.PrintPage += (s, args) => { StackPanel customPrintPanel = new StackPanel(); foreach (Contact c in Contacts) { StackPanel contactPanel = new StackPanel(); contactPanel.Margin = new Thickness(25); TextBlock name = new TextBlock(); name.Text = c.Name; contactPanel.Children.Add(name); TextBlock address = new TextBlock(); address.Text = c.Address; contactPanel.Children.Add(address); TextBlock city = new TextBlock(); city.Text = c.CityStateZip; contactPanel.Children.Add(city); TextBlock phone = new TextBlock();

370 www.it-ebooks.info CHAPTER 15  PRINTING IN SILVERLIGHT

phone.Text = c.Phone; contactPanel.Children.Add(phone); } }; } 4. customPrintPanel.Children.Add(contactPanel);

args.PageVisual = customPrintPanel;

doc.Print("Formatted Print");

Next you’ll add code to these two lambda expressions. In the BeginPrint event, you’ll change the Text of the PrintStatus TextBlock you added to “Printing” so that the user can see when the printing process began. In the EndPrint event, concatenate the phrase “Printing Finished!” to the end of the PrintStatus TextBlock. This will tell the user when the printing process is complete. private void PrintFormatted(object sender, RoutedEventArgs e) { PrintDocument doc = new PrintDocument(); doc.BeginPrint += (s, args) => { PrintStatus.Text = "Printing..."; }; doc.EndPrint += (s, args) => { PrintStatus.Text += "Printing Finished!"; }; } ...

5.

Now press F5 to test the application. When the application opens, click the Print Formatted button. When the print dialog opens, select your printer and click Print. You’ll see the status text displayed as shown in Figure 15-7.

371 www.it-ebooks.info CHAPTER 15  PRINTING IN SILVERLIGHT

Figure 15-7. Handling the additional print events

Summary
In this chapter, you looked at the Silverlight printing API. You saw how to easily print content as it appears on the screen, as well as how to print custom content. As you saw, the new printing API lets you add rich printing functionality to your Silverlight applications. In the next chapter, we will take a look at deploying Silverlight applications.

372 www.it-ebooks.info C H A P T E R 16

Deployment
Up to now in this book, I have discussed only the process of developing Silverlight applications. In this chapter, I turn your focus to post development and discuss the topic of deploying your Silverlight applications.

Deploying Silverlight Applications
Once you have finished developing your Silverlight application, you must then face the question of deployment. Luckily, Silverlight deployment is a trivial task that really involves only one concept: XAP files.

XAP Files
When you compile a Silverlight application, the application is packaged into a single file with the extension .XAP. This file is the only thing that needs to be sent to the client in order to run your application. The XAP file itself is really nothing special and is nothing more than a zip file with a special file extension. To prove this, you can simply change the file extension of a XAP file to give it a .ZIP extension. Once the file has been renamed, you can then view the contents of the compressed archive in the file explorer in Windows or in a zip archive tool such as WinZip. The reason Silverlight uses XAP files to package applications is really to provide two benefits. First, by placing your files in a ZIP archive file, your files are compressed when they are deployed and sent to the client, which in turn reduces download times and improves the end-user experience. Second, by placing your entire Silverlight application in one file, it makes the process of deploying your application extremely simple.

Hosting Silverlight Content
To host Silverlight content on your web server, you do not need to be running a Windows server. In fact, just about any web server can serve Silverlight content, as long as it is set to serve up XAP files. In Internet Information Services 7 (IIS7) this is set up by default. So if you are running Windows 2008 Server, your web server is preconfigured to host your Silverlight content. If you are running a version of IIS previous to IIS7 or if you are running on a non-Windows server, you must do some minor configuration to enable the MIME types for the Silverlight extensions. The two MIME types you need to add are described in Table 16-1.

373 www.it-ebooks.info CHAPTER 16  DEPLOYMENT

Table 16-1. The Two MIME Types

Extension
.xaml appli

MIME Type cation/xaml+xml silverlight-app

.xap application/x-

Because there are so many different servers out there, I won’t attempt to show you how to set up this MIME type for each server possibility, so you will need to do some quick research on how to set up MIME types, though it is an extremely common task for server administration.

Application Library Caching
As the previous section described, when you deploy your Silverlight applications, all files for your application are included in a XAP package. This includes any assemblies that are required by your application. For example, your XAP file might look like Figure 16-1, where you can see that a number of assemblies are included in the package, such as System.Windows.Controls.Data.dll. This assembly alone is 128 KB in size, and this amount has to be downloaded to each and every client that runs your application. Furthermore, if there are multiple Silverlight applications that all require the use of the System.Windows.Controls.Data.dll assembly, each one by default will download their own copy of the assembly.

374 www.it-ebooks.info CHAPTER 16  DEPLOYMENT

Figure 16-1. Exploring the contents of a XAP file Application library caching is a new feature added since Silverlight 3 that allows you to cache assemblies locally and share them among different Silverlight applications running on a client machine. Once the files are separated from the application XAP file, their download location can be specified in the AppManifest.xml file. Let’s run through a very quick example to show how assembly caching works and how to activate it in your Silverlight applications.

Try It Out: Exploring Assembly Caching
In this exercise, you will create a simple Silverlight application that includes a number of assemblies. You will then look at the packaged XAP file before and after you activate assembly caching for the application. You will also explore the source changes that take place when using assembly caching. Let’s get started! 1. In Visual Studio 2010, create a new Silverlight application named AssemblyCaching and allow Visual Studio to create a Web Application project to host your application. In MainPage.xaml, make certain your cursor is positioned within the root Grid and double-click on the DataGrid from the toolbox. Note that for the DataGrid,

2.

375 www.it-ebooks.info CHAPTER 16  DEPLOYMENT

you must have a reference to the Silverlight Software Development Kit (SDK) as explained in Chapter 5. After the DataGrid has been added, right-click on it in the design view and select Reset Layout ➤ All. After this, your XAML should look like the following: 3. 4. Build the application by selecting Build ➤ Build Solution from the main menu. Expand the ClientBin directory within the host web application’s directory using Solution Explorer in Visual Studio. There you should find the AssemblyCaching.xap file, as shown in Figure 16-2.

Figure 16-2. Locating your application’s XAP file 5. Change the file name of this file to be AssemblyCaching.xap.zip from within Solution Explorer in Visual Studio to explore the contents. Once the file is renamed, double-click on the file name to open the compressed file in Windows Explorer. You will see the contents as shown in Figure 16-1. You will see that there are many assemblies contained in the *.xap file. Once you have finished inspecting the file, rename it back to AssemblyCaching.xap. From Visual Studio, right-click on the Silverlight application in Solution Explorer and select Properties. On the properties dialog, you will see the Reduce XAP Size By Using Application Library Caching. Select this option, as shown in Figure 16-3, and save your changes.

6.

376 www.it-ebooks.info CHAPTER 16  DEPLOYMENT

Figure 16-3. Enabling assembly caching 7. Rebuild the application and then navigate back to the ClientBin directory. Delete the AssemblyCaching.zip file from the previous step, and then once again, rename the AssemblyCaching.xap file to a *.zip file and open it in Windows Explorer. You will see that there are significantly fewer assemblies contained within the package, as shown in Figure 16-4.

377 www.it-ebooks.info CHAPTER 16  DEPLOYMENT

Figure 16-4. XAP file with assembly caching enabled 8. If you then refresh Solution Explorer and examine the ClientBin folder, you will see that a number of new .zip files have been added, as shown in Figure 16-5. These .zip files contain the assemblies that were removed from the *.xap file. When your Silverlight application needs the specific assemblies, they will download the assembly via the .zip file in the ClientBin.

378 www.it-ebooks.info CHAPTER 16  DEPLOYMENT

Figure 16-5. The ClientBin folder with assembly caching enabled

ENABLING ASSEMBLY CACHING SUPPORT FOR CUSTOM ASSEMBLIES
By default, custom assemblies do not support assembly caching. To quickly see this, add a control from the Silverlight toolkit and then build with assembly caching turned on. You will notice that the toolkit assemblies are not removed from the *.xap. To add support for assembly caching to your custom controls, a number of steps must be completed: • • First, you must assign your assembly using a public key token. This is done using the sn.exe utility. Next, you need to create an external part manifest for your assembly. This is an XML file with the extension .extmap.xml. This manifest contains information that assembly caching needs in order to know where to retrieve the assembly when it is requested by the Silverlight application.

Once you have taken the preceding steps, your custom assembly can take advantage of assembly caching.

379 www.it-ebooks.info CHAPTER 16  DEPLOYMENT

Full-Screen Pinning
Full-screen support has been available in Silverlight for quite some time, but if you are like me and you have multiple monitors, there has probably been a time when you put something in full screen and then changed the focus to another application on your system. If you have indeed tried this, you probably noticed that the Silverlight application that was shown in full screen was no longer in full screen. Silverlight includes a feature called full-screen pinning, where developers can elect to keep their application in full screen, even when it loses focus. If they have enabled this feature for their application, when the user selects full screen the application prompts them with a consent dialog asking if it is OK to remain in full screen, as shown in Figure 16-6. Assuming the user clicks Yes, the application remains in full screen until the user presses escape while focused on the application or until the application removes itself from full screen.

Figure 16-6. Consent dialog for full-screen mode As a developer, to enable full-screen pinning for your application, you simply need to add one line of code to your application, shown in the following code listing. Notice the property FullScreenOptions is set to StaysFullScreenWhenUnFocused. That is all there is to it! public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); App.Current.Host.Content.FullScreenOptions = System.Windows.Interop.FullScreenOptions.StaysFullScreenWhenUnfocused; } private void Button_Click(object sender, RoutedEventArgs e) { App.Current.Host.Content.IsFullScreen = !App.Current.Host.Content.IsFullScreen; }

}

380 www.it-ebooks.info CHAPTER 16  DEPLOYMENT

Out-of-Browser Support
Another feature in Silverlight is the ability to run your Silverlight applications outside the browser. The feature allows users to right-click on a Silverlight application, install it locally to their machine, and execute it without opening their browser. Out-of-browser support is also just as safe and secure as running Silverlight within the browser, as applications that run out of the browser still live within the sandbox. For developers, running an application out of the browser has a number of benefits. The most obvious is that the same XAP runs both in the browser as well as out of the browser. That means you can now develop an application that has identical user experiences in any browser, in any platform, and even outside the browser on any platform. In addition, out-of-browser functionality supports automatic updating of applications, which means even when a user installs the Silverlight application for out-ofbrowser execution, updates still are automatically sent to the user. Developers also have access to an API that allows them to determine when their applications are run out of the browser and modify the behavior of their app however they wish. To enable out-of-browser support for your Silverlight application, the first step is to view the properties of the Silverlight application and select the Enable Running Application Out Of The Browser check box, as shown in Figure 16-7. Note that if you have enabled assembly caching, you will have to disable it to enable out-of-browser functionality.

381 www.it-ebooks.info CHAPTER 16  DEPLOYMENT

Figure 16-7. Enabling out-of-browser functionality for your application When you select the Enable Running Application Out Of The Browser check box, Visual Studio will also change the debug start action to Out-Of-Browser Application. If it does not, you need to do that step manually, by clicking the Debug tab from the project settings and selecting the Out-Of-Browser Application radio button in the Start Action area, as shown in Figure 16-8. Now, when you run your application, you will see that it starts up out of the browser in its own window.

382 www.it-ebooks.info CHAPTER 16  DEPLOYMENT

Figure 16-8. Setting Start Action to Out-Of-Browser Application If you want to see what the end user will see when installing your application locally from the Web, change the Start Action back to Dynamically Generate A Test Page. Then run your application. When the application is displayed inside the web browser, right-click and select Install OutOfBrowser Application Onto This Computer. You will be presented with the default Install Application dialog shown in Figure 16-9. The user has the option to create shortcuts on either the Start menu, Desktop, or both.

383 www.it-ebooks.info CHAPTER 16  DEPLOYMENT

Figure 16-9. Default Install Application dialog After the installation is complete, the application relaunches outside the browser. At this point, the user can reopen the application at any time via the shortcuts she chooses during the installation.

Out-of-Browser API
As mentioned, in Silverlight, applications running out of the browser are running the exact same XAP as the application running in the browser. This is great for developers because you know the user will have the same experience in both situations. However, what if you wanted to change that experience? What if you wanted to change the behavior of some elements of your application in the event that users were running the application out of the browser? In Silverlight, you can work with a number of API methods, properties, and events to customize your application based on its state. One of these properties is the IsRunningOutOfBrowser property. This property returns true if application is running out of the browser and false if it is running within the browser. You can easily add code that looks at this property and executes accordingly: private void Button_Click(object sender, RoutedEventArgs e) { if (Application.Current.IsRunningOutOfBrowser) { OOBStatus.Text = "Application Running Out of Browser!"; } else { OOBStatus.Text = "Application Running In Browser"; } } By adding this code, you can run the application within the browser (as you can see in Figure 16-10) and out of the browser (as shown in Figure 16-11) to see that your application behaves differently depending on its state.

384 www.it-ebooks.info CHAPTER 16  DEPLOYMENT

Figure 16-10. Application running within the browser

385 www.it-ebooks.info CHAPTER 16  DEPLOYMENT

Figure 16-11. Application running out of the browser

Removing Installed Applications
You might be wondering how you can uninstall the Silverlight applications you installed locally. With Silverlight, you can uninstall your applications from Add/Remove programs within Windows, as shown in Figure 16-12. That is all there is to it!

386 www.it-ebooks.info CHAPTER 16  DEPLOYMENT

Figure 16-12. Removing a Silverlight application from Add/Remove programs In addition to using Add/Remove programs, you can also uninstall your application from within the application itself. Uninstalling this way involves only one very simple step. Open the application, rightclick on it, and select Remove This Application, as shown in Figure 16-13. That is all there is to it!

387 www.it-ebooks.info CHAPTER 16  DEPLOYMENT

Figure 16-13. Removing a Silverlight application from within the application

Elevated Trust Applications
The addition of out-of-browser support in Silverlight 3 was among the most popular of the new features in Silverlight. However, many developers asked for more access to client desktops, including device support and hard-drive access. In Silverlight, developers can create applications that request elevated permissions, which provide the client support developers were seeking. For more information on taking advantage of elevated permissions, see Chapter 10, “System Integration and Device Support.” To enable elevated trust for your application, first enable out-of-browser functionality for your application, as shown in Figure 16-14.

388 www.it-ebooks.info CHAPTER 16  DEPLOYMENT

Figure 16-14. Enabling out-of-browser support Next you need to select the Require Elevated Trust When Running Outside The Browser check box, as shown in Figure 16-15.

389 www.it-ebooks.info CHAPTER 16  DEPLOYMENT

Figure 16-15. Enabling elevated trust When users access your application and run it out of browser, they will be prompted with a consent dialog, shown in Figure 16-16., that will ask them if they are OK with granting your application the elevated permissions.

390 www.it-ebooks.info CHAPTER 16  DEPLOYMENT

Figure 16-16. Elevated trust consent dialog

 Note You are able to provide a much friendlier version of the elevated permission consent screen by signing your application with a code-signing certificate. The certificates start at around $100 per year and you can obtain them through many services, such as Go Daddy, Thawte, VeriSign, and Comodo.

Summary
In this chapter, you explored deploying Silverlight applications. As you have seen, deployment in Silverlight is straightforward and trivial, which is yet another benefit of Silverlight applications. You now are able to build your own Silverlight applications from start to finish and deploy them for the entire world to appreciate! Happy Silverlighting!

391 www.it-ebooks.info Index
A
Accordion control categories and books, 136 content item, 133, 137 ContentTemplate, 137 headers, 137, 139 ItemTemplate, 137 layout resetting, 135, 136 library data source binding, 138 ListBox control, 137 Loaded event, 137 namespace reference, 135 TextBlocks, 137 Visual Studio toolbox, 133, 134 ADO.NET Data services, 157 Animations, 315 animated application bar, 316 Begin() method, 319, 320 BeginTime property, 318 Double type, 318 expression blend (see Expression blend) fluid movement, 315 keyframe, 317 linear interpolation, 317 Page constructor, 320 Pause() method, 319, 320 private Boolean property, 320–321 Resume() method, 319, 320 Seek() method, 319 Stop() method, 319, 320 storyboards, 316–317 System.Windows.Media.Animation namespace, 317 TargetName property, 318 TargetProperty, 319 types, 317 Width and Height properties, 316 XAML, 319–320 Application library caching, 376 AppManifest.xml file, 375 AssemblyCaching.xap file, 376 ClientBin directory, 376, 377 ClientBin folder, 378, 379 custom assemblies, 379 DataGrid, 375 root Grid, 375 System.Windows.Controls.Data.dll assembly, 374 XAP file, 378 ASP.NET Web Services (ASMX), 158

B
Background property, 73 Border control, 72 Background property, 73 BorderBrush property, 73 Brush object, 75 CornerRadius property, 73, 74 description, 72 gradient brushes, 76 syntax, 72–73 BorderBrush property, 73 Brush object, 75 btnIncreaseQuota, 232

C
Canvas panel, layout control browser window, 34, 35 button adding, 32, 33 Canvas.Left and Canvas.Top, 30 default Canvas, 31 MainPage.xaml file, 31 output, 33, 34 Web Application project, 31 XAML attached properties, 30 CheckCoolDown() method, 353 CodePlex, 144 ColorAnimation, 317 Content property, 82

393 www.it-ebooks.info ■ INDEX

Control properties, silverlight, 59 attached properties, 60 attribute Syntax, 59 element syntax, 59 type-converter-enabled Attributes, 60 CornerRadius property, 73, 74 Custom controls compiling and testing, 355–356 control appearance Build Action, 347, 348 CoolDownButton namespace, 348 generic.xaml resource dictionary, 346, 347 rectangle components, 348 Storyboard, 349 Style tag, 348 VisualStateGroups, 349 CoolDownButtonControl.cs class, 343 CoolDownButtonTest, 341 CoolDownSeconds property, 345 CoolDownStates, 341 custom functionality, 340 event handling CheckCoolDown() method, 353 control contract, 354 CorePart property setter, 352 GetChildElement() method, 350 GoToState() method, 351 isCoolDown flag, 353 mouse events handling, 351 OnApplyTemplate() method, 350–351 OnCoolDownButtonChange() method, 354 functionality abstraction, 337 functionality creation, 338 functionality modification, 338 NormalStates, 341 onCoolDownSecondsPropertyChanged(), callback method, 345 pressedTime member, 345 public properties, 344–345 Silverlight Class Library template, 341, 342 Silverlight control functionality, 343 Silverlight control model dependency properties, 339–340 “Parts and States” model, 338 version-selection screen, 341, 342

D
Data binding BasicDataBinding, 91 Binding class, 90 Book object, 92 code, 91–92 DataContext property, 93 FirePropertyChanged method, 95–96 implement interface, 94 INotifyPropertyChanged interface, 93 LayoutRoot’s DataContext, 93 Loaded event handler, 92 Mode=TwoWay parameter, 97 OneTime binding, 97 source, 89 string formatting, 120–121 System.ComponentModel namespace, 93, 94 targets, 89 TextBlock, 91 TextBox controls, 90 TextBox instances, 93 two-way binding, 97–98 Data templates class definitions, 122–123 Company class, 122 Contact class, 122 ContactList, 122 finished application, 127 GetContacts() method, 123–124 ImplicitTemplates namespace, 125 multiple object types, 125 namespace name model, 126 Person class, 122 UserContol.Resources tag, 125 DataGrid control AutoGenerateColumns property, 102 built-in intelligence, 101 client-side functionality, 105 column collection AutogenerateColumns property, 107 DataGridCheckBoxColumn, 109 DataGridColumn properties, 108 DataGridTemplateColumn, 109 DataGridTextColumn, 108 column reordering, 105, 107 column resizing, 105, 106 custom columns, 110 Code File, 110 DataGridCustomColumns, 110

394 www.it-ebooks.info ■ INDEX

DataGridTemplateColumn, 113 DataGridTextColumn columns, 114 GetHands() method, 111, 115 grdData, 113 ItemsSource property, 115 StartingHands instances, 111–113 StartingHands.class, 110 TextBlock controls, 113 event handler, 104 GridData class code, 103–104 ItemsSource property, 105 List generic class, 104 ObservableCollection, 103 sorting, 106 System.Windows.Controls.Data, 103 DependencyProperty.Register() method, 340 DoubleAnimationUsingKeyFrames, 318

E
Element-to-Element binding, 99–101 Events handling click property, 64 EventHandlers, 63 InitializeComponent() method, 69 managed code, 67–70 triggered actions, 62 Visual Studio’s automatic creation, 64 XAML event declaration, 63–66 Expression blend, 4, 5, 9 animation workspace objects and timeline panel, 271 Project panel, 269 Properties panel, 269–271 Toolbox, 267–269 BlendStyling, 289 Button controls, 291, 297 editing, 265, 266 file-modification notification, 267 final project, 298 Grid control, 271 grid editor, 290 Grid layout editing BlendLayout, 272 blue grid ruler, 273 column definitions, 273, 277 element, 273 LayoutRoot, 272, 275 nested grid adding, 275 nested grid properties setting, 276

objects and timeline panel, 275 row and column rulers, 275, 277 row definitions, 274, 277 RowDefinition Collection Editor, 278 ShowGridLines property, 278 height property, 297 HorizontalAlignment property, 293, 294 margin property, 295 New Project dialog box, 263, 264 Objects and Timeline panel, 290–293 Orientation property, 296 root Grid modification, 266 Row and Column properties, 295 setting and resetting inline properties, 298 split-view mode, 261, 265 StackPanel layout control, 290 startup screen, 263 Storyboard animation workspace, 324 application layout, 323 ball repositioning, 326 BlendAnimations, 322 Ellipse control, 322 final timeline, 329 finished bouncing ball animation application, 330 keyframe, 326 KeySpline property, 326–328 new storyboard creation, 323 Objects And Timeline panel, 322 playhead position, 325 Rectangle control, 322 Storyboard Resource dialog box, 324 style properties, 296 TextBlock controls, 290, 296 TextBox controls, 296 TextWrapping property, 294 transformation, 333–336 UserControl, 290 VerticalAlignment property, 293 Visual Studio 2010 Integration, 260 visual XAML editor, 260 VSM, 261 world-class timeline, 261 Expression Studio, 6 Extended controls, 83 additional xmlns declaration, 83 description, 83 GridSplitter, 84–87 .xap file, 83

395 www.it-ebooks.info ■ INDEX

F
File explorer application layout AcceptsReturn property, 219 application code, 219 btnOpenFile, 214 btnSave control, 217 Button controls, 212–213 debugging, 216 file-deletion functionality, 214 grid layout, 210, 211 GridSplitter, 210 HorizontalScrollBarVisibility property, 219 ISExplorer, 209 LayoutRoot grid, 209 lblCurrentDirectory, 213 ListBox controls, 212 lstDirectoryListing, 213 Navigate to Event Handler option, 215 nested StackPanel components, 210 TextBlock controls, 211–212 txtContents TextBox, 218 txtFileName, 217 VerticalScrollBarVisibility property, 219 Width property, 209 XamlParseException, 214 coding CreateDirectory() method, 221 CreateFile() method, 222 currentDir, 221 file explorer application, 225, 226 GetDirectoryNames() method, 223 GetFileNames() method, 224 GetStorageData() method, 220, 223, 226 GetUserStoreForApplication() method, 221 IsolatedStorageFile class, 221 Items.Clear() method, 223 Loaded event handler, 220–221 LoadFilesAndDirs() method, 220–221, 223 OpenFile() method, 228 Quota and AvailableFreeSpace properties, 224 Show File button, 227 StreamReader ReadToEnd() method, 228 StreamWriter Write() method, 228 System.IO.Path.Combine() method, 223, 227

System.IO.Path.GetDirectoryName(), 226 isolated storage file explorer demo, 209 testing, 229–230

G
GetChildElement() method, 350 GoToState() method, 346, 351 Grid layout editing BlendLayout, 272 blue grid ruler, 273 column definitions, 273, 277 element, 273 LayoutRoot, 272, 275 nested grid adding, 275 nested grid properties setting, 276 objects and timeline panel, 275 row and column rulers, 275, 277 row definitions, 273, 274, 276, 277 RowDefinition Collection Editor, 278 ShowGridLines property, 278 GridSplitter control, 84 Background property, 85 description, 84 functionality, 87 Silverlight application, 84, 85 Grouping property, 82

H
Handling events, 62

I
In Internet Information Services 7 (IIS7), 373 IncreaseQuotaTo() method, 233 IntelliSense, 14–15 Isolated storage file explorer (see File explorer) quota, increasing, 232–235 System.IO.IsolatedStorage namespace IsolatedStorageFile, 206 IsolatedStorageFileStream, 206 IsolatedStorageSettings, 207–208 viewing and clearing, 230–232

 J, K
JavaScript Object Notation (JSON), 158

396 www.it-ebooks.info ■ INDEX

L
Layout management advantages and disadvantages, 29 canvas panel browser window, 34, 35 button adding, 32, 33 Canvas.Left and Canvas.Top, 30 default Canvas, 31 MainPage.xaml file, 31 output, 33, 34 Web Application project, 31 XAML attached properties, 30 DockPanel control, 55–58 grid control buttons, 46 Grid.RowDefinitions and Grid.ColumnDefinitions, 43 GridPanel, 42 height and width properties, 43 nesting, 46–50 row and column definitions, 43, 44 row and column grid cell locations, 42 ShowGridLines property, 43 spanning, 41 star sizing, 44 table elements, 41 liquid layout, 29 pros and cons, 29 StackPanel control buttons, 39 default stacking orientation, 36 grid element, 36 horizontal and vertical stacking, 36 horizontal orientation, 38 HorizontalAlignment property, 38 MainPage.xaml file, 36 margin property, 38 nesting, 39–41 static layout, 29 WrapPanel control, 50–54 Linux systems, 1 ListBox control default and custom ListBox items, 115–117 GetHands() method, 120 horizontal-oriented StackPanel, 119 ListBoxCustom, 118 namespace, 118 vertical-oriented StackPanel, 119

M
Mac OS X Dock feature, 1 Modal Child Windows Add New Item dialog, 149, 150 Button_Click event handler, 154 Close() method, 151 Closed()event handler, 145 CodePlex, 144 default window, 152 DialogResult property, 151, 154 DialogResult retrieving, 156 margin and padding, 146 modified window, 153, 154 Ms-PL license, 144 registration form, 145 root Grid, 146 Show() method, 145, 151 terms and conditions, 152 TextBlock, 145 true/false/null response, 145 XAML layout, 147–149 Multiple mouse click support, 71 creation, 71–72 description, 71

N
NameProperty, 340 Navigation framework benefits, 186–187 data passing, 190–194 frame and page object, 175 Add Reference dialog, 178 Click attribute, 183 ContentFrame, 181 debugging, 183 Frame’s Navigate method, 183 Grid cells, 177 HorizontalContentAlignment, 181 HyperlinkButton elements, 176, 177, 183 LinkClick, 177, 183 MainPage.xaml file, 176 NavAppFromScratch, 176 Silverlight Page template, 182 solution explorer, 179 System.Windows.Controls.Navigation, 177–179 tag property, 183 template View 1, 184, 185

397 www.it-ebooks.info ■ INDEX

Navigation framework, frame and page object (cont.) template View 2, 186 VerticalContentAlignment, 181 View1.xaml page, 182 View2.xaml, 182 XML namespace, 180 multiple frames, 201–204 NavigationService Object, 187–190 Silverlight navigation application template, 197–201 URI mapping, 194–197 .NET developers, 11

S
ScaleTransform, 331 Silverlight 5 advantages cross-platform/cross-browser support, 6 Expression Studio, 6 .NET framework, cross-platform version, 6 runtime and deployment, 7 Visual Studio integrated development environment, 6 Expression Blend, 4, 5, 9 RIA, 3, 4 SDK, 7 user interface evolution desktop applications, 2 Mac OS X Dock feature, 1 rich and reach application, 3 Windows 7 Aero Snap feature, 2 Windows 7 taskbar, 1 Visual Studio 2010, 8 XAML, 4, 5 Silverlight application element, 171 application library caching (see Application library caching) audio and video stream, 251 clientaccesspolicy.xml file, 170 COM Interoperability, 251–253 crossdomain.xml file, 171 custom controls (see Custom controls) data access ADO.NET Data services, 157 built-in socket support, 157 JSON, 158 pseudo-conversational environment, 157 security restrictions, 157 sockets, 171–173 WCF service (see Windows Communication Foundation (WCF) service) windows-based smart client, 157 deployment, 373–374 drop functionality, 258 AllowDrop property, 253, 254 DragEventArgs.Data property, 254 drop event, 253 DropMe.txt file, 256 SilverlightDropTarget.Web, 254

O
ObjectAnimation, 317 OnApplyTemplate() method, 350–351 OnCoolDownButtonChange() method, 354

 P, Q
“Parts and States” model, 337–339 Printing, 357 API print content determination, 357 PrintDocument events, 357 simplePrinting application (see SimplePrinting application) custom content customPrintPanel, 367 PageVisual property, 367 PrintDocument, 366 printed output, 368–369 TextBlock, 367 customization, 369–372

R
Representational state transfer (REST) services, 158 Rich Internet Applications (RIAs), 3, 4, 157 RotateTransform, 332 RSS 2.0 and Atom 1.0 syndication feed formats, 158

398 www.it-ebooks.info ■ INDEX

StreamReader, 254 .txt extension, 254 elevated trust applications, 388–391 full-screen pinning, 380 element, 171 Hello World sample, 21–26 layout management (see Layout management) navigation framework (see Navigation framework) out-of-browser functionality API, 384–386 application, automatic updation, 381 Default Install Application dialog, 384 Enable Running Application check box, 381 Start Action setting, 382, 383 printing (see Printing) styling (see Styling) toast notification (see Toast notification) uninstall, 386–388 web site, 26–27 webcam/microphone access AudioCaptureDevice, 247 CameraAccess.Web, 245 CaptureDeviceConfiguration Class, 244 CaptureSource Class, 245 CaptureSource named source, 246 consent dialog, 249 Rectangle control, 247 StartCapture delegate, 247 Stop Capture button, 250 Stop method, 248 VideoCaptureDevice, 247 web camera capture application, 249 Silverlight Class Library template, 341, 342 Silverlight controls, 59 border control, 72 Background property, 73 BorderBrush property, 73 Brush object, 75 CornerRadius property, 73, 74 description, 72 gradient brushes, 76 syntax, 72–73 button control, 61–62 dependency properties, 339–340 description, 59 events handling, 62 click property, 64 EventHandlers, 63

InitializeComponent() method, 69 managed code, 67–70 triggered actions, 62 Visual Studio’s automatic creation, 64 XAML event declaration, 63–66 extended controls, 83 additional xmlns declaration, 83 description, 83 GridSplitter, 84–87 .xap file, 83 multiple mouse click support, 71 creation, 71–72 description, 71 nesting controls, 61–62 “Parts and States” model, 338 setting control properties, 59 attached properties, 60 attribute syntax, 59 element syntax, 59 type-converter-enabled attributes, 60 user input controls, 76 CheckBox controls, 80–83 Colors.FromArgb() method, 79 Content property, 82 description, 76 Grouping property, 82 RadioButton controls, 80–83 TextBox control, 77–80 Silverlight navigation application template, 197– 201 Silverlight property system, 339 Silverlight toolkit accordion control categories and books, 136 content item, 133, 137 ContentTemplate, 137 headers, 137, 139 ItemTemplate, 137 layout resetting, 135, 136 library data source binding, 138 ListBox control, 137 Loaded event, 137 namespace reference, 135 TextBlocks, 137 Visual Studio toolbox, 133, 134 AutoCompleteBox, 139–141 experimental quality band, 131 installation, 131–133 mature/SDK quality band, 129–130 Modal Child Windows Add New Item dialog, 149, 150

399 www.it-ebooks.info ■ INDEX

Silverlight toolkit, Modal Child Windows (cont.) Button_Click event handler, 154 Close() method, 151 Closed()event handler, 145 CodePlex, 144 default window, 152 DialogResult property, 151, 154 DialogResult retrieving, 156 margin and padding, 146 modified window, 153, 154 Ms-PL license, 144 registration form, 145 root Grid, 146 Show() method, 145, 151 terms and conditions, 152 TextBlock, 145 true/false/null response, 145 XAML layout, 147–149 preview quality band, 130 stable quality band, 130 TabControl, 141–142 tree-view control, 129 ViewBox, 142–144 Silverlight’s out-of-the-box support, 337 Silverlight-enabled WCF Service, 162 SimplePrinting application Click event, 359–360 ContactGrid, 358 data bound, 361–362 FontWeight property, 358 implicit styles, 358–359 Loaded event, 360–361 PageVisual property, 364 Print dialog box, 364–366 Print() method, 363 printed output, 365, 366 PrintPage event, 363–364 SimplePrinting.Web, 357 string property, 360 System.Windows.Printing namespace, 362, 363 Text property, 358 SkewTransform, 332 Software development kit (SDK), 7 StackPanel control buttons, 39 default stacking orientation, 36 grid element, 36 horizontal and vertical stacking, 36 horizontal orientation, 38 HorizontalAlignment property, 38

MainPage.xaml file, 36 margin property, 38 nesting, 39–41 StaticResource TextBoxStyle attribute, 303 StreamReader ReadToEnd() method, 228 StreamWriter Write() method, 228 Styling application level, 307–308 BaseButtonStyle, 312 cascading style sheets, 299 hierarchy, 310–311 implicit styles, 313–314 inline properties Button controls, 282 code snippet, 281 default input form, 283 expression blend (see Expression blend) final input form, 289 FontFamily, 283 FontSize, 283 FontWeight, 283 height property, 286 IntelliSense, 283 margin property, 283, 288 row and column definitions, 282 style attributes, 284–285, 287–288 styled buttons, 288 styled RowDefinitions, 287 styled TextBlock labels, 284 styled TextBox controls, 286 VSInlineStyling application, 281 Key attribute, 299 merged resource dictionaries, 309–310 Property attribute, 299 RedButton style, 312 static resources breadcrumb, 303 ButtonStyle, 304 Create Style Resource dialog box, 302 Edit Style, 303, 304 initial Silverlight application, 301 Objects and Timeline panel, 300, 302 Resources panel, 306 Setter elements, 304 StaticResource TextBoxStyle attribute, 303 style attributes, 303 TextBoxStyle, 301–304, 306, 307 Width property, 306 element, 299 TargetType attribute, 299

400 www.it-ebooks.info ■ INDEX

Value attribute, 299 XAML markup extension, 299

T
Toast notification Boolean property App.Current.IsRunningOutOfBrowser, 241 Display Notify Window button, 243 Enable Running Application, 242 LayoutRoot grid, 237–239 NotifyApplication, 237 NotifyWindow user control, 238–239, 241 Out-Of-Browser Application, 243 Rectangle control, 239 Set As StartUp Project, 243 TextBlock control, 239 Transformations expression blend, 333–336 matrix, 330 RotateTransform, 332 ScaleTransform, 331 SkewTransform, 332 TranslateTransform, 332 TranslateTransform, 332 Tree-view control, 129

.NET developers, 11 Silverlight application Hello World sample, 21–26 web site, 26–27 source code color-coding, 11 source editor add references performance, 15–16 box selection, 17–19 call hierarchy window, 19 code generation, 20 highlight references, 16–17 IntelliSense, 14–15 zooming out, 13, 14 Visual Studio 97, 12 Visual Studio 2010, 8, 259, 260 Visual Studio integrated development environment (IDE), 6 Visual Studio toolbox, 133, 134 Visual XAML editor, 260 VSInlineStyling application, 281

 W, X, Y, Z
Webcam/microphone access AudioCaptureDevice, 247 CameraAccess.Web, 245 CaptureDeviceConfiguration Class, 244 CaptureSource Class, 245 CaptureSource named source, 246 consent dialog, 249 Rectangle control, 247 StartCapture delegate, 247 Stop Capture button, 250 Stop method, 248 VideoCaptureDevice, 247 web camera capture application, 249 Web service proxy class, 169 Windows 7 Aero Snap feature, 2 Windows Communication Foundation (WCF) service Add Service Reference dialog box, 163, 164 Class Diagram template, 159 Discover button, 164, 165 DoWork() sample method, 163 GetHands() method, 160–162 GetHandsAsync() method, 167, 169 GetHandsCompleted event, 169 List, 160 new class StartingHands.cs file, 159 Object Browser, 166, 167

U
User input controls, 76 CheckBox controls, 80–83 Colors.FromArgb() method, 79 Content property, 82 description, 76 Grouping property, 82 RadioButton controls, 80–83 TextBox control, 77–80

V
Visual Basic 5.0, 12 Visual State Manager (VSM), 261 Visual studio, 94–95 code completion, 11 extension manager, 20 history, 12 IDE, 11 integrated debugger, 11 multimonitor support, 13

401 www.it-ebooks.info ■ INDEX

WCF service (cont.) Page_Loaded event handler, 169 poker starting hands application, 159 Silverlight-enabled WCF Service, 162 StartingHandServiceClient, 166 StartingHandServiceReference, 164, 166 StartingHandService.svc service, 163 template column, 167

text columns, 167 UI columns, 158 WCFService entry, 166 web service proxy class, 169 Windows Presentation Foundation (WPF), 13, 339 Windows-based smart client, 157

402 www.it-ebooks.info Beginning Silverlight 5 in C#
Fourth Edition



Robert Lair

www.it-ebooks.info

Beginning Silverlight 5 in C#
Copyright © 2012 by Robert Lair This work is subj ect to copyright. All rights are res erved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microf ilms or in any other physical way , and transmission or information stor age an d re trieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for ex clusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher's location, in i ts current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. ISBN-13 (pbk): 978-1-4302-3461-6 ISBN-13 (electronic): 978-1-4302-3462-3 Trademarked n ames, logos, an d images may app ear in this book. Rather than us e a trademark s ymbol with every occurrence of a trademarked name, logo, or ima ge we use the names, logos, and images only in a n editorial f ashion 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 ta ken as an expres sion of opinion as to whethe r or not they are subject to proprietary rights. While the advice and inf ormation in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor th e publisher can accept any legal responsibility for any errors or o missions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. President and Publisher: Paul Manning Lead Editor: Ewan Buckingham Technical Reviewer: Todd Meister Editorial Board: Ste ve An glin, Ewan Buckin gham, Gary Corn ell, Louise Corrigan , Morgan Erte l, Jon athan Gennick, Jon athan Hassell, Robert Hutchin son, Michelle Lowman , Jame s Markh am, Matthe w M oodie, Je ff Olson, J effrey P epper, D ouglas Pundick, Ben R enow-Clarke, D ominic Sha keshaft, G wenan Sp earing, M att Wade, Tom Welsh Coordinating Editor: Jennifer L. Blackwell Copy Editor: Roger LeBlanc Compositor: Bytheway Publishing Services Indexer: SPI Global Artist: SPI Global Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Scie nce+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10 013. Pho ne 1 -800-SPRINGER, fax (2 01) 34 8-4505, e-mail orders-ny@springer-sbm.com, or vi sit 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 ti tles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales. Any source code or other supplementary materials ref erenced by the author i n this te xt is av ailable to re aders at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code.

www.it-ebooks.info

To Max, I am so proud of you. –Dad

www.it-ebooks.info

Contents
 About the Author.................................................................................................. xiv  About the Technical Reviewer .............................................................................. xv  Acknowledgments ............................................................................................... xvi  Introduction ........................................................................................................ xvii  Chapter 1: Welcome to Silverlight 5 .......................................................................1 The Evolution of the User Interface....................................................................................1 Rich Internet Application Solutions....................................................................................4 What Is Silverlight?............................................................................................................4 Benefits of Silverlight ........................................................................................................5
Cross-Platform/Cross-Browser Support................................................................................................... 6 Cross-Platform Version of the .NET Framework....................................................................................... 6 Use of Familiar Technologies ................................................................................................................... 6 Small Runtime and Simple Deployment ................................................................................................... 7

The Silverlight Development Environment.........................................................................7 Summary ...........................................................................................................................9  Chapter 2: Introduction to Visual Studio 2010......................................................11 What Is Visual Studio? .....................................................................................................11 What’s New in Visual Studio 2010? .................................................................................12
Support for Multiple Monitors................................................................................................................. 12 Zoom Support for Source Editor ............................................................................................................. 13 Extension Manager................................................................................................................................. 20 v

www.it-ebooks.info

 CONTENTS

Building Your First Silverlight Application in Visual Studio..............................................21
Try It Out: Hello World in Silverlight 5..................................................................................................... 21 Hosting Your Silverlight Application: Web Site or Web Application? ...................................................... 26

Summary .........................................................................................................................27  Chapter 3: Layout Management in Silverlight ......................................................29 Layout Management ........................................................................................................29 The Canvas Panel ............................................................................................................30
Try It Out: Using the Canvas Panel ......................................................................................................... 31 Filling the Entire Browser Window with Your Application ...................................................................... 34

The StackPanel Control....................................................................................................35
Try It Out: Using the StackPanel Control................................................................................................. 36 Try It Out: Nesting StackPanel Controls.................................................................................................. 39

The Grid Control...............................................................................................................41
Try It Out: Using the Grid Control ............................................................................................................ 42 Try It Out: Nesting a Grid and Spanning a Column ................................................................................. 46

The WrapPanel Control ....................................................................................................50
Try It Out: Using the WrapPanel Control ................................................................................................. 51

The DockPanel Control.....................................................................................................55
Try It Out: Using the DockPanel Control.................................................................................................. 55

Summary .........................................................................................................................58  Chapter 4: Silverlight Controls..............................................................................59 Setting Control Properties................................................................................................59
Attribute Syntax...................................................................................................................................... 59 Element Syntax....................................................................................................................................... 59 Type-Converter-Enabled Attributes ........................................................................................................ 60 Attached Properties ................................................................................................................................ 60

Nesting Controls Within Controls .....................................................................................61 vi www.it-ebooks.info

 CONTENTS

Handling Events in Silverlight ..........................................................................................62
Try It Out: Declaring an Event in XAML................................................................................................... 63 Try It Out: Declaring an Event Handler in Managed Code....................................................................... 67

Multiple Mouse Click Support..........................................................................................71
Try It Out: Multiple Click Support............................................................................................................ 71

The Border Control...........................................................................................................72 User Input Controls ..........................................................................................................76
Try It Out: Working with the TextBox Control.......................................................................................... 77 Try It Out: Working with the RadioButton and CheckBox Controls ......................................................... 80

Extended Controls............................................................................................................83
Adding an Extended Control ................................................................................................................... 83 Try It Out: Using the GridSplitter............................................................................................................. 84

Summary .........................................................................................................................87  Chapter 5: Data Binding and Silverlight List Controls ..........................................89 Data Binding ....................................................................................................................89
The Binding Class ................................................................................................................................... 90 Try It Out: Simple Data Binding in Silverlight ......................................................................................... 90

Element-to-Element Binding ...........................................................................................99
Try It Out: Element-to-Element Binding.................................................................................................. 99

The DataGrid Control......................................................................................................101
Try It Out: Building a Simple DataGrid .................................................................................................. 102 The Columns Collection ........................................................................................................................ 107 Try It Out: Building a DataGrid with Custom Columns .......................................................................... 110

The ListBox Control........................................................................................................115
Default and Custom ListBox Items........................................................................................................ 115 Try It Out: Building a ListBox with Custom Content.............................................................................. 117

Data Binding and String Formatting ..............................................................................120 vii www.it-ebooks.info

 CONTENTS

Implicit Data Templates.................................................................................................121
Try It Out: Working with Implicit Data Templates ................................................................................. 122

Summary .......................................................................................................................128  Chapter 6: Silverlight Toolkit ..............................................................................129 Overview of the Silverlight Toolkit.................................................................................129
Mature/SDK Quality Band ..................................................................................................................... 129 Stable Quality Band .............................................................................................................................. 130 Preview Quality Band ........................................................................................................................... 130 Experimental Quality Band ................................................................................................................... 131

Installing the Toolkit ......................................................................................................131 Toolkit Controls..............................................................................................................133
Accordion.............................................................................................................................................. 133 Try It Out: Working with the Accordion Control .................................................................................... 134

AutoCompleteBox ..........................................................................................................139 TabControl .....................................................................................................................141 ViewBox .........................................................................................................................142 Modal Windows .............................................................................................................144
Try It Out: Using the Modal Child Window ............................................................................................ 145

Summary .......................................................................................................................156  Chapter 7: Data Access and Networking ............................................................157 Data Access in Silverlight Applications .........................................................................157 Accessing Data Through Web Services .........................................................................158
Try It Out: Accessing Data Through a WCF Service .............................................................................. 158

Accessing Services from Other Domains.......................................................................170 Accessing Data Through Sockets ..................................................................................171 Summary .......................................................................................................................173 viii www.it-ebooks.info

 CONTENTS

 Chapter 8: Navigation Framework ......................................................................175 Frame and Page Object .................................................................................................175
Try It Out: Creating a Silverlight Navigation Application....................................................................... 176

Benefits of the Navigation Framework ..........................................................................186
Deep Linking......................................................................................................................................... 187

The NavigationService Object........................................................................................187
Try It Out: Using the NavigationService Object..................................................................................... 188

Passing Data to Navigation Pages .................................................................................190
Try It Out: Passing Data to Navigation Pages ....................................................................................... 191

URI Mapping ..................................................................................................................194
Try It Out: URI Mapping and the Navigation Framework ...................................................................... 195

Silverlight Navigation Application Template ..................................................................197
Try It Out: Using the Silverlight Navigation Application Template ........................................................ 197

Using Multiple Frames...................................................................................................201
Try It Out: Using Multiple Frames ......................................................................................................... 202

Summary .......................................................................................................................204  Chapter 9: Isolated Storage in Silverlight...........................................................205 Working with Isolated Storage.......................................................................................205
Using the Isolated Storage API ............................................................................................................. 205 Try It Out: Creating a File Explorer for Isolated Storage ....................................................................... 208

Managing Isolated Storage............................................................................................230
Viewing and Clearing Isolated Storage................................................................................................. 230 Try It Out: Increasing the Isolated Storage Quota................................................................................. 232

Summary .......................................................................................................................235  Chapter 10: System Integration and Device Support.........................................237 Notification (Toast) API ..................................................................................................237
Try It Out: Implementing Toast Notifications ........................................................................................ 237 ix

www.it-ebooks.info

 CONTENTS

Webcam/Microphone Access ........................................................................................244
CaptureDeviceConfiguration Class ....................................................................................................... 244 CaptureSource Class ............................................................................................................................ 245 Try It Out: Accessing a User’s Web Camera and Microphone .............................................................. 245

Working with Captured Streams....................................................................................251 COM Interoperability ......................................................................................................251
Try It Out: Executing an EXE ................................................................................................................. 251

Dropping Files on a Silverlight Application ....................................................................253
Drop Event ............................................................................................................................................ 253 Try It Out: Enabling an Application as a Drop Target............................................................................ 254

Summary .......................................................................................................................258  Chapter 11: Introduction to Expression Blend ....................................................259 Key Features in Expression Blend .................................................................................259
Visual XAML Editor................................................................................................................................ 260 Visual Studio 2010 Integration ............................................................................................................. 260 Split-View Mode ................................................................................................................................... 261 Visual State Manager and Template Editing Support ........................................................................... 261 World-Class Timeline............................................................................................................................ 261 Try It Out: Working with Projects in Expression Blend ......................................................................... 262

Exploring the Workspace ...............................................................................................267
Toolbox ................................................................................................................................................. 267 Project Panel ........................................................................................................................................ 269 Properties Panel ................................................................................................................................... 269 Objects And Timeline Panel.................................................................................................................. 271

Laying Out an Application with Expression Blend .........................................................271
Working with the Grid Control in Expression Blend.............................................................................. 271 Try It Out: Editing a Layout Grid with Expression Blend ....................................................................... 272

x

www.it-ebooks.info

 CONTENTS

Summary .......................................................................................................................279  Chapter 12: Styling in Silverlight........................................................................281 Inline Properties.............................................................................................................281
Try It Out: Setting Inline Properties with Visual Studio......................................................................... 281 Try It Out: Setting Inline Properties with Expression Blend .................................................................. 289

Silverlight Styles............................................................................................................299
Try It Out: Using Styles As Static Resources ........................................................................................ 300 Defining Styles at the Application Level ............................................................................................... 307 Merged Resource Dictionaries ............................................................................................................. 309 Silverlight Style Hierarchy .................................................................................................................... 310 Inheriting Styles Using BasedOn .......................................................................................................... 312 Implicit Styles ....................................................................................................................................... 313

Summary .......................................................................................................................314  Chapter 13: Transformations and Animations ....................................................315 Introduction to Silverlight Animation .............................................................................315
Silverlight Storyboards ......................................................................................................................... 316 Types of Animations in Silverlight ........................................................................................................ 317

Programmatically Controlling Animations .....................................................................319 Using Expression Blend to Create Animations...............................................................321
Viewing a Storyboard in the Expression Blend Timeline ...................................................................... 322 Try It Out: Creating an Animation with Expression Blend ..................................................................... 322

Creating Transformations in Silverlight .........................................................................330
Transformation Types........................................................................................................................... 330 Try It Out: Using Expression Blend to Transform Silverlight Objects .................................................... 333

Summary .......................................................................................................................336  Chapter 14: Custom Controls ..............................................................................337 When to Write Custom Controls.....................................................................................337 xi www.it-ebooks.info

 CONTENTS

Silverlight Control Model ...............................................................................................338
“Parts and States” Model. ................................................................................................................... 338 Dependency Properties . ...................................................................................................................... 339

Creating Custom Controls in Silverlight .........................................................................340
Implementing Custom Functionality . ................................................................................................... 340 Try It Out: Building a Custom Control . ................................................................................................. 340

Summary .......................................................................................................................356  Chapter 15: Printing in Silverlight ......................................................................357 The Printing API .............................................................................................................357
PrintDocument Events . ........................................................................................................................ 357 Determining Print Content . .................................................................................................................. 357 Try It Out: Implementing Simple Printing. ............................................................................................ 357

Printing Custom Content................................................................................................366
Try It Out: Implementing a Custom Print . ............................................................................................ 366

Additional Printing Customization..................................................................................369
Try It Out: Handling the BeginPrint and EndPrint Events . .................................................................... 369

Summary .......................................................................................................................372  Chapter 16: Deployment .....................................................................................373 Deploying Silverlight Applications .................................................................................373
XAP Files. ............................................................................................................................................. 373 Hosting Silverlight Content . ................................................................................................................. 373

Application Library Caching...........................................................................................374
Try It Out: Exploring Assembly Caching. .............................................................................................. 375

Full-Screen Pinning .......................................................................................................380 Out-of-Browser Support ................................................................................................381
Out-of-Browser API . ............................................................................................................................ 384

Removing Installed Applications....................................................................................386 xii www.it-ebooks.info

 CONTENTS

Elevated Trust Applications ...........................................................................................388 Summary .......................................................................................................................391  Index ...................................................................................................................393

xiii

www.it-ebooks.info

About the Author

 Robert Lair has been working with .NET technologies since prior to its alpha when he built IBuySpy, the first-ever demo application used to show off .NET to the development community. He is a published author of many books and magazine articles, including previous versions of Beginning Silverlight. Robert has also been a speaker at a number of .NET technical conferences. Technologies in which Robert specializes include Silverlight, mainframe modernization to .NET, ASP.NET custom application development, SharePoint development and integration, and many related technologies. Today Robert works as the development manager at T3 Technologies (www.t3t.com), a company that offers mainframe alternatives on the Windows platform. Follow Robert on twitter at www.twitter.com/robertlair and on the web at www.robertlair.com/blog.

xiv

www.it-ebooks.info

About the Technical Reviewer

 Todd Meister has been working in the IT industry for over 15 years. He’s been a technical editor on over 75 titles, ranging from SQL Server to the .NET Framework. Besides editing technical titles, he is the Senior IT Architect at Ball State University in Muncie, Indiana. He lives in central Indiana with his wife, Kimberly, and their five loving children.

xv

www.it-ebooks.info

Acknowledgments
I would like to thank the many people at Apress who made this book happen. I would especially like to thank Jennifer Blackwell, Ewan Buckingham, Dominic Shakeshaft, and Todd Meister. Without all of your hard work, this book would never have happened, thank you all. I would also like to thank my family. To my wife, Debi, who has once again sacrificed much during the times when I was busy writing this book. And to my son, Max, who was very understanding when his father couldn’t spend as much time with him.

–Robert Lair

xvi

www.it-ebooks.info

Similar Documents

Premium Essay

College Homework Help

...COLLEGE HOMEWORK HELP Through college homework help, we aim to provide you with all necessary material and solution you require for the understanding of whole of your college’s curriculum. If you are searching for homework help from experts who are well-read in the subjects of mathematics, calculus, statistics and algebra you have come to right place. All scholars are verified degree holders who have years of experience helping students with their maths, accountancy and English problems. Here you can submit anything from simple queries to complex or lengthy assignments and receive expert guidance to help you develop fully-worked solutions. We can assist with any type of project - homework assignments, practice questions, laboratory write-ups, and more. Every subject has its own characteristics like English you need to learn writing skills and in maths you need to learn formulae but every subject demands a single similar thing that is dedication and if you are dedicated to actually learn a subject our experts are equally dedicated to teach you that. Generally, classroom lectures are not enough for the student to be able to cope up with the complexities of problems given for homework, and that is why additional homework help is so essential. College homework help provided by expert and qualified tutors has been always proved to be of immense help and has often been back bone support for students who do well in class. Having problems with your homework or feeling too lazy to figure...

Words: 404 - Pages: 2

Premium Essay

Psy Tut 1

...        PSY1004F/PSY1006F Work piece 3 Biopsychology and Memory Name & Surname:       Student Number:                    Tutorial group:                  Tutor:                      Date:         _________________________________________________________________________________________________ 1. There are a number of clients at the Shady Pines Nursing Home who have suffered neurological damage. Given the limited, and admittedly oversimplified, information provided in your handbook about the patients at Shady Pines Nursing Home, make an educated guess as to the part of the brain that has been damaged. a. Anna: Click here to enter text. b. Lindiwe: Click here to enter text. c. Luke: Click here to enter text. d. Tarryn: Click here to enter text. e. Matthew: Click here to enter text. f. Benson: Click here to enter text. g. Craig: Click here to enter text. [15 marks] 2. Read the examples outlined in your tutorial handbook and for each one (i) identify the probable cause of forgetting, (ii) provide a definition for the probable cause, and (iii) provide an explanation of the cause with reference to the example. ...

Words: 252 - Pages: 2

Free Essay

Apa Assignment

...APA Assignment CSS101 February 13, 2013 The purpose of this essay is to tell you about the resources that are available to me and the amount of help they have provided. I will discuss all of the resources and provide all of the resources and give great information why I have chosen them. There a few program like SHARC, Tutoring Center, and my local library. I will now discuss the information I’ve come across in this essay. At Stevens Henager College that have a program called SHARC. This resource is very helpful and reliable in many ways. I constantly use this resource to help me out on a couple of assignment’s I’ve had problems with. I believe that SHARC is being a crucial part of my college life as I try to peruse my future career. They are there to help in any way they can just to prepare you for your future job. SHARC provides so much important information to help any student succeed. Another resource I truly count on is the tutoring center. They have plenty of certified tutors and tons or other academic tutors and tutor sessions. This tutoring center has helped me so many times on my classes and assignments. I believe that the tutoring center will be a huge influence and a constant stop throughout my college life. It would always be a place to go to get things I’ve had problems with and I know that they’re to help. The last resource I use is my local library. It’s always been a great place to go since I was in high school to get any...

Words: 376 - Pages: 2

Premium Essay

Dennis

... Contents Paper Description and Aims .............................................................................................................. 1 Learning Outcomes .......................................................................................................................... 1 Teaching & Support Staff.................................................................................................................. 2 Course Delivery ................................................................................................................................. 3 i. ii. i. ii. iii. iv. Lectures................................................................................................................................ 3 Tutorials ............................................................................................................................... 3 Prescribed textbook ............................................................................................................. 4 Blackboard ........................................................................................................................... 4 Podcast of lectures ............................................................................................................... 5 Student Webmail.................................................................................................................. 5 Course Learning Resources ......................................................

Words: 3081 - Pages: 13

Premium Essay

Peer Tutoring and the Conversation of Mankind.Doc

...87 .,~. Peer Tutoring and the "Conversation of Mankind" by Kenneth A. Bruffee The beginnings of peer tutoring lie in practice, not in theory. A decade or so ago, faculty and administrators in a few institutions around the country became aware that, increasingly, students entering college had difficulty doing as well in academic studies as their abilities suggested they should be able to do. Some of these students were in many ways poorly prepared academically. Many more of them, however, had on paper excellent secondary preparation. The common denominator among the poorly prepared and the apparently well prepared seemed to be that, for cultural reasons we may not yet fully under­ stand, all these students had difficulty adapting to the traditional or "normal" conventions of the college classroom. One symptom of the difficulty was that many of these students refuSed .help when it was offered. Mainly, colleges offered ancillary programs staffed by professionals. Students avoided them in droves. Many solutions to this problem were suggested and tried, from mandated programs to sink-or-swim. One idea that seemed at the time among the most exotic and unlikely (that is, in the jargon of the Sixties, among the most "radical") turned out to work rather well. Some of us had guessed that students were refusing the help we were providing because it seemed to them merely an extension of the work, the expectations, and above all the social structure of traditional classroom learning. And...

Words: 5727 - Pages: 23

Premium Essay

Void

...MODULE TITLE : Strategic Management CODE : BB309 PRE-REQUISITE : BB112 Fundamental of Management CREDIT HOURS : 3 LECTURER : Remie Johan (remie@ucsiuniversity.edu.my) ASSIGNMENT: CASE STUDY (30%) – British Petroleum Introduction: Students (based on groups) will be given a case prior to the allocated tutorials presentation session. Students are expected to answer all questions listed in the case. This assignment is aimed at providing students the necessary skills in understanding and applying the appropriate management concepts in solving real-life business scenarios. Instructions to students: 1. Work in groups of 5 people (max). Assignments that do not adhere to these group requirements will not be accepted. Students must submit their Group Name List / Assignment Cover Page (Appendix 1) latest by Week 3 (during tutorial). 2. Students must stick to the same tutorial class throughout the semester, as group/ individual activities will be conducted during each tutorial. Group marks will be given during tutorials. Written Report Format • Table of Content 1. Introduction 1. An introduction of the case and concept 2. Content 1. Answer to question # 1 2. Answer to question # 2 3. Answer to question # 3 (if any) 4. Answer to question # 4 (if any) 3. Summary / conclusion about the case | ...

Words: 717 - Pages: 3

Free Essay

Um Papers to Follow

...Click on Content > Course Content and read the Start Here information. * Next, go to Content > Start Here and review the orientation notes. Follow the directions and complete the Classroom Walk through if you have not done so already. Please log in two to three times per week and read the News items here, keep up with the course materials and assignments, and turn in your work on time. Note that all assignments are due Sunday night at 11:59 PM (EST) Required Reading for week 1: * Module 1 * Onboarding for this course and for graduate study at UMUC Assignments for week 1: * Watch Dean Krause's Welcome Video * Post your brief bio in the Introduce Yourself! Discussion * Complete the Academic Integrity Tutorial *...

Words: 575 - Pages: 3

Free Essay

Communications

...WILFRID LAURIER UNIVERSITY WATERLOO, ON COMMUNICATION STUDIES CS 101A — CANADIAN COMMUNICATION IN CONTEXT FALL 2015 Instructor Email Office Location Office Hours Phone Dr. Nathan Rambukkana nrambukkana@wlu.ca DAWB 3-136 Monday 12:00 – 1:00 or by appointment 519 884 0710 x4346 ∆ COURSE FORMAT Lecture Tutorials A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 Thursday 4:00 to 5:50 Location: BA 201 Time Friday 8:30-9:20 Friday 9:30-10:20 Friday 10:30-11:20 Friday 11:30-12:20 Friday 12:30-1:20 Friday 8:30-9:20 Friday 9:30-10:20 Friday 10:30-11:20 Friday 11:30-12:20 Friday 12:30-1:20 Thursday 6:30-7:20 Thursday 7:30-8:20 Thursday 6:30-7:20 Thursday 7:30-8:20 Tutorial Leader Copland, Kayla Copland, Kayla Pope, Amara Brown, Sami Brown, Sami Gupta, Alison Gupta, Alison Naraghi, Erika Naraghi, Erika Pope, Amara Zaman, Syeda Zaman, Syeda Bull, Katie Bull, Katie 1 Locations Seminary S102 Seminary S102 Seminary S102 Seminary S102 Seminary S102 Peters P2027 Peters P2027 Peters P2027 Peters P2027 Peters P2027 Seminary S102 Seminary S102 Peters P2027 Peters P2027 Our time each week will divided between lectures, group discussions and, occasionally, the screening of relevant documentaries/other material. The readings for each lecture should be completed by the time of class. ∆ COURSE OVERVIEW Boilerplate Course Description: An introduction to the key issues in Canadian communication...

Words: 2263 - Pages: 10

Free Essay

Web 236 Learning Consultant - Tutorialrank.Com

...more course tutorials visit www.tutorialrank.com Tutorial Purchased: 5 Times, Rating: A+ WEB 236 Week 1 DQS part 1/3 WEB 236 Week 1 DQS part 2/3 WEB 236 Week 1 DQS part 3/3 WEB 236 Week 1 One Weekly Summary WEB 236 Week 2 DQS part 1/2 WEB 236 Week 2 DQS part 2/2 WEB 236 Week 2 Two Weekly Summary WEB 236 Week 2 Individual Assignment: Website Purpose and Architecture Plan- please add own images WEB 236 Week 3 DQS part 1/2 WEB 236 Week 3 DQS part 2/2 WEB 236 Week 3 Individual Assignment: Website Navigation and Development Plan- Please Add Own Images WEB 236 Week 4 Individual Assignment: Website Content Plan- Please Add Own Images WEB 236 Week 4 Learning Team Assignment: Research and Drafting: Develop Recommendations for Re-Design WEB 236 Week 4 DQS WEB 236 Week 4 Four Weekly Summary WEB 236 Week 5 Learning Team Assignment: Website Evaluation Paper and Prototype for Re-Design ----------------------------------------------------- WEB 236 Week 1 DQS part 1/3(UOP) For more course tutorials visit www.tutorialrank.com Tutorial Purchased: 0 Times, Rating: No Rating WEB 236 Week 1 DQS part 1/3 ------------------------------------------------- WEB 236 Week 1 DQS part 2/3(UOP) For more course tutorials visit www.tutorialrank.com Tutorial Purchased: 1 Times, Rating: A+ WEB 236 Week 1 DQS part 2/3 ----------------------------------------------- WEB 236 Week 1 DQS part 3/3(UOP) For more course tutorials visit www.tutorialrank.com Tutorial Purchased:...

Words: 297 - Pages: 2

Free Essay

Ac 553 Assignment Week 5 – a+ Grade

...20: Corporations may elect a calendar year, Question 22: Corporations are allowed to net long-term Problem 55: a. There are no tax consequences to Susan at transfer b. The corporation will recognize c. Susan basis in Problem 62: The corporation’s taxable This tutorial aims to provide answers of questions given in chapter number 14 of AC 553 Week 5 course. Home Work Hour aims to provide quality study notes and tutorials to the students of AC 553 Assignment Week 5 – A+ Grade in order to ace their studies. AC 553 ASSIGNMENT WEEK 5 – A+ GRADE To purchase this visit here: http://www.nerdypupil.com/product/ac-553-assignment-week-5/ Contact us at: nerdypupil@gmail.com AC 553 ASSIGNMENT WEEK 5 AC 553 Assignment Week 5 – Chapter 14 Question 4: Code Sec. 351 allows Question 20: Corporations may elect a calendar year, Question 22: Corporations are allowed to net long-term Problem 55: a. There are no tax consequences to Susan at transfer b. The corporation will recognize c. Susan basis in Problem 62: The corporation’s taxable This tutorial aims to provide answers of questions given in chapter number 14 of AC 553 Week 5 course. Home Work Hour aims to provide quality study notes and tutorials to the students of AC 553 Assignment Week 5 – A+ Grade in order to ace their studies. AC 553 ASSIGNMENT WEEK 5 – A+ GRADE To purchase this visit here: http://www.nerdypupil.com/product/ac-553-assignment-week-5/ Contact us at: nerdypupil@gmail.com AC 553 ASSIGNMENT...

Words: 545 - Pages: 3

Free Essay

Gen 300 Uop Course Tutorial/Tutorialrank

...GEN 300 Entire Course (UOP) For more course tutorials visit www.tutorialrank.com Tutorial Purchased: 3 Times, Rating: A+ GEN 300 Ethics in an Academic Environment Assignment POWERPOINT ONLY GEN 300 Team Dynamics Instructions GEN 300 Effects of Technology Essay GEN 300 Research,Summary, and Paraphrase Activity GEN 300 Ethics in an Academic Environment Assignment PAPER ONLY GEN 300 Final Paper on Team Dynamics GEN 300 Student Web Scavenger Hunt GEN 300 Week 1 DQs GEN 300 Week 2 DQs GEN 300 Week 3 DQs GEN 300 Week 4 DQs GEN 300 Week 5 DQs ............................................................................................................................................................... GEN 300 Effects of Technology Essay (UOP) For more course tutorials visit www.tutorialrank.com Tutorial Purchased: 3 Times, Rating: A+ Technology and the changes it brings can have a very big effect on our lives. Which technological change has had the largest effect on life in this country? Why? Prepare at least a 350-word essay explaining the technology you have chosen and how it has affected our lives. As you write your paper, remember your essay will be scored based on how well you address the following: Develop a multi-paragraph response to the assigned topic that clearly communicates your controlling idea to the audience. Support your controlling idea with meaningful examples, reasons, and information based upon your research or...

Words: 1275 - Pages: 6

Free Essay

My Year Abroad – Brisbane, Australia 2005/2006

...anyone wanted a housemate. Here, I found two Norwegian girls looking for a third, we started talking and I got the room. Now, I am located in a very modern, pretty, three-room apartment in Kangaroo Point, close to both the city and QUT. The apartment also comes with a shared pool and BBQ-facilities. I pay $130 ($1 = 5 NKr. approx.) pr. week, which is a reasonable price for a place like this. Prices vary depending on what area you want to stay in and how many people you’d like to share with. Electricity comes as an addition every third month, and we also have an internet and telephone line we have to pay. Subjects and timetables You choose your subjects prior to departure, but you still have to confirm them and enrol in lectures and tutorials when you have arrived, the university will help...

Words: 3082 - Pages: 13

Free Essay

Porter Forces

...MARKETING & RETAIL DIVISION MGT9A3: BUSINESS IN THE 21ST CENTURY SCQF Level: 8, SCQF Credit: 22 Pre-requisite: MGT9A2 AUTUMN 2013 Module coordinator: Student support: Teaching Staff: MODULE AIMS Carol Marshall, (3A40) carol.marshall@stir.ac.uk Lynne McCulloch (3B44) l.a.mcculloch@stir.ac.uk K Davies, J Joseph, C Marshall, S Mawson, C Taylor The first two foundation modules examined the firm and the external environment. The emphasis for the module is the management of organisations in the 21st century. As such, this module concentrates on the management of the functional areas of the business, namely operations management, human resource management and marketing within the broader context of the changing organisation and the global economy. MODULE LEARNING OBJECTIVES By the end of this module, through completion of the assessment, attendance at lectures and participation in seminars you will have gained knowledge and understanding of:  the changing nature of the global economy and its impact on management,  operations management and organisational aspects of the business,  the future for managing human resources  the changing nature of the market place, marketing and consumer behaviour Also, you will have developed skills in being able to:  think critically, conceptually and contextually,  select and apply theoretical academic models,  search and select appropriate reading material,  use case studies to apply theoretical models to real life  present...

Words: 2031 - Pages: 9

Free Essay

Communications Styles

...of Phoenix Material Communication Styles Worksheet You spent the past few days exploring the resources available to students at University of Phoenix, and you want to share what you learned with a friend who is interested in enrolling. Write a 150- to 200-word e-mail to your friend summarizing the resources available to students. Hey Lizette I wanted to tell you about all the great resources that are available to students at the University of Phoenix! One thing I like about this school is the great resources online, especially the online school library. In the library you can find all sorts of help. They have a whole section for you to get help on your writing skills and grammar skills. They have everything from videos tutorials to small game writing grammar quiz games. They also have a math department that helps you improve on your math skills and it's called the "Center for Mathematics". In the "Center for Mathematics" you can find live online tutoring! Which means that you will have someone online there with you tutoring you and if you have a question, you can submit your question right there and then and the tutor will answer it. In the University Of Phoenix online library you can also find a program tool called "River point Writer" this tool helps you format your papers. If you feel that you need more help on your writing skills, grammar skills, math skills or anything else the online website has a "Program Section" where you can enroll into free school workshops...

Words: 632 - Pages: 3

Free Essay

Casual vs Academic Writing

...Associate Level Material Appendix A Communication Styles Worksheet You spent the past few days exploring the resources available to students at University of Phoenix, and you want to share what you learned with a friend who is interested in enrolling. * Write a 150- to 200-word e-mail to your friend summarizing the resources available to students. * * Hey Dan! * * I know you’re thinking of goin to UoP. I have been learning about all the resources available. There are lots. * * The CWE is probably the best. It’s located in the library section. It has tutorials, sample papers, and writing & style guides. CWE can help with grammar and spelling, APA formatting and check for plagiarism. * * There’s also a resource for math. Here you’re able to review math topics, complete worksheets and there’s answer booklets. Live math tutors are available to help. There are videos to show how to do math problems. * * There’s a skills & career center too. Here there are resources to help with writing cover letters and resumes. * * There are so many ways to research topics in the library too. There’s a specialized and general resource. Ebooks are available as well as dictionaries, thesis, and encyclopedias. * There’s also videos too. * * There are many on line communities in all areas so you can chat with other students. * So check out UoP! It will be great! * * Lori * * You asked...

Words: 471 - Pages: 2