views:

20

answers:

2

I have a XAMl string table that is working a treat when i need to refer to it from my controls in XAML.

However I could also do with being able to access it for use in other sections of code - such as validation messages in my model. As you cannot add a resource file to a silverlight I beed to use this.

someone must know what i need to call to get at the resource file, preferably strongly typed so intelli-sense can bail out my forgetfulness.

The Resource File definition

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:system="clr-namespace:System;assembly=mscorlib">

    <system:String x:Key="test">test</system:String>
</ResourceDictionary>
A: 

You can use the indexer to receive this. Once you have a reference to the resource dictionary, you can use:

string test = (string)resources["test"];

For details, see Referencing Resources from Code.

Reed Copsey
no way to get intelli sense to auto complete? I really hate hooking things up with strings. so error prone. But thanks for the answer.
John Nicholas
@John: No. It's not typesafe, nor a "real" type - more like a Dictionary (hence, ResourceDictionary).
Reed Copsey
fair enough. thanks
John Nicholas
+1  A: 

Instead of listing the strings in XAML, why not using resource files (RESX) directly? This way Visual Studio will create wrappers around your resources using (publicresxfilecodegenerator) and you will be able to get rid of the magic strings, both in code as well as XAML.

Murven
This is what i tried to do - i quite liked the idea of seperating the UI strings from the internal strings - which would usually be error messages instead of tool tips, but you dont have the resources tab in a silverlight project settings (you know where you have build options etc),
John Nicholas
You have to manually add one or several RESX files to your project. When you do so, Visual Studio uses the RESXFILECODEGENERATOR to create a wapper around your file and you can use the file as a static class with properties after that.
Murven
thankyou, you are right. I wish i had asked the quetsion you have answered as that is what i origionally wanted before giving up.
John Nicholas