动态加载ResourceDictionary

发布于 2024-11-18 01:09:51 字数 743 浏览 8 评论 0原文

我的项目中有一个文件夹 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

○愚か者の日 2024-11-25 01:09:52

我是否必须手动转换程序集编译资源 (MyAssembly.g.resources) 中的名称?

情况可能是这样,我自己也会这样做,但是关于如何做到这一点的问题已经得到解答已经这样了,这应该不是什么大问题。确保字典的编译操作匹配,并且您可能希望在名称前添加包 uri。

Do I have to manually convert the names from the assembly's compiled resources (MyAssembly.g.resources)?

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.

梅窗月明清似水 2024-11-25 01:09:51

顺便说一句,人们还可以手动加载 ResourceDictionary,如下所示:

ResourceDictionary dict = new ResourceDictionary(); 
System.Windows.Application.LoadComponent(dict, 
  new System.Uri("/SomeAssembly;component/SomeResourceDictionary.xaml",
  System.UriKind.Relative));

之后,可以使用 foreach 对其具有的 Keys 属性等与该 dict 对象进行对话

http://msdn.microsoft.com/en-us/library/ms596995(v=vs.95).aspx

加载的 XAML 文件可以是应用程序定义文件(例如 App.xaml),也可以是页面文件(例如 MainPage.xaml)。 XAML 文件可以位于以下位置之一:

  • 包含在申请包中。
  • 嵌入到应用程序集中。
  • 嵌入到来源站点的库程序集中。

BTW, one can also manually load a ResourceDictionary as it seems:

ResourceDictionary dict = new ResourceDictionary(); 
System.Windows.Application.LoadComponent(dict, 
  new System.Uri("/SomeAssembly;component/SomeResourceDictionary.xaml",
  System.UriKind.Relative));

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

The XAML file that is loaded can be either an application definition file (App.xaml, for example) >or a page file (MainPage.xaml, for example). The XAML file can be in one of the following locations:

  • Included in the application package.
  • Embedded in the application assembly.
  • Embedded within a library assembly at the site of origin.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文