代码之家  ›  专栏  ›  技术社区  ›  John Nicholas

如何从代码访问XAML文件中的字符串表?

  •  0
  • John Nicholas  · 技术社区  · 15 年前

    我有一个XAMl字符串表,当我需要从XAMl中的控件引用它时,它正在进行处理。

    不过,我也可以访问它,以便在代码的其他部分使用,例如模型中的验证消息。由于您无法将资源文件添加到silverlight,我不得不使用它。

    资源文件定义

    <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>
    
    2 回复  |  直到 15 年前
        1
  •  0
  •   Reed Copsey    15 年前

    你可以使用索引器来接收这个。一旦有了对资源字典的引用,就可以使用:

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

    Referencing Resources from Code .

        2
  •  1
  •   Murven    15 年前

    为什么不使用 resource files (RESX)