We always look for the option of Reusability. So, what is the way in Silverlight where we can make use of this concept ? When we are making any design and we as a designer ordeveloper dont wont to do the same work again which we have done before.
Here is the concept called Resource Dictionary , which helps in solving this problem –
WHAT IS RESOURCE DICTIONARY ?
With this we can manage Styles in different files. This is feature availed to us where we can store our defined or custom styles.
STEP WISE PROCEDURE TO MAKE USE OF RESOURCE DICTIONARY
1) Create a new Silveright project either in Blend or through Visual studio. If creating through Visual Studio , open the MainPage.xaml in Blend, by right clicking the same and choosing – “Open with Expression Blend” from the drop down box.
I will use a simple Object – “Button”, as an example. You can use the same concept for any Element you like.
2) Add a Button to the LayoutRoot.
3) Right Click the Button and choose “Edit Template” and then “Edit a copy”
3) Now, after this a new Pop-up Box comes up “Create Style Resource”. Name the Style – I have named it as “MyNewButtonStyle”. Now, in “Define Option” , Three choices are given which means – Where to store the Style
The Default option is – “This Document” – which means whatever changes you make will be reflected in this document only. The “Application” Option will store the Style information in App.xaml file. Third Option is “Resource Dictionary” which we are dealing about , but this option is grayed as there is no resource dictionary we have made as of now.
4) So, Now go to the File>New Item> Resource Dictionary. Name your Resource Dictionary. NOTE: Look at the extension , its Xaml.
5) This will make your Third Option in Resource Dictionary active, reflecting the proper file name.
6) I have made some small changes in the button.
7) open App.xaml in xaml code view and you can see that – Resource dictionary has been added.
8) Now if you see the Button Xaml – you can see that Style is bound to the name you have given to the Style. Here for me it will show “MyNewButtonStyle”, which I have given in Step 3.
9) Now, if you open the Resource dictionary file which has .xaml extension , you can see what changes you have made. Like – I have made changes for the Button. ( I am not mentioning it here as , its a big file ) you can see in the same in your PC.
This is how we have made use of Resource Dictionary.
10) So, Now that we have created the Resource Dictionary , and as we want to use the same dictionary in another project . Its straight forward you, have to just refer the name of the resource dictionary under the three option “Create Style Resource”. And you are done.