枚举嵌入资源目录中的文件
在 WPF 中,有没有办法枚举特定嵌入资源目录中的所有文件?也就是说,所有项目的目录都将“构建操作”设置为“资源”。
Is there a way, in WPF, to enumerate through all files within a specific embedded resource directory? That is, a directory of items all having a "Build Action" set to "Resource".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些资源被编译到名为
YourAssemblyName.g.resources
的资源流中。因此,我们加载这个流,它看起来是一个字典,其中键是资源名称,值是资源数据。我们对资源名称感兴趣,因为它(通常)是资源的原始文件夹和文件名。然后,我们过滤掉以我们感兴趣的文件夹开头的那些键。LINQ查询过滤掉以给定文件夹名称开头的所有资源键,并从键中删除文件夹名称。
您需要知道的一件事是 XAML 文件被编译并赋予扩展名 BAML。因此,假设您在名为
Themes/Theme1.xaml
、Themes/Theme2.xaml
等的文件夹下有一堆资源字典。这些将被编译到您的程序集中如Themes/Theme1.baml
、Themes/Theme2.baml
等。The resources are compiled into a resource stream named
YourAssemblyName.g.resources
. So, we load up this stream which appears to be a dictionary where the key is the resource name and the value is the resource data. We are interested in the resource name as that is (usually) the original folder and file name for the resource. We then filter out those keys that begin with the folder we are interested in.The LINQ query filters out all the resource keys that start with the given folder name and also removes the folder name from the key.
One thing you need to know is that XAML files get compiled and given the extension BAML. So, let's say you have a bunch of resource dictionaries under a folder named
Themes/Theme1.xaml
,Themes/Theme2.xaml
, etc. These will get compiled into your assembly asThemes/Theme1.baml
,Themes/Theme2.baml
, etc.