Rules To Better WPF And Silverlight

Since 1990, SSW has supported the developer community by publishing all our best practices and rules for everyone to see. 

If you still need help, visit ​Windows desktop applications | Windows 8 store applications & WPF​​ and book in a consultant.​​​

Hold on a second! How would you like to view this content?
Just the title! A brief blurb! Gimme everything!
  1. Do you follow Composite Application Guidance (formerly code-named "Prism") in your Silverlight (and WPF) Projects?

    The Composite Application Guidance from the patterns & practices team at Microsoft is designed to help you more easily build loosely coupled, independently evolvable modular WPF and Silverlight client applications which take advantage of the capabilities of WPF and Silverlight.
    With Composite Application Library (CAL), you can only adopt the components your application requires. For example, you could change your communication approach from .NET Framework events to use the Event Aggregator, which allows you to send loosely coupled messages between modules.
    Composite Application Guidance
  2. Do you use dynamic application loading in Silverlight?

    ​In a large business application with different and distinct modules, often it will make sense to build the separate modules and load them dynamically using a combination of Inversion of Control (IoC) and Silverlight's Dynamic Assembly Loading.
    There are a few huge wins for a customer when you build your line of business application this way:
    1. The initial load isn't as large - you can have a quick load of a login and common UI, then as the person logs in, prepare their environment by loading additional components
    2. You can show a quick "Do you know… you can do XXX" while this is loading. You can also show them a quick list of what has been happening (e.g. a Dashboard).
    3. You can also implement special cases where perhaps HR and Finance both use the same area, but with different customizations. So based on who logs into the system, Silverlight can dynamically load a different assembly giving them a different behaviour.
    4. You can also build simple "Plug-in" like functionality, allowing simple customizations of your application based on your own published interface API.

    This works really well with Silverlight's Application Caching as well - and lets you build and release different component modules separately.

    You can find out more about Silverlight's Dynamic Assembly Loading here:

  3. Do you avoid using Thread.Sleep in your Silverlight application?

    ​Calling Thread.Sleep on your Silverlight application causes the UI thread to sleep. That means the application is not responsive.
    If you want to delay something, you can use a storyboard.
    this.Dispatcher.BeginInvoke(new Action(() =>
                             //Try to reconnect in the background
    Bad: Using Thread.Sleep() causes your Silverlight application to freeze 
    Storyboard sb = new Storyboard() { Duration = TimeSpan.FromSeconds(5) };
    sb.Completed += (ds, de) => this.Dispatcher.BeginInvoke(new Action(() =>
                                                                              //Try to reconnect in the background
    GOOD: Use a Storyboard with a duration of the delay and once the Storyboard is finished running
  4. Do you offer out-of-browser support?

    Silverlight out of browser enables the application to be placed in a restricted store on the user’s machine; and then provide a link directly to it from the user’s desktop or start menu. This is all enabled within Silverlight without any additional download of runtime or the need to write applications in a different way.

    An application can now be easily found on the user’s desktop or start menu, and launched with a single click. However is not easy to do, the main difficulty, is that you do not have the HtmlPage object, so if you were relying on IFrame pop up or javascript, or if you are using Silverlight's navigation framework (e.g. silverlight.aspx#Contacts/JohnDoe).

    Figure: OOB demo from

  5. Do you use the MVVM pattern in your Silverlight and WPF Projects?

    ​​The term MVVM means Model-View-ViewModel design pattern. This pattern is an adaptation of the MVC and MVP patterns in which the view model provides a data model and behavior to the view but allows the view to declaratively bind to the view model. The view becomes a mix of XAML and C# (as WPF or Silverlight controls), the model represents the data available to the application, and the view model prepares the model in order to bind it to the view.
    The most important aspect of WPF or Silverlight that makes MVVM a great pattern to use is the data binding infrastructure. By binding properties of a view to a ViewModel, you get loose coupling between the two and entirely remove the need for writing code in a ViewModel that directly updates a view. In a sense, Views and unit tests are just two different types of ViewModel consumers. Having a suite of tests for an application's ViewModels provides free and fast regression testing, which helps reduce the cost of maintaining an application over time.
    A good article about MVVM
  6. When do you use Silverlight?

    There is a place for Silverlight, but it can only be the 'richer' experience. In some case it is better not to use Silverlight for Data Entry Form and also content publisher should have alternative of Silverlight for Andriod & iOS user.

    Mobile Device:

    Silverlight in iPad doesn't work
        Figure: Silverlight does not work on an iPad, It can't be only experience. 

    Further Reading: PCWorld  . 

    Data Entry Forms
    NO. HTML must be the default experience. Microsoft says for Business Apps, but be careful. Silverlight is *Not* for data entry forms. Tab Index Implement is very hard and end result isn’t consistent among popular browser. Users frequently uses tab to move from Text Box to another Text Box and require consistence experience while entering data using different browser. HTML 5 introduced autofocus, placeholder and validation feature which is useful for creating consistent Data Entry form for various browser.
     Useful Links: HTML 5 Form Elements 
    Microphone or Camera
    YES, You can access camera & microphone from Silverlight. System.Windows. Media.CaptureSource object provides methods and properties used to work with audio and video capture devices. In essence, CaptureSource is like a little media player. After video being captured, We can give ShaderEffect  or manipulate captured still image.
    Useful links: Learning Video
    Socket Communication
    YES. System.Net.Sockets.Socket Class provides a set of methods and properties for network communications. The Socket class allows you to perform asynchronous data transfer using any of the communication protocols listed in the ProtocolType enumeration. Currently, the only supported ProtocolType is the TCP protocol. Supported Version Silverlight 4 & 3 . MSDN – Socket Class 
    For interactive charts in a BI (Business Intelligence) solution
    NO. There are some aftermarket solutions available. Notable are Telerik Silverlight Control , Infragistics   Silverlight Data Visualization. Telerik   provides BI solution using Advanced GridView, Charts with zooming, scrolling, multi-level resource grouping. Both solutions use shared API across Silverlight and WPF, as a result product user can visualize data in more flexible way.
    For Richer TextBox
    YES. Using Silverlight Rich TextBox user can format Text, insert image, DataGrid, Calendar, display Text right-to-left, print content and can access clipboard. Accessing Clipboard   can be implemented by using Clipboard Object. Selected Text context can be implemented using Popup   Control.  Sample in MSDN  
    For mapping apps
    YES. Microsoft.Maps.MapControl provides functionality to access Bing Maps. Using script a site can implement map feature by referencing the Bing Maps Silverlight Control XAP file in an object tag. Here are some sample applications:  Microsoft Bing Map   & DeepEarth Project 
    Slow Response
    The initial slow response is due to downloading of the .xap file. This is not an issue since it is a one-time download and the user gets a fast response with later interaction. For later visits, user will not face slow response issue as browser already cached the data. Also check out rules - Do you use dynamic application loading in Silverlight