设置 ResourceDictionary.Source 时,从代码创建 WPF ResourceDictionary 似乎不起作用
我有一个包含 xaml ResourceDictionary 的项目,我希望在 FrameworkElement 之外使用它。资源字典将包含项目本地类的 DataTemplate,以避免污染 app.xaml(因为该项目是一个 prism 模块,并且根据配置并不总是存在)。
因此,我有一个包含资源构建操作的 test.xaml 文件。
这旨在为 TestObject 类提供 DataTemplate。
在 TestObject 类中,我有一个 GetTemplate() 方法
以下工作原理:
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 属性时,这会引发异常
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”访问它?
在这种情况下,本地程序集是指执行程序集还是代码/资源所属的程序集?
编辑:更新以反映实际问题。
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).
So, I have a test.xaml file with a Resource build action.
This is intended to supply the DataTemplate for a TestObject class.
In the TestObject class I have a GetTemplate() method
The following works:
DataTemplate GetTemplate()
{
Uri uri = new Uri("MyProject;component/test.xaml", UriKind.Relative);
var dict = new ResourceDictionary { Source = uri};
return (DataTemplate)dict["TestObjectDataTemplate"];
}
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"];
}
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?
Edit: Updated to reflect the actual issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
UriKind.RelativeOrAbsolute
。更清楚的喜欢。
编辑:
举例来说:
您有两个项目项目 A 和项目 B。
您正在使用项目 A 作为项目 B 的引用
现在,如果您想使用像
/test.xaml
这样的资源。那么,该资源应该驻留在项目 B 中。因为,它是执行程序集。 [它将适用于项目 A 和项目 B。您可以使用上述语法。就像/test.xaml
]如果您希望在项目 A 中定义和使用资源。那么,您应该使用
"/ProjectA;component/test.xaml"
因为它不是当前正在执行的程序集。 [它将适用于项目 A 和项目 B。您必须使用"/ProjectA;component/test.xaml"
才能在这两个项目中访问]Try
UriKind.RelativeOrAbsolute
.More clearly like.
Edit:
Say for example:
You have two projects Project A and Project B.
You are using Project A as reference in Project B
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
]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]设置
Source
attr 有效,我在许多项目中成功使用了它。您的
Uri
可能有误。您应该尝试使用完全限定的包 Uri,例如:如果您的
test.xaml
文件不在项目根目录中,请确保正确设置其路径。Setting the
Source
attr works, I successfully used it in many projects.Your
Uri
might be wrong. You should try a fully qualified pack Uri, like :If your
test.xaml
file is not in the project root, be sure to set its path correctly.