设置 ResourceDictionary.Source 时,从代码创建 WPF ResourceDictionary 似乎不起作用 [英] Creating WPF ResourceDictionary from code doesn't seem to work when setting ResourceDictionary.Source

查看:61
本文介绍了设置 ResourceDictionary.Source 时,从代码创建 WPF ResourceDictionary 似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,其中包含我希望在 FrameworkElement 之外使用的 xaml ResourceDictionary.资源字典将包含项目本地类的 DataTemplate,以避免污染 app.xaml(因为项目是一个棱镜模块,根据配置并不总是存在).

I have a project containing a xaml ResourceDictionary that I wish to use outside of a FrameworkElement. The resource dictionary will contain a DataTemplate for a class local to the project to avoid polluting the app.xaml (as the project is a prism module, and will not always be present depending on config).

所以,我有一个带有资源构建操作的 test.xaml 文件.

So, I have a test.xaml file with a Resource build action.

这旨在为 TestObject 类提供 DataTemplate.

This is intended to supply the DataTemplate for a TestObject class.

在 TestObject 类中,我有一个 GetTemplate() 方法

In the TestObject class I have a GetTemplate() method

以下工作:

DataTemplate GetTemplate()
{
    Uri uri = new Uri("MyProject;component/test.xaml", UriKind.Relative);

    var dict = new ResourceDictionary { Source = uri};

    return (DataTemplate)dict["TestObjectDataTemplate"];
}

当我将 uri 分配给 ResourceDictionary.Source 属性时,这会引发异常

This throws an exception when I assign the uri to the ResourceDictionary.Source property

DataTemplate GetTemplate()
{
    Uri uri = new Uri("/test.xaml", UriKind.Relative);

    var dict = new ResourceDictionary { Source = uri};

    return (DataTemplate)dict["TestObjectDataTemplate"];
}

第二个示例失败,因为在本地程序集中找不到/test.xaml.为什么我需要使用ReferencedAssembly;component/test.xaml"来访问它?

The second example fails as the /test.xaml can't be found in the local assembly. Why would I need to access it with "ReferencedAssembly;component/test.xaml" ?

在这种情况下,本地程序集是指执行程序集还是代码/资源所属的程序集?

In this instance, does local assembly mean the executing assembly or the assembly the code/resource is part of?

更新以反映实际问题.

推荐答案

尝试 UriKind.RelativeOrAbsolute.

更明显的喜欢.

    DataTemplate GetTemplate()
    {           
        ResourceDictionary resource = new ResourceDictionary()
        {
            Source = new Uri(@"/AssemblyFullName;component/test.xaml", UriKind.RelativeOrAbsolute)
        };

        return (DataTemplate)resource["TestObjectDataTemplate"];
    }

在这种情况下,进行本地组装表示执行程序集或汇编代码/资源是其中的一部分?

In this instance, does local assembly mean the executing assembly or the assembly the code/resource is part of?

比如说:
您有两个项目 Project A 和 Project B.

Say for example:
You have two projects Project A and Project B.

您在项目 B 中使用项目 A 作为参考

You are using Project A as reference in Project B

现在,如果您想使用这样的资源 /test.xaml.然后,此资源应驻留在项目 B 中.因为它是执行程序集.[它将可用于项目 A 和项目 B.您可以使用上述语法.如/test.xaml]

Now, if you want to use the resource like this /test.xaml. Then, this resource should reside in the Project B. Since, it is the executing assembly. [It will be available for both Project A as well as Project B. You could use the above mentioned syntax. like /test.xaml]

如果您希望在项目 A 中定义和使用资源.那么,您应该使用 "/ProjectA;component/test.xaml" 因为它不是当前正在执行的程序集.[它将可用于项目 A 和项目 B.您必须使用 "/ProjectA;component/test.xaml" 来访问这两个项目]

If you want the resource to be defined and used inside Project A. Then, you should use "/ProjectA;component/test.xaml" because it is not the current executing assembly. [It will be available for both Project A as well as Project B. You have to use "/ProjectA;component/test.xaml" this to access in both the projects]

这篇关于设置 ResourceDictionary.Source 时,从代码创建 WPF ResourceDictionary 似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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