C# 为什么;无法解决该资源“问题”;使用ComponentResourceKey?

C# 为什么;无法解决该资源“问题”;使用ComponentResourceKey?,c#,wpf,xaml,resourcedictionary,componentresourcekey,C#,Wpf,Xaml,Resourcedictionary,Componentresourcekey,我正在尝试使用ComponentResourceKey重用自定义资源,但它不起作用,我收到以下警告: 警告12无法解析资源“{ComponentResourceKey ResourceId=SadTileBrush,TypeInTargetAssembly={x:Type res:CustomResources}}”。 以下是资源库/Themes/generic.xaml: <ResourceDictionary xmlns="http://schemas.microsoft.co

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

警告12无法解析资源“{ComponentResourceKey ResourceId=SadTileBrush,TypeInTargetAssembly={x:Type res:CustomResources}}”。

以下是
资源库/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:ResourceLibrary">
    <ImageBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:CustomResources},
                        ResourceId=MyBrush}"
        ImageSource="ResourceLibrary;component/../../myImage.jpg">
    </ImageBrush>
</ResourceDictionary>
<Button Background="{DynamicResource {ComponentResourceKey
                    TypeInTargetAssembly={x:Type res:CustomResources}, 
                    ResourceId=MyBrush}}"> Some text </Button>
用法如下(在
SomeOtherProject/MyWindow.xaml
中):

一些文本
为什么“资源无法解决”


请注意,我知道有一个所谓的问题“”,但在这种情况下,问题在于代码隐藏,我无论如何都缺少它…

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

((DLL='Local'-此类='res')


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

<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>

然后在实际的window/usercontrol中,我将window/usercontrol资源与上面的资源字典合并在一起,它就工作了


希望这对你有所帮助

你是如何将ResourceLibrary.dll嵌入SomeOtherProject的?@devhedgehog,你的意思是被引用的吗?我的意思是在你的代码中的某个点上,你有这样的东西,以便将ResourceLibrary的资源与SomeOtherProject的资源合并。你能给我们看看吗that@devhedgehog,不,我不知道尽管如此,但我认为如果我使用
ComponentResourceKey
没有必要。我错了吗?是的,你错了,你仍然需要它。
<Button Background="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type res:CustomResources}, ResourceId=SadTileBrush}}" Padding="5" Margin="5" FontWeight="Bold" 
            FontSize="14" Content="A Resource From ReusableResourceLibrary" />
<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>