Empowering Richer Experiences

Hardware Accelerated PlaneProjection

The PlaneProjection can now be hardware accelerated like the other transform types (Scale, Skew, Rotate, and Translate) in Silverlight.

Word Based Text Trimming (trailing ellipses)

The TextBlock has a new TextTrimming property that accepts either of the TextTrimming enumerator values of None or WordEllipsis. When this property is set to WordEllipsis and the text in the TextBlock exceeds the visible limit, the text appears truncated with a trailing three ellipsis. The example below shows the XAML for two TextBlocks with the same text. The first TextBlock sets the TextTrimming to WordEllipsis while the second TextBlock does not.


<TextBlock Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" Text="The quick brown fox jumped over the tall white fence" TextTrimming="WordEllipsis" Width="120" /> <TextBlock Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" Text="The quick brown fox jumped over the tall white fence" Width="120" />

When the application runs, the TextBlock controls appear as shown below:

Figure 17

TextTrimming with WordEllipsis

Implicit Styles

Silverlight 4 introduces new styling features that allow you to create a style as a resource that can be used implicitly by all elements of a target type. This allows application developers to customize the look across multiple instances of a control and modify the appearance of these control instances by changing the implicit style.

An implicit style can be created by creating a style, omitting the x:Key name, and setting the TargetType. The XAML below shows an implicit style for all Button controls that makes the foreground red. When this implicit style exists for the Button control, it will be applied automatically without having to set the style to each button.


<Style TargetType="Button"> <Setter Property="Foreground" Value="Red" /> </Style>

Implicit styles can be used to declare a style and apply it to a particular control as shown above or to style commonly used properties such as FontFamily, FontSize, Foreground (font color), and Background. If you create an implicit style at the application level, whether embedded in the App.xaml or in a resource dictionary, the style will be applied to all controls that match the target type within your application.

MouseWheel Support

Silverlight has support for responding to mouse wheel events on controls where scrolling is appropriate. The Silverlight ScrollViewer automatically handles the MouseWheel event so the ListBox, ComboBox, TextBox, and ScrollViewers automatically scroll. For example, when the mouse wheel scrolls over a ListBox, the ListBox begins to scroll. Silverlight can also use MouseWheel events to perform custom functionality. For example, you can configure the MouseWheel event so that when a user turns the mouse wheel the application zooms in and out using a scale transform.

The MouseWheel event exists on all UIElements. This event fires when the mouse wheel is turned while the mouse pointer is over a UIElement. The MouseWheelEventArgs expose a Handled property. Since this event occurs synchronously it can be marked as handled to prevent the HTML event from being raised. The Delta property of the MouseWheelEventArgs indicates the relative change that occurred in the mouse wheel rotation. A positive Delta indicates a forward turn of the mouse wheel while a negative Delta indicates a backward turn.

The snippet below demonstrates event handler coding for the MouseWheel event. This handler applies the turns in the mouse wheel to a slider control.


private void OnMouseWheel(object sender, MouseWheelEventArgs e) { if (e.Delta > 0) slider_X.Value += slider_X.LargeChange; else slider_X.Value -= slider_X.LargeChange; }

Right Mouse Click

Silverlight 4 adds the ability to right click a visual element to perform custom actions such as displaying a menu that provides contextual commands or invoking commands for a Silverlight game. The MouseRightButtonDown and the MouseRightButtonUp events are now exposed on all UIElement controls. These events allow you to handle the right-mouse-click events and display a custom context menu instead of the default Silverlight menu. The events are routed to a particular element and use the same MouseButtonEventArgs that the MouseLeftButtonUp and MouseLeftButtonDown events use so you can determine which element was affected and where on that element a right mouse button event occurred in order to show a context menu at that location.

When the MouseRightButtonDown event is handled and the MouseButtonEventArgs.Handled property is set to true, the default Silverlight configuration menu will not be displayed. The MouseButtonEventArgs.StylusDevice.DeviceType property will report the source device type for the event such as mouse, stylus, or touch.

Figure 18

Right Click Displaying a Custom Context Menu

The code example below shows how to implement the right click functionality and display a custom menu control.


public MainPage() { InitializeComponent(); mediaElement.MouseRightButtonDown += new MouseButtonEventHandler(MouseRightButtonDownHandler); mediaElement.MouseRightButtonUp += new MouseButtonEventHandler(MouseRightButtonUpHandler); } private void MouseRightButtonDownHandler(object sender, MouseButtonEventArgs e) { e.Handled = true; } private void MouseRightButtonUpHandler(object sender, MouseButtonEventArgs e) { var menu = new MyCustomMenuControl(mediaElement); menu.Show(e.GetPosition(LayoutRoot)); }

Programmatic Clipboard Access

Silverlight 4 adds the ability to programmatically access the clipboard to format and modify data during copy, cut, and paste operations. To copy data from a Silverlight application to the clipboard use the Clipboard.SetText method:



Once the text is copied to the clipboard, it can be retrieved from the clipboard using the Clipboard.GetText method:


rtb.Selection.Text = Clipboard.GetText();

This effectively performs a copy and paste operation. To perform a cut operation, simple use the Clipboard.SetText method and set the source of the cut operation to an empty string.


Clipboard.SetText(rtb.Selection.Text); rtb.Selection.Text = "";

The SetText and GetText methods of the clipboard object can only be invoked in the context of a user initiated action.

When an attempt is made to programmatically access the clipboard for the first time, Silverlight will prompt the user to request permission.

Figure 19

Clipboard Access Prompt

Silverlight as a Drop Target

Silverlight now supports being a drop-target for the common drag-and-drop practice of clicking on a virtual object and dragging it to a different location or onto another virtual object. Both Windows and Mac platforms support this feature. Silverlight only supports dragging of file(s) from local client and dropping them onto a Silverlight application. It does not support dragging of folders (directories). If multiple files need to be dragged, you can select those files and drag them as a group. You must forward the relevant DOM events from HTML-level scripting to the specific API of the control (see attached doc).

The AllowDrop property of the UIElement must be set to true and its Visibility property must be set to Visibility.Visible for the UIElement to be a drop target. Once these are set, the following events can be handled to implement the drop target features:

  • DragEnter – Fires when the source is dragged from the local client and it enters the boundary of the UIElement that is the drop target.
  • DragLeave – Fires when the file(s) being dragged from the client’s local files leaves the boundary of the UIElement
  • DragOver – Fires when the input system reports a drag event with this element as the potential drop target.
  • Drop – Fires when an object is dropped within a UIElement’s boundary.

When handled, these events contain a DragEventsArg that contains a Data property of type IDataObject. The Data property contains information associated with the corresponding drag or drop event. Data will return null when it is accessed outside the context of the OnDrop event and when the item dropped is not a file. When a valid drop has occurred you can access the list of file(s) that were dropped using the GetData method and passing it the DataFormats.FileDrop enumerator. This returns an array of files that can then be accessed for file name, size, and be opened if needed.


IDataObject data = e.Data; if (data.GetDataPresent(DataFormats.FileDrop)) { FileInfo[] files = data.GetData(DataFormats.FileDrop) as FileInfo[]; foreach (var file in files) { //FileStream reader = file.OpenRead(); } }

Handle Drag-and-Drop Events for Macintosh

Drag-and-drop events for applications hosted by Safari on Macintosh require that you forward the relevant DOM events from your own HTML-level scripting to the specific API of the control. This is a requirement because the plug-in model for this architecture does not provide these events to plug-ins such as the Silverlight runtime. The following is an excerpt of the embed/object tagging you would use to define DOM event handlers for the HTML element that instantiates the Silverlight plug-in:


<embed id="AgControl1" ... ondragenter="handleDragEnter(event)" ondragover="handleDragOver(event)" ondragleave="handleDragLeave(event)" ondrop="handleDropEvent(event)" .../>

The following is an example of an event handler that you write in JavaScript to account for event forwarding of drag-drop events on Safari / Macintosh. You should write one handler for each of the drag-related DOM events dragEnter, dragLeave, dragOver, and drop. This is the example for dragEnter. Note the getElementById call to return the Silverlight plug-in instance, which is the object where you forward the HTML DOM level handling to.


function handleDragEnter(oEvent) { // Prevent default operations in DOM oEvent.preventDefault(); agControl = document.getElementById("AgControl1"); var flag = agControl.dragEnter(oEvent); // If handled, then stop propagation of event in DOM if (flag) oEvent.stopPropagation(); }
The FireFox architecture for Mac OSX distributions of FireFox restricts access to object-level access to the drop event. For this reason, the workaround suggested above for Safari might not function for FireFox.

Webcam and Microphone Support

Silverlight now has webcam and microphone support. Through its device capture API Silverlight can determine which video and audio devices are connected to the computer. The API exposes features to retrieve the devices, prompt the user for permission to access the devices, and use to the devices to capture audio and/or video.

The System.Windows.Media namespace exposes several classes for hardware device capture. The CaptureDeviceConfiguration class’s AllowedDeviceAccess property returns true if the user has already granted access to the devices during the current session. The CaptureDeviceConfiguration.RequestDeviceAccess method requests access to the devices from the user. This will display a dialog to the user asking for permission, as shown below.

Figure 20

Device Access User Prompt

Once access has been granted to the devices they can be retrieved using the API. The CaptureDeviceConfiguration class’s GetDefaultVideoCaptureDevice method gets the default video device and the GetDefaultAudioCaptureDevice method gets the default audio device. Once the video device is retrieved, the video capture can be initiated by creating a new instance of the CaptureSource class and setting its VideoCaptureDevice property to the video device. At this point, the video capture can be started by using the CaptureSource’s Start method. The code sample below shows the basic steps to request access to the devices, begin the video capture process, and paint the video using a VideoBrush.


if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess()) { VideoCaptureDevice vcd = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice(); AudioCaptureDevice acd = CaptureDeviceConfiguration.GetDefaultAudioCaptureDevice(); if (null != vcd && null != acd) { cs = new CaptureSource(); cs.VideoCaptureDevice = vcd; cs.AudioCaptureDevice = acd; cs.Start(); VideoBrush videoBrush = new VideoBrush(); videoBrush.Stretch = Stretch.Uniform; videoBrush.SetSource(cs); TO_FILL.Fill = videoBrush; } else MessageBox.Show("Error initializing Webcam or Microphone."); }

The CaptureSource class points to specific devices and starts and stops capture. The CaptureDeviceConfiguration class provides the CaptureSource with the configured VideoCaptureDevice and AudioCaptureDevice. Set the default video and audio devices for the Silverlight application by right clicking the Silverlight application and going to the Webcam/Mic tab as shown below.

Figure 21

Default Capture Devices

Silverlight 4’s configuration window adds new support for webcam and microphone previews, as shown in the figure above. Notice that the figure shows the selected webcam and microphone both displaying a preview of their inputs.

The capture process can be used to grab a bitmap from the webcam. This can be done by first handling the CaptureImageCompleted event and then executing the CaptureImageAsync method. The code sample below shows the capture of an image from the CaptureSource.


cs.CaptureImageCompleted += (s, pe) => { var bmp = pe.Result; ImageBrush brush = new ImageBrush(); brush.ImageSource = bmp; brush.Stretch = Stretch.UniformToFill; someBorder.Background = brush; }; cs.CaptureImageAsync();

Webcam and Microphone Permissions

Silverlight 4’s configuration window also adds new support for viewing permissions. Figure 16 shows that the webcam and microphone access has been requested and allowed by the user. If a user decides they want to change the permissions they can allow, deny or simply remove the permissions from this tab of the configuration window.

Figure 22



Silverlight adds a CompositeTransform type that enables the creation of transform groups with one XAML node. This new transform type reduces XAML and collapses all of the types of transform that can be in a TransformGroup into a single node that applies the combined transform in the common order.

For example, this XAML as written in Silverlight 3:


<Rectangle Height="100" Width="100" Fill="Red"> <Rectangle.RenderTransform> <TransformGroup> <ScaleTransform ScaleX="2"/> <RotateTransform Angle="45"/> <TranslateTransform Y="42"/> </TransformGroup> </Rectangle.RenderTransform> </Rectangle>

could instead be written like this:


<Rectangle Height="100" Width="100" Fill="Red"> <Rectangle.RenderTransform> <CompositeTransform ScaleX="2" Rotation="45" TranslateY="42"/> </Rectangle.RenderTransform> </Rectangle>

Support for all PNG Formats

Silverlight 4 adds support to display all PNG image formats with proper transparency.

Offline Digital Rights Management

Silverlight now allows you to use Digital Rights Management (DRM) in content that your Silverlight applications can access offline. This allows you to better protect and more securely deliver content cross-platform for a variety of scenarios, including streaming, progressive download, rentals, and subscriptions. The Silverlight client can use two forms of DRM protected content: the traditional Windows Media Digital Rights Management 10 (WMDRM 10) and the newer PlayReady that uses the Advanced Encryption Standard (AES).

The next major wave of PlayReady innovation being built into Silverlight focuses on meeting the top media customer ask for the Silverlight DRM client – support for Offline scenarios.  The three key business models targeted for this release of the Silverlight DRM client are Rental, Subscription, and Purchase.  The Silverlight PlayReady ecosystem has several features that are valuable for these business models.

  • PlayReady licenses can be persisted for use by the Silverlight Offline client when accessing PlayReady protected content in the disconnected state (offline).
  • Content access policies such as an Expiration Date and an Expire-after-first-play time value support for rental and subscription scenarios
  • Managed APIs to request a license given only the content header or a key identifier support for the pre-fetching of licenses
  • Support for license chaining can improve the customer experience during subscription renewal by only requiring one license, the root license, to be renewed in order to light up their entire library of subscription content bound to that root license
  • PlayReady Domain support to enable content portability and limiting playback to a certain number of PlayReady clients

MP4 Playback Protected DRM

Silverlight 4 can play back H264 and AAC-LC content protected using PlayReady DRM via Silverlight’s MediaStreamSource.

WMS Multicast

Silverlight’s MediaElement can now take in a Windows Media Station file (.nsc) to enable delivery of media files via multicast, which saves bandwidth on multicast capable networks. Multicast streaming is very similar to traditional over-the-air radio or TV broadcast where a single signal is transmitted and individual receivers “tune in” to the program.  WMS Multicast is compatible with existing WMS-based Multicast for easy migration from WMP to Silverlight. It is the lowest cost and the most scalable of all streaming types. In IP Multicast, routers that are multicast-enabled replicate the media stream and broadcast it over a network port where clients can tune in. 

Multicast streaming is the most efficient way to deliver audio and video over a network as it scales in a non-linear way.  That is, each new viewer does not increase the amount of bandwidth required. In Silverlight 3, Multicast is enabled only through an open source plug-in developed in cooperation with Microsoft by Qumu.  Silverlight 4 does not break compatibility with the plug-in although the plug-in is now unnecessary.

Output Protection

Silverlight 4 adds the capability to read Output Protection policies inside of PlayReady licenses and to engage output protections based on those policies. This enables content providers and developers to protect content from the output ports of the Graphics card to Input port of a display device.

Parser Enhancements

The parser in Silverlight 4 adds performance and stability improvements to the parser engine, support for setting order dependent properties, and the XmlnsPrefix and XmlnsDefinition attributes. New valid XAML constructs are now accepted and there is better validation of XAML errors.  For example, direct content is now handled properly, making the following XAML behave just as it would in WPF: “<Button>Hello, World!</Button>”.   In addition, custom dictionaries are now supported in Silverlight XAML (using x:Key to add items).

The new parser provides significant improvements to runtime error reporting in XAML.  With the newly rearchitected parser, far more error messages provide useful information about what went wrong and on what line/column the error occurred within the XAML document.

The parser has also added support for XmlnsDefinition, allowing you to eliminate the plethora of xmlns declarations in every XAML document.  You can take advantage of intellisense across all of your referenced control libraries and namespaces using a single XML namespace.  The Silverlight SDK and Toolkit have been updated to take advantage of this functionality, meaning any toolkit control can be accessed with the prefix “toolkit:” (or “sdk:” for SDK controls), and only one xmlns declaration at the top of your document.

Support has been enabled for components that implement ISupportInitialize, too. This allows such components to be notified when the parser is done setting properties so that order-dependent property setting can be handled appropriately.

Deep Zoom

Deep Zoom’s code base has been updated with support for hardware acceleration, resulting in increased performance.

Google Chrome Support

Silverlight 4 adds support for Google Chrome browser, in addition to the Safari, FireFox and Internet Explorer.

Private Mode Browsing

Private mode browsing, supported by many web browsers, is now supported by Silverlight 4.

Pinned Full-Screen Mode on Secondary Display

Silverlight 4 now supports pinning an application to full screen mode when the application is not in focus. This is useful when you have more than 1 display monitor and you are watching a video in full screen mode on a monitor and interacting with other applications in another monitor. The following code sets the application to allow full screen when not in focus.


Application.Current.Host.Content.FullScreenOptions = FullScreenOptions.StaysFullScreenWhenUnfocused; Application.Current.Host.Content.IsFullScreen = true;

Media Updates

Several updates have been made to the media stack in Silverlight 4. Silverlight 4 provides more descriptive MediaSourceStream errors. Instead of seeing 4001 AG_E_NETWORK error for the MediaStreamSource, you will now have the improved support for seeing information about the error that is more relevant to the media pipeline.

Content protection updates are included in Silverlight 4. The MediaSourceStream is able to use the sub sample encryption mechanism for protecting Mp4 content as described in the PIFF specification. Also, there is now support for the CGMS-A analog output protection mechanism.

As an additional layer of security when engaging output protections as required by content providers, the graphics card will be checked to make sure it is appropriately up-to-date.

Digital Constraint Token support exists as a new output protection policy that is based on both the frame quality (High-Def or Standard-Def) as well as the desired protection level. This allows content owners to protect their content under adaptive streaming.