代码之家  ›  专栏  ›  技术社区  ›  Ant

如何从WPF中的其他字符串资源构建字符串资源?

  •  1
  • Ant  · 技术社区  · 15 年前

    这是我的“示例代码”,包括我要做的。显然,目前还不起作用,但我有什么办法可以让它起作用吗?

    <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="ProductName">Foo</System:String>
        <System:String x:Key="WindowTitle">{ProductName} + Main Window</System:String>
    </ResourceDictionary>
    
    1 回复  |  直到 15 年前
        1
  •  2
  •   Ray Burns    15 年前

    以这种方式向ResourceDictionary添加计算字符串的唯一方法是创建 MarkupExtension . 你的

    <ResourceDictionary ...>
      <sys:String x:Key="ProductName">Foo</sys:String>
      <local:MyStringFormatter
        x:Key="WindowTitle"
        StringFormat="{0} Main Window"
        Arg1="{StaticResource ProductName}" />
    </ResourceDictionary>
    

    这假设您已经创建了一个 加价扩展 “本地”命名空间中名为 MyStringFormatterExtension 具有名为“StringFormat”、“Arg1”、“Arg2”等属性并具有 ProvideValue() 方法做的很明显。

    Binding 使用 StringFormatter ResourceDictionary .