In-Depth

Top 10 Things I Wish I Knew Before I Started My Silverlight 4 Project

Creating a business application is relatively easy in Silverlight 4 with the right tools in your toolbox and a little advice. Here are 10 things a Silverlight developer thinks you should know.

Released in April, Silverlight 4 delivers key functionality for developing business-oriented .NET applications. Silverlight 4 takes the line of business advances in Silverlight 3 -- data binding and Windows Communication Foundation (WCF) RIA Services -- to the next level.
Microsoft's rich Internet application technology -- a platform for.NET-enabled plug-ins that can run on multiple browsers, and out of browser on Windows and Mac desktops -- now supports the ability to compile code once and run it in Silverlight 4 or .NET Framework 4 runtimes.
With the new Silverlight designer in Visual Studio 2010 or Visual Web Developer Express 2010, and the Silverlight 4 Tools for Visual Studio 2010 add-in, you have what you need to create great applications. Building a business application with Silverlight 4 is relatively easy, but practical advice can help you avoid mistakes and save time. Before creating your Silverlight application, here are 10 things I think you should know:


1. Take Advantage of Project Templates
Visual Studio 2010, in conjunction with Silverlight 4 Tools for Visual Studio 2010, provides multiple project templates to create Silverlight 4 applications. The Silverlight Navigation Application implements functionality to work with multiple views inside the same Silverlight application and also integrates seamlessly with the browser's history (back button). The Silverlight Business Application template implements the same kind of navigation, authentication and WCF RIA Services -- a components framework that manages the programming logic for n-tier applications. So before starting with an empty Silverlight Application project template, consider using the template that is most appropriate for your solution.

2. Understand How to Work with Data
Most business applications require access to a data source. The only way to access data with Silverlight is by using a Web Service. But there are several ways to publish information from the server to a Silverlight application:

  • WCF RIA Services: This framework (formerly .NET RIA Services) interfaces with the data layer, which can be Entity Framework, and provides access to the data on the RIA client side as it appears on the ASP.NET Web server. WCF RIA Services manage the interaction between the client and the server.
  • WCF Data Services: Previously called ADO.NET Data Services, or "Astoria", WCF Data Services in .NET Framework 4 exposes Entity Data Model objects via Web services. This framework allows the user to query the data using the URI (with REST). WCF Data Services manage standard HTTP methods like GET, PUT, POST and DELETE (Ex: http://odata.netflix.com/Catalog/).

It's important to understand how to work with DataContext, a key property that simplifies the way data binding works. DataContext can be used to bind all controls within a container control to a single data source. This is useful when there are several controls that all use the same binding source. It could become repetitive to indicate the binding source for every control. Instead, the DataContext can be set for the container of the controls.

3. Think Outside of the Browser
Silverlight 4 provides new functionality associated with the Out of Browser feature, which lets you configure an application so that users can download it from a Web site and install it to run outside of a Web browser on their desktop. You now have more control of the appearance (size, position, etc.) of the application window, compared to the limitations that the browser imposed. If you want the application window to be 600 x 600 pixels and centered, you can now handle these attributes in Out of Browser. With the elevated trust option associated with Out of Browser in Silverlight 4, you can now access COM interop (Windows Component Object Model) for some of your application. For example, you can now easily interact with Excel to get some data or to provide reports. Keep in mind that this affects cross-platform compatibility because COM is only available in Windows.

Silverlight 4 also provides a new Web Browser Control and a Web Browser Brush. This lets you display a Web page inside your Silverlight application. Before using this feature, consider situations that may occur when the user is running the application; for example when the network connection is not available. In this case, you can implement some cache on the client side that allows the user to work offline.

4. Interface with First-Generation Printing
Silverlight 4 provides a new feature that allows printing from your Silverlight application. This feature is at an early stage but it can be used to generate simple reports. You can print any part of your Silverlight application because it will print the UI Element that you specify.

Pete Brown and Dave Poll at Microsoft created a Silverlight Reporting framework for short documents (2-5 pages) that interfaces with the printing functionality to simplify the process of generating reports inside of Silverlight using the Printing feature. This Simple Report Writer for Silverlight 4 can be downloaded from CodePlex

5. Check Out Prism
Are you planning a large-scale application with Silverlight? You might want to look at Prism, a framework and guidance that was developed by Microsoft's Patterns and Practices group for Windows Presentation Foundation and updated for Silverlight. This project, based on the Composite Application Library, offers tools to build modular client applications.

Prism implements functionality that allows modularity, view regions and commanding in your project, so that your enterprise application will be scalable and testable over time. Prism v4 for Silverlight 4, Visual Studio 2010 and .NET Framework 4 is underway. The May 2010 release, Prism 2.2 (for Silverlight 4) can be downloaded here.

6. Get Users into the Action
User experience is the key to great applications. New features in Silverlight 4 help to provide more interactivity with the end user. You can now handle the right click event and implement a custom context menu. The mouse wheel is also supported. Most computers today provide a built-in Webcam and microphone. In Silverlight 4, you can natively work with those components to create an online support application, for example.

Multi-touch input, which Microsoft describes as "raw touch input to mouse events" not gestures, is supported in Silverlight 4, Internet Explorer 8 and Windows 7. Multi-touch allows you to manipulate multiple objects at the same time, as if you had multiple mouse events. You can also manipulate a single object with more than one finger at a time -- resizing an image with two fingers, for example. This feature is ideal for creating kiosk applications.

7. Alert with Background Notifications
Often when you create a business application, you need to execute a task that runs in the background. This might be an issue because you don't want the user to not be able to use the application. Silverlight 4 introduces toast notifications for Out of Browser applications only that tell the user that something is happening in the background. You can see these small messaging windows (that appear in the bottom right corner) in applications such as Messenger or Outlook. In Macintosh applications, the toast windows appear at the top of the screen.

8. Answer the Question: Visual Studio or Expression Blend?
One of the questions that is often raised about Silverlight is: “Which tool should I use to build my application?” You should use both Visual Studio and Expression Blend. The Silverlight Designer embedded in Visual Studio 2010 is very simple in its functionality. You can add, resize and position controls, but you cannot edit templates or work with some storyboards using this editor. When you want to create complex designs, you'll need Expression Blend 4. Expression Blend will simplify the creation and editing of templates, storyboards and visual state. Creating a storyboard manually in Visual Studio is not something to consider based on the complexity of it. But when it's time to implement some code, Visual Studio is more efficient. Visual Studio also supports debugging, which is not available in Expression Blend.

Expression Blend 4 contains new libraries of controls. The Microsoft.Expression.Controls.dll contains the PathListBox control, which allows you to display data using a path. With this control you can display a list of products in a circle using an ellipse. The Expression Blend toolbox is a great way to explore controls. Remember that these new controls can also be used inside of Visual Studio but they are not part of the toolbox, so you'll have to reference the libraries manually.

9. Centralize Your Styles
XAML and Silverlight make it easy to create customizable applications by using styles. A style can define a color, a template or any existing properties of UI controls. In fact, a style allows the user to define the value of parameters. With Silverlight 4, you now have the choice of using implicit or explicit styling. Implicit styling applies to any control of a specific type. Explicit styling needs to be applied manually by specifying the key of the style on each control.

There are multiple locations where you can store styles: at the control level, at the page level and at the application level. Remember that if you customize your application and your styles are not centralized, they may be hard to change or fix. This is why I recommend that you store the styles in an external file called Resource Dictionary. As with CSS, it's a best practice to centralize your style definitions, and a good way to do this is by using Resource Dictionary. You can have multiple Resource Dictionary files in your application, this way you can easily structure your styles.

With application design and styling, you often think about font type. Silverlight 4 now supports the use of fonts that are available locally on the client machine. This gives more flexibility to the graphic designer.

10. Consider Additional Tools for Testing and Debugging
If you want to implement unit testing, Visual Studio 2010 provides a template and some APIs that allow you to create unit tests for your Silverlight business application. Consider adding other tools to your toolbox. Fiddler allows you to debug any issue between the server and the client. I often use this free tool when I want to debug a Silverlight application installed in production. It allows you to see any communication between the application and any Web services. Another great tool, Silverlight Spy, published by First Floor Software, inspects the XAP file downloaded by the client. It gives you access to the logical tree, to browse the UI Visual Tree and monitor performance. Having the proper tools will help you save some time.

Like any other type of application, planning is the key to success. With these 10 things in mind, you can create great business applications in Silverlight 4 and avoid hitting a few speed bumps along the way.


Reader Comments:

Fri, Aug 6, 2010 ws

MENI: From what I've heard, windows phone 7 apps are either xna (gaming) or silverlight apps.

Thu, Aug 5, 2010 Dustin Lincoln, NE

Meni, I would definitely recommend Silverlight if it met the client's needs. While I don't do Flash development, I don't make a habit of recommending any one product just for the sake of doing so, but I think Microsoft is on the right track with Silverlight and are doing things that will make it co-exist nicely with HTML 5 by concentrating on functionality outside the realm of UI. I actually blogged on Silverlight and talked about it from an HTML 5 perspective: http://www.dustinhorne.com/post/Silverlight-An-RIA-Power-Tool.aspx

Mon, Aug 2, 2010 Meni

Disclaimer: I'm an open-source and open standards fanobi, and this is off topic, feel free to ignore this A Question for you: would you recommend for your clients to start a silverlight project today? from comments on the mini-microsoft blog i read that some divisions at Microsoft itself are putting an emphesis on HTML5 over silverlight, thus putting SL on hold. I acknowledge that tooling and development time might be better with SL, but IMO today that takes second seat to openness. [windows phone 7's browser will not support SL on release. I see no support from MS for Android phones, mono maybe. And I would not trust MS to keep the Mac port alive forever, look at IE for the Mac!]

Add Your Comment:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above