首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >让ComponentResourceKey开始工作吗?

让ComponentResourceKey开始工作吗?
EN

Stack Overflow用户
提问于 2009-09-14 16:12:47
回答 1查看 9.2K关注 0票数 3

我正在构建一个包含多个程序集的WPF应用程序,我想在它们之间共享一个资源字典。这需要一个ComponentResourceKey。我已经建立了一个小的演示,以测试CRK,我似乎无法使它工作。

我的演示有两个项目,一个名为Demo的WPF项目和一个名为Common的项目。公共项目有一个名为Themes的文件夹。它包含我的资源字典generic.xaml。以下是资源字典的文本:

代码语言:javascript
复制
<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>

公共还包含一个名为SharedResources.cs的类。它包含一个属性,用于引用字典中的刷资源:

代码语言:javascript
复制
public static ComponentResourceKey RedSolidBrush
{
    get { return new ComponentResourceKey(typeof (SharedResources), "RedSolidBrush"); }
}

最后,我的Demo项目中的主窗口引用画笔资源来填充矩形:

代码语言:javascript
复制
<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和isn中编译得很好,但是资源没有被调用。我唯一的线索是在Blend中有一个错误消息:

代码语言:javascript
复制
The Resource "{ComponentResourceKey ResourceId=RedSolidBrush, TypeInTargetAssembly={x:Type res:SharedResources}}" could not be resolved.

知道为什么这不管用吗?谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-09-14 19:47:33

我发现了我的问题。我把组件资源键和资源字典中的资源ID搞混了。换句话说,我的组件资源键与资源ID相同。

代码语言:javascript
复制
public static ComponentResourceKey RedBrushKey
{
    get {return new ComponentResourceKey(typeof(SharedResources), "RedSolidBrush"); }
}

属性名现在是RedBrushKey,而不是RedSolidBrush。关键现在起作用了。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1422538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档