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.
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.
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 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:
Content="Go to Microsoft" TargetName="_new" />
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:
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:
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.
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.
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.
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.
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.
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!