如何构建字符串以动态访问.resx资源

时间:2015-10-20 18:06:02

标签: c# wpf xaml

我想知道如何动态构建一个字符串来引用我的Resources.resx文件中的字符串?我基本上想要在XAML中执行以下操作:

这样我就可以获得标题为ToyBlockNameToyBallName的资源,该资源应来自resx,以便在必要时进行翻译。然后我希望将这些单独的字符串插入其他字符串的格式中。有许多字符串使用这些名称,所以最好是我可以替换单个单词而不是每种类型的玩具都有一个版本。

一些示例字符串为The {0} comes in a box.The {0} costs only a few dollars.

基本上尝试在XAML中执行此操作:

String.Format(Properties.Resources.Default["ToyComesInBox"],
    Properties.Resources.Default["Toy" + Constants.ToyName + "Name"]);

(其中ToyName =“Block”或“Ball”等)

有没有办法在XAML中实现这一点,还是有其他一些我没想到的方法?

1 个答案:

答案 0 :(得分:0)

我认为仅使用XAML可以做到这一点,但我们正在使用converter / s进行此操作。它可能会变得非常混乱,但如果你设计得比我更好,它有更多的潜力,它使代码更好IMO。

public class LocalizationConverter : IValueConverter
{
    public object Convert(
        object value, 
        Type targetType, 
        object parameter, 
        string language)
    {
        string valueString = value as string;
        var paramString = parameter as string;

        //so here you have the value of your binding in the value string
        //and if it's empty (because you don't want to use the bound value
        //you're setting the value string to be the param string

        if (string.IsNullOrWhiteSpace(valueString))
        {
            valueString = paramString;
        }

        //if value string (formerly your param string is empty just return
        //there is no value to be found
        if (string.IsNullOrWhiteSpace(valueString))
        {
            return null;
        }

        //now the fun starts :) ,I pass values with small command and 
        //separator symbol to be able to parse my parameters
        //something like this:
        //if (paramString.StartsWith("AddAfter|"))
        //{
        //    var valToAppend = paramString.Substring("AddAfter|".Length);
        //    return Strings.Get(Strings.Get(valToAppend + valueString));
        //}
        //and this basically does -> append the given parameter string after 
        //the part with the command to the value that comes from the binding
        //and then uses the resulting string from res dictionary 

        //So you could do something like passing "ToyType|Block" or you can
        //pass something in the value like Block and then in the parameters
        //have description ToyType or even pass not string object and get
        //what you want from it like
        //if(value is ToyType)
        //{
        //    return StringsGet((value as ToyType).Name)
        //}


        //Your parsing logic HERE



        //This is how we get strings from resources in our project
        //that you already know how to do

        return Strings.Get(valueString);
    }

    public object ConvertBack(
        object value,
        Type targetType, 
        object parameter, 
        string language)
    {
        throw new NotImplementedException();
    }

}

<强>用法

在资源(页面或全局)中定义

<converters:LocalizationConverter x:Key="Loc" />

在XAML中使用

仅来自参数的值(在本例中为字符串)

<TextBlock Text="{Binding 
                  Converter={StaticResource Loc},
                  ConverterParameter=ToyBlockName}" />

或仅来自绑定变量的值(可以是任何类型的对象)

<TextBlock Text="{Binding ToyBlockNameBoundValue 
                  Converter={StaticResource Loc}}" />

或者来自可以解析的绑定变量+复杂参数的值

<TextBlock Text="{Binding SomeBoundValue 
                  Converter={StaticResource Loc},
                  ConverterParameter=SomeMoreComplex|Parameter}" />