Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将多个资源文件加载到一个资源管理器中_C#_.net - Fatal编程技术网

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

C# 将多个资源文件加载到一个资源管理器中,c#,.net,C#,.net,我正在寻找一种方法,使一个资源管理器可以保存来自多个资源文件的所有数据。这可能吗?如果是的话,这将对我很有用,因为我有大约10多个带翻译的资源文件。我想为此创建一个包装器类,并创建一个资源管理器,因此如果使用rm.GetString(“string”)我从一个资源文件中获取此值。我可能认为这不是最好的主意,但是。。。如果你有什么好主意,请在这里分享 我正在尝试以下代码: var rm = new ResourceManager("ProjectNameSpace.ResourceName",

我正在寻找一种方法,使一个资源管理器可以保存来自多个资源文件的所有数据。这可能吗?如果是的话,这将对我很有用,因为我有大约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);
      }
   }
类程序
{
静态void Main(字符串[]参数)
{
IconManager.FindIconsInResources(Resources1.ResourceManager);
//IconManager.FindIconsInResources(Resources2.ResourceManager);
//IconManager.FindIconsInResources(Resources3.ResourceManager);
Image iconImage=IconManager.GetIcon(“Incors\u office\u building\u 16x16”);
}
}
公共静态类IconManager
{
专用静态只读词典_iconDictionary=
新字典();
/// 
///方法读取程序集的资源信息,找到所有图标并添加它们
///到图标集合。
/// 
公共静态void FindIconsInResources(ResourceManager ResourceManager)
{
//获取对资源的访问权(文化不重要,但必须指定)
资源集资源集=
resourceManager.GetResourceSet(CultureInfo.GetCultureInfo(“en-us”),true,true);
如果(resourceSet==null)
抛出新异常(“无法创建资源集”);
//检查每个资源对象的循环顶部
foreach(资源集中的DictionaryEntry DictionaryEntry)
{
//检查是否为图标(或某种图形)
如果(!(dictionaryEntry.Value为位图))
继续;
//获取资源名称,它基本上是不带“.png”和“-”的文件名
//并将空格转换为“u”。忽略.ico文件。
string resourceKey=(string)dictionaryEntry.Key;
if(resourceKey.EndsWith(“ico”,StringComparison.Ordinal))
继续;
//在图标字典中添加(或替换)图标
_iconDictionary[resourceKey]=资源集;
}
}
/// 
///方法从多个资源文件之一获取图标图像。
/// 
公共静态图像GetIcon(字符串iconName)
{
资源集资源集;
_TryGetValue(iconName,out resourceSet);
如果(resourceSet==null)
返回null;
返回(图像)resourceSet.GetObject(iconName);
}
}

当然可以编写一个包装器类,枚举属于指定程序集集的所有资源文件,然后在此包装器中查询名为uniqueName的字符串,然后它将尝试在第一个资源文件中查找它,然后在另一个资源文件中查找它,直到找到为止。我写了一些类似的东西来查找一组程序集中定义的所有图标,然后在查询图标名称时返回图标。有关如何执行此操作的任何提示?都将尝试实现类似的东西。如果有效,我会将此标记为答案。@rosko当然,如果您有问题,请将其作为评论发布在这里。