c# - 将多个资源文件加载到一个资源管理器中

标签 c# .net

我正在寻找一种方法来制作一个资源管理器来保存多个资源文件中的所有数据。这可能吗?如果是,那对我很有用,因为我有 10 多个带翻译的资源文件。我想为此制作一个包装器类并制作一个资源管理器,所以如果我使用 rm.GetString("string"); 我从一个资源文件中获取此值。我可能认为这不是最好的主意,但是...如果您有任何好的想法,请在这里分享!

我正在尝试以下代码:

var rm = new ResourceManager("ProjectNameSpace.ResourceName",
    Assembly.GetExecutingAssembly());

通过这样做,我只从我指定的文件加载资源:ProjectNameSpace.ResourceName,对吗?

对于这种或不同的方法,是否有任何好的解决方法?

最佳答案

这不是您要问的确切内容,但也许会有所帮助。这是我使用的程序的一些精简复制粘贴代码,该程序读取多个资源文件并创建资源文件中所有图标的组合字典。

   class Program
   {
      static void Main(string[] args)
      {
         IconManager.FindIconsInResources(Resources1.ResourceManager);
         //IconManager.FindIconsInResources(Resources2.ResourceManager);
         //IconManager.FindIconsInResources(Resources3.ResourceManager);

         Image iconImage = IconManager.GetIcon("Incors_office_building_16x16");
      }
   }


   public static class IconManager
   {
      private static readonly Dictionary<string, ResourceSet> _iconDictionary = 
                                                             new Dictionary<string, ResourceSet>();

      /// <summary>
      /// Method to read the resources info for an assembly and find all of the icons and add them 
      /// to the icon collection.
      /// </summary>
      public static void FindIconsInResources(ResourceManager resourceManager)
      {
         // Get access to the resources (culture shouldn't matter, but has to be specified)
         ResourceSet resourceSet = 
                   resourceManager.GetResourceSet(CultureInfo.GetCultureInfo("en-us"), true, true);
         if (resourceSet == null)
            throw new Exception("Unable to create ResourceSet.");

         // Top of loop to examine each resource object 
         foreach (DictionaryEntry dictionaryEntry in resourceSet)
         {
            // Check it's an icon (or some kind of graphic)
            if (!(dictionaryEntry.Value is Bitmap))
               continue;

            // Get the resource name, which is basically the filename without ".png" and with '-' 
            //  and blanks converted to '_'. Ignore .ico files.
            string resourceKey = (string)dictionaryEntry.Key;
            if (resourceKey.EndsWith("_ico", StringComparison.Ordinal))
               continue;

            // Add (or replace) the icon in the icon dictionary
            _iconDictionary[resourceKey] = resourceSet;
         }
      }


      /// <summary>
      /// Method to get an icon image from one of several resource files.
      /// </summary>
      public static Image GetIcon(string iconName)
      {
         ResourceSet resourceSet;
         _iconDictionary.TryGetValue(iconName, out resourceSet);
         if (resourceSet == null)
            return null;

         return (Image)resourceSet.GetObject(iconName);
      }
   }

关于c# - 将多个资源文件加载到一个资源管理器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27177121/

相关文章:

c# - 如何使用 MVC3 创建部分类别列表

c# - 编码不带傅立叶函数的 UnsharpMask

c# - 我只能关闭一次窗体,在创建窗口句柄之前无法在控件上调用 InvalidOperation Exception Invoke 或 BeginInvoke

.net - JSON.NET JsonConvert 与 .NET JavaScriptSerializer

c# - 让 Linq 与 Mysql 和 Mono 一起玩,这可能吗?

c# - 即使 DebugType=full,也无法在 Release模式下调试应用程序

c# - 以异步方式调用普通方法的最佳方法是什么?

c# - 将此 C# 代码转换为经典 ASP/VBScript?

c# - 防止在 ObservableCollection.CollectionChanged 事件上添加新项目

c# - 在没有进程引用它后删除 "in use"文件