WPF C#反射:使用构建操作“页面"遍历所有资源. [英] WPF C# Reflection: Iterate over all resources with build action "Page"

查看:393
本文介绍了WPF C#反射:使用构建操作“页面"遍历所有资源.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有很多ResourceDictionaries的.dll.

I have a .dll with a lot of ResourceDictionaries.

所有这些ResourceDictionaries的构建操作都设置为页面" .

The build action of all these ResourceDictionaries is set to "Page".

在Dll内部,我想找到所有这些ResourceDictionaries并对其进行迭代.

Inside the Dll, I want to find all these ResourceDictionaries and iterate over them.

如果将构建操作设置为"EmbeddedResource",则可以使用Reflection:

If I set the build action to "EmbeddedResource", I can use Reflection:

var embeddedResources = Assembly.GetExecutingAssembly().GetManifestResourceNames().ToList();

但是GetManifestResourceNames()不能不适用于具有构建操作页面"的资源.

But GetManifestResourceNames() does not work for resources with build action "Page".

如何查找或遍历具有构建操作页面"的所有资源?

解决方案不必是反射.任何其他方式都非常欢迎.

谢谢!

解决方案:

女士们,先生们!我必须宣布,本周的得奖者和本次赏金得主是吴宗宪.乔恩·吴(Jon Wu)给出了正确的提示,并通过搜索找到了以下解决方案:

Ladies and Gentleman! I have to announce, that the man of the week and the winner of this bounty, is Mr. Jon Wu. Jon Wu gave the right hint and through searching, I found this solution:

在运行时枚举.NET程序集资源

稍作更改的工作代码如下:

The working code, slightly changed looks like this:

public static string[] GetResourceNames()
    {
        var asm = Assembly.GetExecutingAssembly();
        string resName = asm.GetName().Name + ".g.resources";
        using (var stream = asm.GetManifestResourceStream(resName))
        using (var reader = new System.Resources.ResourceReader(stream))
        {
            return reader.Cast<DictionaryEntry>().Select(entry => (string)entry.Key).ToArray();
        }
    }

如果调用此方法,则所有资源字符串都以".baml"结尾,然后可以对其进行迭代.

If you call this method, you get all the resource strings with a ".baml" at the end and you can iterate over them.

感谢乔恩·吴(Jon Wu)的正确提示.

Thank you Jon Wu for the right hint.

推荐答案

根据

页面(仅WPF):用于将xaml文件编译为baml.然后使用与Resource相同的技术嵌入baml(即,可以作为AppName.g.resources使用).

所以听起来您只需要查找用YourAppName.g.resources标识的资源即可.

So it sounds like you just need to look for resources identified with YourAppName.g.resources.

这篇关于WPF C#反射:使用构建操作“页面"遍历所有资源.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆