动态加载ResourceDictionary
我的项目中有一个文件夹 Templates,其中充满了(已编译的)XAML ResourceDictionaries。
在 UserControl 中,我想将所有模板加载到 ResourceDictionary 中。我将使用如下代码:
public MyView()
{
InitializeComponent();
foreach (var resourceUri in new GetResourceUrisFromTemplatesFolder())
Resources.MergedDictionaries.Add(
new ResourceDictionary
{ Source = new Uri(resourceUri, UriKind.Relative) });
}
我需要编写的是 GetResourceUrisFromTemplatesFolder 方法。我需要它来发现该文件夹中的所有资源。
URI 可以采用类似 /MyAssembly;component/MyNS/Templates/Template12345.xaml
或 ../../Templates/Template12345.xaml
的形式,
这可能吗?
我是否必须手动转换程序集编译资源 (MyAssembly.g.resources
) 中的名称?
I have a folder in my project, Templates, full of (compiled) XAML ResourceDictionaries.
In a UserControl, I want to load all the templates into the ResourceDictionary. I would use code like the following:
public MyView()
{
InitializeComponent();
foreach (var resourceUri in new GetResourceUrisFromTemplatesFolder())
Resources.MergedDictionaries.Add(
new ResourceDictionary
{ Source = new Uri(resourceUri, UriKind.Relative) });
}
What I need to write is the GetResourceUrisFromTemplatesFolder
method. I need it to discover all the resources from that folder.
The URIs could take a form like /MyAssembly;component/MyNS/Templates/Template12345.xaml
or ../../Templates/Template12345.xaml
Is this possible?
Do I have to manually convert the names from the assembly's compiled resources (MyAssembly.g.resources
)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
情况可能是这样,我自己也会这样做,但是关于如何做到这一点的问题已经得到解答已经这样了,这应该不是什么大问题。确保字典的编译操作匹配,并且您可能希望在名称前添加包 uri。
That might be the case and i myself would approach it that way, however the question about how to do just that has been answered already so it should be not that much of an issue. Make sure that the dictionaries' compile action matches, and you probably want to prefix the names with a pack uri.
顺便说一句,人们还可以手动加载 ResourceDictionary,如下所示:
之后,可以使用 foreach 对其具有的 Keys 属性等与该 dict 对象进行对话
http://msdn.microsoft.com/en-us/library/ms596995(v=vs.95).aspx 说
BTW, one can also manually load a ResourceDictionary as it seems:
After that, can talk to that dict object using foreach on the Keys property it has etc
At http://msdn.microsoft.com/en-us/library/ms596995(v=vs.95).aspx says