gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:45:39 25 4
gpt4 key购买 nike

我正在寻找一种方法来制作一个资源管理器来保存多个资源文件中的所有数据。这可能吗?如果是,那对我很有用,因为我有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com