让 ComponentResourceKey 发挥作用?
我正在构建一个包含多个程序集的 WPF 应用程序,并且我想在它们之间共享一个资源字典。这需要 ComponentResourceKey。我已经构建了一个小型演示来测试 CRK,但我似乎无法让它工作。
我的演示有两个项目,一个名为 Demo 的 WPF 项目和一个名为 Common 的 DLL。 Common 项目有一个名为Themes 的文件夹。它包含我的资源字典,generic.xaml。以下是资源字典的文本:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Common" >
<SolidColorBrush
x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:SharedResources}, ResourceId=RedSolidBrush}"
Color="Red"/>
</ResourceDictionary>
Common 还包含一个名为 SharedResources.cs 的类。它包含一个用于引用字典中画笔资源的属性:
public static ComponentResourceKey RedSolidBrush
{
get { return new ComponentResourceKey(typeof (SharedResources), "RedSolidBrush"); }
}
最后,我的演示项目中的主窗口引用画笔资源来填充矩形:
<Window x:Class="ComponentResourceKeyDemo.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:res="clr-namespace:Common;assembly=Common"
Title="Window1" Height="300" Width="300">
<Grid>
<Rectangle Height="100" Width="100" Stroke="Black" Fill="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type res:SharedResources}, ResourceId=RedSolidBrush}}" />
</Grid>
</Window>
我找不到它不起作用的原因。它在 VS 2008 和 Blend 中编译良好,但未调用资源。我唯一的线索是 Blend 中的一条错误消息:
The Resource "{ComponentResourceKey ResourceId=RedSolidBrush, TypeInTargetAssembly={x:Type res:SharedResources}}" could not be resolved.
知道为什么这不起作用吗?感谢您的帮助。
I am building a WPF app with several assemblies, and I want to share a resource dictionary among them. That requires a ComponentResourceKey. I have built a small demo to test out the CRK, and I can't seem to get it working.
My demo has two projects, a WPF project called Demo, and a DLL called Common. The Common project has a folder called Themes. It contains my resource dictionary, generic.xaml. Here is the text of the Resource dictionary:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Common" >
<SolidColorBrush
x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:SharedResources}, ResourceId=RedSolidBrush}"
Color="Red"/>
</ResourceDictionary>
Common also contains a class called SharedResources.cs. It contains a property for referencing the Brush resource in the dictionary:
public static ComponentResourceKey RedSolidBrush
{
get { return new ComponentResourceKey(typeof (SharedResources), "RedSolidBrush"); }
}
Finally, the main window in my Demo project references the brush resource to fill a rectangle:
<Window x:Class="ComponentResourceKeyDemo.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:res="clr-namespace:Common;assembly=Common"
Title="Window1" Height="300" Width="300">
<Grid>
<Rectangle Height="100" Width="100" Stroke="Black" Fill="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type res:SharedResources}, ResourceId=RedSolidBrush}}" />
</Grid>
</Window>
I can't find the reason it's not working. It compiles fine in VS 2008 and Blend, but the resource isn't invoked. The only clue I have is an error message in Blend:
The Resource "{ComponentResourceKey ResourceId=RedSolidBrush, TypeInTargetAssembly={x:Type res:SharedResources}}" could not be resolved.
Any idea why this isn't working? Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了我的问题。我将组件资源键与资源字典中的资源 ID 混淆了。换句话说,我的组件资源密钥与资源 ID 相同。我将静态属性更改为:
属性名称现在为 RedBrushKey,而不是 RedSolidBrush。关键现在起作用了。
I found my problem. I was confusing the Component Resource Key with the Resource ID inside the resource dictionary. In other words, my Component Resource Key was the same as the Resource ID. I changed my static property to this:
The property name is now RedBrushKey, instead of RedSolidBrush. And the key is now working.