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

查看:97
本文介绍了在文件夹中搜索和列出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或UserControls).因此,除非您加载发现的每个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,并选择与您的命名方案匹配的任何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天全站免登陆