Use a ResourceDictionay and an external resources located in an assembly

I have notice that some developers are asking questions about how to use a ResourceDictionary and reference it to a resource located in a separate assembly. Here is a simple step by step guide to make it possible, I assume you already have a Silverlight project created:

1) Create a new Silverlight Class Library

image


2) Add a Silverlight Resource Dictionary file to the Class Library project

image


3) Add your styles to it and build


image

4) From your Silverlight Application, add a reference to the Silverlight Class Library Project or assembly file

image

5) In the Resource property of the App, or UserContorl etc add a ResourceDictionary element, set the source property by using the following pattern ”/<assembly>;component/<resource>.xaml”. If the name of your Silverligth Class Library is ”MyClassLib” and have a Resource Dictionary file called MyDictionary.xaml in the root folder of the Silverlight Class Library, the Source property should have the following value:

<ResourceDictionary Source=”/MyClassLib;component/MyDictionary.xaml”/>


image

6) You can now use the styles.


If you don’t have more than just a Dictionary Resources added to the Assembly, and want to share the resources to other Silverlight app, just use a simple .xaml file which you copy and share among the different Silverlight apps, easier to add and use, and you can avoid extra dependencies to an assembly and adding references. Only use an assembly with a resource if you want to protect the resource so others can’t change your original resource, or have controls or other logic added into your assembly which you are going to use in your Silverlight app.

1 Comment

Comments have been disabled for this content.