从C#中的资源文件中获取一组值



我在资源文件(resx)中存储了一组值,并对这些值进行了命名,如下所示:

Form.Option.Value1 | Car 
Form.Option.Value2 | Lorry
Form.Option.Value3 | Bus
Form.Option.Value4 | Train

如果System.Resources.ResourceManager类有办法一次性检索所有这些值。我正在寻找一种按前缀获取的方法:

ResourceManager manager ...
IEnumerable<string> values = manager.GetStringsByPrefix("Form.Option");

这样做的原因是,我们有一个带有下拉列表的表单,其中的值可能需要根据区域性进行更改。

还可以将字符串值作为键值对返回,这样我就可以获得资源的名称及其值,例如:

IEnumerable<KeyValuePair<string,string>> values = manager.GetPairWithPrefix("Form.Options")

您可以使用GetResourceSet方法在ResourceManager中枚举特定语言的所有字符串。

如果您使用Visual Studio/.NET Framework中内置的本地化功能来本地化表单(包括组合框列表),它会生成如下代码:

在Form1.de.resx:中找到

<data name="comboBox1.Items" xml:space="preserve">
  <value>Auto</value>
</data>
<data name="comboBox1.Items1" xml:space="preserve">
  <value>Bahn</value>
</data>

在Form1.resx:中找到

<data name="comboBox1.Items" xml:space="preserve">
  <value>Car</value>
</data>
<data name="comboBox1.Items1" xml:space="preserve">
  <value>Train</value>
</data>

它像这样加载它们(在Initializecomponent的Form1.Designer.cs中找到):

System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
resources.ApplyResources(this.comboBox1, "comboBox1");
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Items.AddRange(new object[] {
   resources.GetString("comboBox1.Items"),
   resources.GetString("comboBox1.Items1")});
this.comboBox1.Name = "comboBox1";

这可能不是你所要求的答案,但作为.NET创建者提出的完全相同问题的解决方案,我怀疑它会引起人们的兴趣。

如果要使用.NET/VS本机本地化,只需设置表单的Language属性,然后通过IDE更新所有字符串。当您切换回(默认)时,您的原始字符串将被恢复。这两种语言都将在特定语言的resx文件中被记住。

您可能应该重新考虑格式。像

 Form.Option.Value = Car;Lorry;Bus;...

感谢您的回复,我通过对ResourceManager进行子类化并添加一个新方法来解决这个问题:

public class ResourceManager : System.Resources.ResourceManager
{
    public ResourceManager(Type resourceSource)
        : base(resourceSource)
    {
    }
    public IEnumerable<string> GetStringsByPrefix(string prefix)
    {
        return GetStringsByPrefix(prefix, null);
    }
    public IEnumerable<string> GetStringsByPrefix(string prefix, CultureInfo culture)
    {
        if (prefix == null)
            throw new ArgumentNullException("prefix");
        if (culture == null)
            culture = CultureInfo.CurrentUICulture;
        var resourceSet = this.InternalGetResourceSet(culture, true, true);
        IDictionaryEnumerator enumerator = resourceSet.GetEnumerator();
        List<string> results = new List<string>();
        while (enumerator.MoveNext())
        {
            string key = (string)enumerator.Key;

            if (key.StartsWith(prefix))
            {
                results.Add((string)enumerator.Value);
            }
        }
        return results;
    }
} 

尽管用于编辑resx文件的IDE不支持它,但您可以将字符串数组(我怀疑是任何可序列化类)添加到resx文件中:

string outPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
outPath = System.IO.Path.Combine(outPath, "MyResources.resx");
using (System.Resources.ResXResourceWriter rw = new System.Resources.ResXResourceWriter(outPath))
{
   rw.AddResource("ComboBox1Values", new string[] { "Car", "Train" });
   rw.Generate();
   rw.Close();
}

在输出文件中,您将看到以下内容:

<data name="ComboBox1Values" mimetype="application/x-microsoft.net.object.binary.base64">
  <value>AAEAAAD/////AQAAAAAAAAARAQAAAAIAAAAGAgAAAANDYXIGAwAAAAVUcmFpbgs=</value>
</data>

最新更新