在文件夹中搜索和列出 WPF 资源字典 [英] searching and listing WPF resource dictionaries in a folder

查看:22
本文介绍了在文件夹中搜索和列出 WPF 资源字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个 WPF 应用程序,它将具有我们的构建系统标记的多个皮肤.理想情况下,我们希望应用程序列出可用的皮肤,因为某些版本将具有一对多的皮肤.

I'm making a WPF application that is going to have multiple skins branded in by our build system. Ideally we would like the application to list off the skins available as some builds will have one to many skins.

在运行时有没有办法枚举特定文件夹中的所有资源字典?

At runtime is there a way to enumerate all the Resource Dictionaries in a specific folder?

我想避免在我的代码隐藏中硬编码 XAML 文件名,因为这是一个不断变化的情况.

I want to avoid hard coding the XAML filenames in my codebehind as this is a changing situation.

推荐答案

排序.

您可以枚举所有 BAML(编译后的 XAML)文件,如下所示:

You can enumerate all BAML (compiled XAML) files as follows:

  var resourcesName = assembly.GetName().Name + ".g";
  var manager = new System.Resources.ResourceManager(resourcesName, assembly);
  var resourceSet = manager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
  var allXamlFiles =
    from entry in resourceSet.OfType<DictionaryEntry>()
    let fileName = (string)entry.Key
    where fileName.EndsWith(".baml")
    select fileName.Substring(0, fileName.Length-5) + ".xaml";

如果不实际加载它们,就无法知道其中哪些是 ResourceDictionaries,哪些是其他 XAML,例如 Windows 或 UserControl.因此,您的直接问题的答案是否",除非您加载找到的每个 XAML 以检查它是否为 ResourceDictionary.这会很慢.

There is no way to know which ones of these are ResourceDictionaries and which are other XAML such as Windows or UserControls without actually loading them. So the answer to your direct question is "no" unless you load each XAML you find to check if it is a ResourceDictionary. This would be very slow.

另一方面,如果您愿意为 ResourceDictionaries 使用命名方案,那么您可以枚举程序集中的所有 BAML 并选择与您的命名方案匹配的任何一个,并相信它们是 ResourceDictionaries.只需扩展上述查询中的where"子句即可.

On the other hand, if you're willing to use a naming scheme for your ResourceDictionaries, then you can enumerate all BAMLs in your assembly and select whichever match your naming scheme, and trust that they are ResourceDictionaries. Just extend the "where" clause in the above query.

因此答案是有点".

这篇关于在文件夹中搜索和列出 WPF 资源字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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