The RadioButton
The RadioButton also derives from ToggleButton and uses the same IsChecked property and the same Checked, Unchecked, and Indeterminate events. Along with these, the RadioButton adds a single property named GroupName, which allows you to control how radio buttons are placed into groups. Ordinarily, radio buttons are grouped by their container. That means if you place three RadioButton controls in a single StackPanel, they form a group from which you can select just one of the three. On the other...
ASPNET Controls That Use Silverlight
As you saw in Chapter 1, it's easy to build a simple ASRNET web application that includes Silverlight content. You simply need to create a website that includes an HTML or an .aspx test page. Although this approach allows you to place Silverlight and ASRNET pages side by side on the same website, they aren't in any way integrated. You can navigate from one page to another for example, use a link to send a user from an ASRNET web form to a Silverlight entry page , but there's no interaction...
Template Bindings
Although the revised button template respects the content of the button, it ignores most other properties. For example, consider this instance that uses the template lt Button Template StaticResource ButtonTemplate Content A Templated Button Margin 10 This markup gives the button a Margin value of 10 and a Padding of 20. The element that holds the button is responsible for paying attention to the Margin property. However, the Padding property is ignored, leaving the contents of your button...
Dependency Properties
Essentially, a dependency property is a property that can be set directly for example, by your code or by one of Silverlight's services such as data binding, styles, or animation . The key feature of this system is the way that these different property providers are prioritized. For example, an animation will take precedence over all other services while it's running. These overlapping factors make for a very flexible system. They also give dependency properties their name in essence, a...
Dashes
Instead of drawing boring solid lines for the borders of your shape, you can draw dashed lines lines that are broken with spaces according to a pattern you specify. When creating a dashed line in Silverlight, you aren't limited to specific presets. Instead, you choose the length of the solid segment of the line and the length of the broken blank segment by setting the StrokeDashArray property. For example, consider this line lt Polyline Stroke Blue StrokeThickness 14 StrokeDashArray 1 2 Points...
Creating a Deep Zoom Image Set
To get started, load Deep Zoom Composer and click New Project. There are three steps to building a Deep Zoom image set with Deep Zoom composer. First, you import the picture or pictures you plan to use. Next, you arrange the pictures. If you have a single picture, this won't take long. If you have multiple pictures, this is when you tile them together by hand. Finally, you export the Deep Zoom image set and create the Sil-verlight project. You can switch from one step to another using the three...
The LinearGradientBrush
The LinearGradientBrush allows you to create a blended fill that changes from one color to another. Here's the simplest possible gradient. It shades a rectangle diagonally from blue in the top-left corner to white in the bottom-right corner lt Rectangle Width 150 Height 100 gt lt Rectangle.Fill gt lt LinearGradientBrush gt lt GradientStop Color Blue Offset 0 gt lt GradientStop Color White Offset 1 gt lt LinearGradientBrush gt lt Rectangle.Fill gt lt Rectangle gt The top gradient in Figure 8-1...
Regular Expression Basics
All regular expressions are made up of two kinds of characters literals and metacharacters. Literals represent a specific defined character. Metacharacters are wildcards that can represent a range of values. Regular expressions gain their power from the rich set of metacharacters that they support. Two examples of regular expression metacharacters include s which represents any whitespace character and d which represents any digit . Using these characters, you can construct the following...
The Resources Collection
Every element includes a Resources property, which stores a dictionary collection of resources. The resources collection can hold any type of object, indexed by string. Although every element includes the Resources property, the most common way to define resources is at the page level. That's because every element has access to the resources in its own resource collection and the resources in all of its parents' resource collections. So if you define a resource in the page, all the elements on...
The HyperlinkButton
The ordinary Button control is simple enough you click it and it fires a Click event that you handle in code. But what about the other variants that Silverlight offers One of the simplest is the HyperlinkButton. When clicked, it directs the browser to another web page, effectively ending the current Silverlight application. The HyperlinkButton class adds two properties NavigateUri a relative or absolute path that points to a web page and TargetName which, optionally, identifies a bookmark in...
The ContentPresenter
The previous example creates a rather unhelpful button that displays hard-coded text. What you really want to do is take the value of the Button.Content property and display it in your custom template. To pull this off, you need a specially designed placeholder called Content-Presenter. The ContentPresenter is required for all content controls it's the insert content here marker that tells Silverlight where to stuff the content. Here's how you can add it to the current example lt...
The CodeBehind Class
XAML allows you to construct a user interface, but in order to make a functioning application, you need a way to connect the event handlers that contain your application code. XAML makes this easy using the Class attribute that's shown here 1 lt UserControl The x namespace prefix places the Class attribute in the XAML namespace, which means this is a more general part of the XAML language, not a specific Silverlight ingredient. In fact, the Class attribute tells the Silverlight parser to...
T
TabControl class, 148-150 TabControl control, 146 TabControl element, 125 TabIndex property, 121 TabItem element, 148 TabItem.Content property, 149 TabStripPlacement property, 150 Tag property, 294 TagName element, 387 target objects, 436 TargetName property, 139, 246-248, 250-251, 253-254, 256, 263-266 TargetProperty property, 246-248, 250-251, 253-254, 256, 260, 263-266 TCP Transmission Control Protocol , 565-567 TcpListener class, 569, 573 lt td gt tag, 540, 541 Template property, 337-345,...
Attached Properties 1
Chapter 2 introduced a special type of dependency property called an attached property. An attached property is a full-fledged dependency property and, like all dependency properties, it's managed by the Silverlight property system. The difference is that an attached property applies to a class other than the one where it's defined. The most common example of attached properties is found in the layout containers you saw in Chapter 3. For example, the Grid class defines the attached properties...
Animating Brushes
Animating brushes is another common technique in Silverlight animations, and it's just as easy as animating transforms. Once again, the technique is to dig into the particular subprop-erty you want to change, using the appropriate animation type. Figure 9-4 shows an example that tweaks a RadialGradientBrush. As the animation runs, the center point of the radial gradient drifts along the ellipse, giving it a three-dimensional effect. At the same time, the outer color of the gradient changes from...
Interacting with HTML Elements
Silverlight includes a set of managed classes that replicate the HTML DOM document object model in managed code. These classes allow your Silverlight code to interact with the HTML content on the same page. Depending on the scenario, this interaction might involve reading a control value, updating text, or adding new HTML elements to the page. The classes you need to perform all these feats are found in the System.Windows.Browser namespace, and are listed in Table 12-1. You'll learn about them...
Unhandled Exceptions
Although you should use disciplined exception handling code in situations where errors are possible for example, when reading a file, downloading web content, or accessing a web service , it's not always possible to anticipate all sources of error. If your application encounters an error that isn't handled, it will end, and the Silverlight content region will revert to a blank space. If you've included JavaScript code that reacts to potential errors from the Silverlight plug-in as described in...
FrameBased Animation
Along with the property-based animation system, Silverlight provides a way to create frame-based animation using nothing but code. All you need to do is respond to the static CompositionTarget.Rendering event, which is fired to get the content for each frame. This is a far lower-level approach, which you won't want to tackle unless you're sure the standard property-based animation model won't work for your scenario for example, if you're building a simple side-scrolling game, creating...
Row Details
The DataGrid also supports row details an optional, separate display area that appears just under the column values for a row. The row details area adds two things that you can't get from columns alone. First, the row details area spans the full width of the DataGrid and isn't carved into separate columns, which gives you more space to work with. Secondly, the row details area can be configured so it appears only for the selected row, allowing you to tuck the extra details out of the way when...
Defining Columns
Using automatically generated columns, you can quickly create a DataGrid that shows all your data. However, you give up a fair bit of control. For example, you can't control how columns are ordered, how wide they are, how the values inside are formatted, and what header text is placed at the top. A far more powerful approach is to turn off automatic column generation by setting Auto-GenerateColumns to false. You can then explicitly define the columns you want, with the settings you want, and in...
Page Transitions
In Chapter 6, you saw how to support page navigation in a Silverlight application. The basic technique is to use some sort of layout container as your application's root element. You can then add user controls to this container and remove them when needed. Navigating from one page to another consists of removing the user control for the current page and adding the user control for the next page. With this framework in place, it takes just a bit more work to use an animation that switches...
Understanding Silverlight Animation
Often, an animation is thought of as a series of frames. To perform the animation, these frames are shown one after the other, like a stop-motion video. Silverlight animations use a dramatically different model. Essentially, a Silverlight animation is simply a way to modify the value of a dependency property over an interval of time. For example, to make a button that grows and shrinks, you can modify its Width property in an animation. To make it shimmer, you could change the properties of the...
VideoBrush
The VideoBrush is a Silverlight brush that paints an area with the video content that's currently playing in a specified MediaElement. Like other Silverlight brushes, you can use the VideoBrush to fill anything from a basic shape to a complex path or element. The basic approach to using a VideoBrush is straightforward. First, create a MediaElement for the file you want to play lt MediaElement x Name fireMovie Source fire.wmv Height 0 Notice that this example sets the Height and Width of the...
Line Rectangle and Ellipse Geometries
The LineGeometry, RectangleGeometry, and EllipseGeometry classes map directly to the Line, Rectangle, and Ellipse shapes that you learned about in the first half of this chapter. For example, you can convert this markup that uses the Rectangle element lt Rectangle Fill Yellow Stroke Blue Width 100 Height 50 gt lt Rectangle gt to this markup that uses the Path element lt Path Fill Yellow Stroke Blue gt lt Path.Data gt lt RectangleGeometry Rect 0,0 lt Path.Data gt lt Path gt The only real...
Creating the BackgroundWorker
To use the BackgroundWorker, you begin by creating an instance in your code and attaching the event handlers programmatically. If you need to perform multiple asynchronous tasks, you can create your BackgroundWorker objects when needed and store them in some sort of collection for tracking. In the example described here, just one BackgroundWorker is used, and it's created in code when the page is first instantiated. Here's the initialization code that enables support for progress notification...
Video Encoding
To get the best results, you should prepare your files with Silverlight in mind. For example, you should use video files that won't overwhelm the bandwidth of your visitors. This is particularly true if you plan to use large media files for example, to display a thirty-minute lecture . Typically, the WMV files that you use in your Silverlight application will be a final product based on larger, higher-quality original video files. Often, the original files will be in a non-WMV format. However,...
The GridSplitter
Every Windows user has seen splitter bars draggable dividers that separate one section of a window from another. For example, when you use Windows Explorer, you're presented with a list of folders on the left and a list of files on the right . You can drag the splitter bar in between to determine what proportion of the window is given to each pane. In Silverlight, you can create a similar design and give the user the ability to resize rows or columns by adding a splitter bar to a Grid. Figure...
Brushes
As you know, brushes fill an area, whether it's the background, foreground, or border of an element, or the fill or stroke of a shape. For elements, you use brushes with the Foreground, Background, and BorderBrush properties. For shapes, you use the Fill and Stroke properties. You've used brushes throughout this book, but so far you've done most of your work with the straightforward SolidColorBrush. Although SolidColorBrush is indisputably useful, there are several other classes that inherit...
A Reflection Effect
Transforms are important for applying many types of effects. One example is a reflection effect, such as the one demonstrated in Figure 8-12. To create a reflection effect in Silverlight, you need to begin by explicitly duplicating the content that will use the effect. For example, to create the reflection shown in Figure 8-11, you need to begin with two identical Image elements one of which shows the original image and the other which shows the reflected copy lt Grid x Name LayoutRoot...
Simultaneous Animations
The Storyboard class has the ability to hold more than one animation. Best of all, these animations are managed as one group meaning they're started at the same time. To see an example, consider the following storyboard. It wraps two animations, one that acts on the Width property of a button and the other that acts on the Height property. Because the animations are grouped into one storyboard, they increment the button's dimensions in unison. lt Storyboard x Name storyboard lt DoubleAnimation...
Storing a Data Object As a Resource
You have one other option for specifying a data object. You can define it as a resource in your XAML markup, and then alter each binding expression by adding the Source property. For example, you could create the Product object as a resource using markup like this lt UserControl.Resources gt lt local Product x Key resourceProduct ModelNumber AEFS100 ModelName Portable Defibrillator UnitCost 77 Description Analyzes the electrical activity of a person's heart and applies an electric shock if...
Application Shutdown
At some point, your Silverlight application ends. Most commonly, this occurs when the user surfs to another page in the web browser or closes the browser window. It also occurs if the users refreshes the page effectively abandoning the current instance of the application and launching a new one , if the page runs JavaScript code that removes the Silverlight content region or changes its source, or an unhandled exception derails your code. Just before the application is released from memory,...
The RadialGradientBrush
The RadialGradientBrush works similarly to the LinearGradientBrush. It also takes a sequence of colors with different offsets. As with the LinearGradientBrush, you can use as many colors as you want. The difference is how you place the gradient. To identify the point where the first color in the gradient starts, you use the GradientOrigin property. By default, it's 0.5, 0.5 , which represents the middle of the fill region. Note As with the LinearGradientBrush, the RadialGradientBrush uses a...
Using a Deep Zoom Image Set in Silverlight
As you work with Deep Zoom Composer, it creates two folders in your initial project location. One folder, named Working Data, holds temporary files. The second folder, named Source Images, holds the original, imported pictures. It also holds subfolders with the final result of the exporting process. For example, if you create a Deep Zoom project named Panorama, and you use it to export a Silverlight project, your files will end up in the folder Panorama Source Images OutputSdi PanoramaProject...
Understanding Sockets and TCP
Strictly speaking, sockets are nothing more than endpoints on a network. They consist of two numbers IP address. The IP address identifies your computer on a network or the Internet. Port. The port number corresponds to a specific application or service that's communicating over the network. The combination of two sockets one on the client that's running the Silverlight application, and one on a web server that's running a server application defines a connection, as shown in Figure 17-7. Figure...
Configuring Animation Properties
To get the most out of your animations, you need to take a closer look at the seemingly simple animation class properties that were set in the previous example, including From, To, and Duration. As you'll see, there's a bit more subtlety and a few more possibilities than you might initially expect. The From value is the starting value. In the previous example, the animation starts at 160 pixels. Thus, each time you click the button and start the animation, the Width is reset to 160 and the...
Hybrid Pages
Almost all of the examples you've seen in this book have used HTML-only test pages or ASPNET test pages with very little ASPNET content beyond the Silverlight control. However, more ambitious ASPNET developers might use Silverlight to add new functionality or just sugarcoat existing ASPNET pages. Examples include Silverlight-powered ad content, menu systems, and embedded applets like calculators or games . In Chapter 12, you considered a variation of this idea, and built Silverlight pages that...
Defining and Registering a Dependency Property
You'll spend much more time using dependency properties than creating them. However, there are still many reasons that you'll need to create your own dependency properties. Obviously, they're a key ingredient if you're designing a custom Silverlight element. They're also required in some cases if you want to add data binding, animation, or another Silverlight feature to a portion of code that wouldn't otherwise support it. Creating a dependency property isn't difficult, but the syntax takes a...
Supported File Types
Because Silverlight needs to ensure compatibility on a number of different operating systems and browsers, it can't support the full range of media files that you'll find in a desktop application like Windows Media Player. Before you get started with Silverlight audio and video, you need to know exactly what media types it supports. For audio, Silverlight supports the following Windows Media Audio WMA , versions 7, 8, and 9 MP3, with fixed or variable bit rates from 8 to 320 kbps Note Unlike...
The Full Eight Ball Example
Now that you've considered the fundamentals of XAML, you know enough to walk through the definition for the page in Figure 2-1. Here's the complete XAML markup lt UserControl x Class EightBall.Page Width 400 Height 300 gt lt Grid x Name grid1 gt lt Grid.RowDefinitions gt lt RowDefinition Height gt lt RowDefinition Height Auto gt lt RowDefinition Height gt lt Grid.RowDefinitions gt lt TextBox VerticalAlignment Stretch Margin 10,10,13,10 x Name txtQuestion TextWrapping Wrap FontFamily Verdana...
Combining Silverlight and HTML Content
In Chapter 8, you learned how to create a windowless Silverlight content region. You can then use a transparent background to allow your Silverlight elements to sit directly on your HTML page. You can even use partial transparency to let the HTML content show through underneath your Silverlight content. This visual integration comes in quite handy when you use Silverlight code integration. For example, many developers have created custom-skinned media players using Silverlight's standard video...
Complex Properties
As handy as type converters are, they aren't practical for all scenarios. For example, some properties are full-fledged objects with their own set of properties. Although it's possible to create a string representation that the type converter could use, that syntax might be difficult to use and prone to error. Fortunately, XAML provides another option property-element syntax. With property-element syntax, you add a child element with a name in the form Parent.PropertyName. For example, the Grid...
Key Frame Animation
All the animations you've seen so far have used linear interpolation to move from a starting point to an ending point. But what if you need to create an animation that has multiple segments and moves less regularly For example, you might want to create an animation that slides an element into view quickly and then slowly moves it the rest of the way into place. You could achieve this effect by creating a sequence of two animations and using the BeginTime property to start the second animation...
F
F value command, 212 FadeElementEffect class, 279 feeds, 562 FindPrimesCompletedEventArgs class, 518 FindPrimesThreadWrapper class, 517-520 method, 522 FirstDayOfWeek property, 155 Fixed Decimal type, 464 Fixed size option, 85 focus, control, 120-121 Focus method, 387 focus cue, showing, 350-352 FocusStates group, 347-348, 351-352, 358, 368 FontFamily property, 36, 127-129, 131, 143 fonts embedding, 129-130 overview, 127-128 standard, 128-129 FontSize property, 36, 127, 259, 336 FontStretch...
The Messaging Server
Although you can create the messaging server as a separate application, it's tidier to place it in the same application as the policy server. Because the policy server does its listening and request-handling work on separate threads, the messaging server can do its work at the same time. Like the policy server, the messaging server is broken into two classes MessengerServer, which listens for requests and tracks clients, and MessengerConnection, which handles the interaction of a single client....
Marshalling Code to the User Interface Thread
Much like .NET client applications for example, WPF applications and Windows Forms applications , Silverlight supports a single-threaded apartment model. In this model, a single thread runs your entire application and owns all the objects that represent user interface elements. Furthermore, all these elements have thread affinity. The thread that creates them owns them, and other threads can't interact with them directly. If you violate this rule for example, try to access a user interface...
Creating a Thread Wrapper
The previous example shows how you can update the user interface directly from a background thread. However, this approach isn't ideal. It creates complex, tightly coupled applications that mingle the code for performing a task with the code for displaying data. The result is an application that's more complex, less flexible, and difficult to change. For example, if you change the name of the text box in the previous example, or replace it with a different control, you'll also need to revise...
Sending Messages
The messages in the chat application are slightly more detailed than simple strings. Each message includes three details the text, the sender's chosen name, and the sender's time when the message was submitted. These three details are encapsulated in a custom Message class public string MessageText get set public string Sender get set public DateTime SendTime get set public Message string messageText, string sender MessageText messageText Sender sender To send a message, the user enters some...
The CheckBox
Both the CheckBox and the RadioButton are buttons of a different sort. They derive from ToggleButton, which means they can be switched on or off by the user, hence their toggle behavior. In the case of the CheckBox, switching the control on means placing a checkmark in it. The CheckBox class doesn't add any members, so the basic CheckBox interface is defined in the ToggleButton class. Most important, ToggleButton adds an IsChecked property. IsChecked is a nullable Boolean, which means it can be...
The WrapPanel Example
Now that you understand the theory behind dependency properties, it's time to ground your knowledge in a realistic example. In Chapter 3, you saw how to create a custom layout container called the WrapPanel, which flows elements over multiple lines to fit the available space. By adding properties to the WrapPanel, you can customize the way it works. Because the WrapPanel is a Silverlight element, its properties should almost always be dependency properties, which gives them the flexibility to...















