开发者

How can I access a string table that is in a XAML file from code?

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://sc开发者_StackOverflow中文版hemas.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>


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜