我正在构建一个包含多个程序集的WPF应用程序,并且我想在它们之间共享资源字典。这需要使用ComponentResourceKey。我已经构建了一个小型演示来测试CRK,但好像无法使其正常工作。
我的演示有两个项目,一个名为Demo的WPF项目和一个名为Common的DLL。 Common项目有一个名为Themes的文件夹。其中包含我的资源字典generic.xaml。以下是资源字典的文本:
有任何想法为什么这不起作用吗?感谢您的帮助。
我的演示有两个项目,一个名为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的类。它包含一个用于引用字典中 Brush 资源的属性:
public static ComponentResourceKey RedSolidBrush
{
get { return new ComponentResourceKey(typeof (SharedResources), "RedSolidBrush"); }
}
最后,在我的Demo项目中,主窗口引用刷子资源来填充一个矩形:
<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.
有任何想法为什么这不起作用吗?感谢您的帮助。