Monday, December 13, 2010

Sharing resources and styles between projects in Silverlight

I have lately been working on a somewhat larger Silverlight application for a client here in New Zealand. The application is being built using PRISM, which means that it is loaded in a composite/ modular way. And with this modular/composite loading, being able to share resources between different modules becomes pretty interesting as it makes it easier to get all modules to share the same look and feel. So this got me to rehash a topic I have already talked about before, merged dictionaries.

This is obviously not a PRISM only thing to do, but it often becomes a little more sought after when the application starts getting spread out over several different projects… Luckily, it is very easy to share resources between projects in Silverlight.

The first thing to do is to create a class library project in Visual Studio, which will create an almost empty project for you. All you have to do to make it completely empty is to delete the Class1.cs file.

After you have created a completely empty project, it is time time to create some resources. This is easiest to do by adding a new item to the project, and selecting the “Silverlight Resource Dictionary” item. This will create a new XAML file with a ResourceDictionary as the root element.

I’m working with VS2010 and Silverlight 4. In earlier versions, I believe the “Silverlight Resource Dictionary” item was missing, and we had to create our own, which isn’t terribly hard, but still annoying. Just create a new XML file and add the following XML to it

<ResourceDictionary
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

</ResourceDictionary>

The above is exactly the same XAML that gets create for us automatically when adding a new “Silverlight Resource Dictionary”.

Read more: DarksideCookie

Posted via email from .NET Info