首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么使用ComponentResourceKey“无法解析资源”?

为什么使用ComponentResourceKey“无法解析资源”?
EN

Stack Overflow用户
提问于 2014-03-02 20:17:37
回答 1查看 2.2K关注 0票数 2

我尝试使用ComponentResourceKey重用自定义资源,但它不起作用,并且我收到以下警告:

Warning 12 The resource "{ComponentResourceKey ResourceId=SadTileBrush, TypeInTargetAssembly={x:Type res:CustomResources}}" could not be resolved.

这是ResourceLibrary/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:ResourceLibrary">
    <ImageBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:CustomResources},
                        ResourceId=MyBrush}"
        ImageSource="ResourceLibrary;component/../../myImage.jpg">
    </ImageBrush>
</ResourceDictionary>

ResourceLibrary/CustomResources.cs

代码语言:javascript
复制
namespace ResourceLibrary{
    public class CustomResources{}
}

用法如下(SomeOtherProject/MyWindow.xaml格式):

代码语言:javascript
复制
<Button Background="{DynamicResource {ComponentResourceKey
                    TypeInTargetAssembly={x:Type res:CustomResources}, 
                    ResourceId=MyBrush}}"> Some text </Button>

为什么“资源无法解析”?

请注意,我注意到了SO问题"Getting a ComponentResourceKey to Work?",但这种情况下的问题是在代码隐藏中,无论如何我都没有代码隐藏……

EN

回答 1

Stack Overflow用户

发布于 2014-04-01 15:16:57

使用ComponentResourceKey时,请确保xmlns前缀与.dll类文件不同

(DLL= 'Local‘- this class = 'res')

代码语言:javascript
复制
<Button Background="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type res:CustomResources}, ResourceId=SadTileBrush}}" Padding="5" Margin="5" FontWeight="Bold" 
            FontSize="14" Content="A Resource From ReusableResourceLibrary" />

我创建了这个字典类来嵌入/合并我的.dll字典

代码语言:javascript
复制
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/ReusableResourceLibrary;component/Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
<ImageBrush x:Key="DicTileBrush" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 50 50" ImageSource="../Resources/Images/Smiley_Happy.png" Opacity="0.3" />
</ResourceDictionary>

然后在我实际的窗口/用户控件中,我将窗口/用户控件资源与上面的资源字典合并在一起,并且它起作用了

希望这能有所帮助

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

https://stackoverflow.com/questions/22127424

复制
相关文章

相似问题

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