如何构建一个字符串来动态访问.resx资源



我想知道如何动态地构建一个字符串来引用参考资料中的字符串。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中实现这一点,或者有一些我没有想到的其他方法?

我不认为这是可能的XAML,但我们正在做的转换器/s。它可能会变得相当混乱,但如果你设计得比我好,它就会有更多的潜力,它会使代码更好。

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}" />

最新更新