10 Harder-to-find-than-they-should-be Answers for Silverlight Beginners

I’ve been teaching myself WPF / Silverlight as part of my next big project at StannardLabs: a line of trivia games for WinMo7 / Android / iOS phones. It’s mostly an exercise for learning how each of those platforms work, how they differ, and some of the application marketing differences on each platform.

In the process of learning Silverlight 4, the most recent version of Silverlight for which few guides or books exist, I’ve built a list of the top ten answers I wish were easier to find when I started learning Silverlight.

I was unable to quickly find answers on these subjects perhaps because I was using the wrong lexicon or perhaps these are things that professional Silverlight / WPF developers consider to be too “obvious” to be worthy of inclusion in any of their books or blog entries.

Well to make it easier for all of you, I went ahead and researched all of these.

Here’s my list:

  1. How do I actually navigate to different Silverlight content within an application?

    Answer: Thinking about Silverlight from a web-based approach at the moment, what you're really asking is "how can I create separate pages within a Silverlight application?" Fortunately, that metaphor matches up identically to what Silverlight uses: the Navigation framework and Silverlight Pages.[footnote:The Navigation elements uses a pathing formula similar to ASP.NET routing if you're familiar with ASP.NET MVC.]

    The easiest solution I've found using these tools is to create a masterpage (Main.xaml or something like that) which has a Silverlight Frame element sitting in the middle of it, and you swap out your interchangable pages within that frame.

    How Frames and Navigation Work in Silverlight

  2. How can I create a hyperlink to a webpage from within a Silverlight application?

    Answer: Using the same Silverlight Navigation framework I highlighted in my answer to question one you can create hyperlinks in Silverlight to external content which will open in a web browser. Here's a code sample from MSDN which will show you how it's done:

    <HyperlinkButton NavigateUri="http://www.microsoft.com"
        Content="Go to Microsoft" TargetName="_new" />
  3. How do I open a modal dialog window within a Silverlight application?

    Answer: First, understand that Silverlight is not Windows Forms or WPF, which both implement modal dialog behavior familiar to anybody who has worked with the Win32 API in the past. Silverlight is a cross-platform technology, therefore the behaviors you're used to courtesy of the Win32 API and its .NET progeny won't be available on every environment in which your Silverlight application executes therefore the standard modal dialog tools you're used to in WPF can't be used in Silverlight.

    That being said, it's easy to implement a modal dialog user interface element in Silverlight. Here's a couple of resources which explain how to do this:

  4. How can I span different font styles to small portions of inline text within a TextBlock element?

    Answer: Essentially, what you're asking for is the XAML / WPF equivalent of a <span> tag from HTML so you can selectively apply styles within a TextBlock element.

    Fortunately, an equivalent exists - it's called the Run element, which can be used anywhere inside the text content of a TextBlock element. Here are some great examples of the Run element being used in Silverlight, one of which I'm going to include below:

            <Run Background="Red"> 
            <Run Background="Blue"> 
  5. What’s the best way to implement session-specific storage for a Silverlight application?

    Answer: This question isn't as tricky as it seems - if you're an ASP.NET developer like me, you approach the concept of session and state differently than desktop programmers given that we work with HTTP, a stateless protocol.

    So here's the good news: Silverlight handles states just like a desktop application! So you can stash your session data in a handful of public properties on your application class for lightweight scenarios, create a static session class for middleweight scenarios, or use isolated storage for heavier scenarios - check out this thread from the official Silverlight forums if you want more information.

  6. Are there any compelling reasons to use an RIA-service instead of a vanilla WCF service in Silverlight applications?

    Answer: If you've ever read Microsoft .NET: Architecting Applications for the Enterprise (which you should read for the discussion of design tools and patterns even if you aren't an enterprise developer, which I am not) then you'll know that the answer to every design question like this one is always "it depends." This post by Shawn Wildermuth does a better job than I ever could laying out the factors that might lead you to pick one data service implementation for Silverlight over another, but I'll sum it up for you in order to answer this question:

    If your goal is to get a Silverlight application up and running as quickly as possible, then you should use RIA services as you won't need to write as much code compared to the alternatives nor will you need to worry about WCF cross-domain policy files, change tracking, and so forth.

  7. On the contrary, why might you choose a WCF service over an RIA-service for a Silverlight application?

    Answer: I made the decision to go with a vanilla WCF service over an RIA service because, from everything that I've read on the matter, WCF is a better choice for situations where you need to support additional clients besides Silverlight (iOS and Android clients in my case.) Additionally, if you have an existing WCF service already in place, you'll want to use that instead of rewriting everything to use RIA services.

  8. Can RIA-services be easily reused among non-Silverlight applications?

    Answer: the answer appears to be "no," but given that I'm still new to Silverlight myself I could be wrong. If you need to support additional clients, especially ones not on the .NET framework, then I recommend using WCF instead.

  9. How can I dynamically add XAML elements to my application at run-time?

    Answer: there are hundreds of different ways to add new XAML elements to your Silverlight application at run-time. My personal favorite is this example which shows you how to use LINQ to create additional data-driven elements in a Silverlight application. If you're looking for a simple guide on how to add new XAML elements programmatically, then I'll refer you to "Dynamic Manipulation of Silverlight Controls" which sums it up nicely.

  10. What’s the best way to implement form validation in a Silverlight application?

    Answer: where there is data, there shall be validation. Silverlight ships with its own validation framework, much the same way that ASP.NET ships with a validation framework. This step-by-step guide on forms validation in Silverlight 3 is the best example on how to get started that I've found.

And that does it - as always, I'm eager to hear back from the community regarding better answers to these questions, or perhaps more interestingly: better questions I did not ask!

Discussion, links, and tweets

I'm the CTO and founder of Petabridge, where I'm making distributed programming for .NET developers easy by working on Akka.NET, Phobos, and more..