我正在尝试使用 重用自定义资源ComponentResourceKey
,但它不起作用,我收到以下警告:
Warning 12 The resource "{ComponentResourceKey ResourceId=SadTileBrush, TypeInTargetAssembly={x:Type res:CustomResources}}" could not be resolved.
这是ResourceLibrary/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>
和ResourceLibrary/CustomResources.cs
:
namespace ResourceLibrary{
public class CustomResources{}
}
用法如下(中SomeOtherProject/MyWindow.xaml
):
<Button Background="{DynamicResource {ComponentResourceKey
TypeInTargetAssembly={x:Type res:CustomResources},
ResourceId=MyBrush}}"> Some text </Button>
为什么“资源无法解析”?
请注意,我知道 SO 问题“ Getting a ComponentResourceKey to Work? ”,但这种情况下的问题在于代码隐藏,无论如何我都缺少它......