我試圖用ComponentResourceKey
重用定製的資源,但它不工作,我得到這樣的警告:爲什麼使用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?」的,但在這種情況下,問題是在代碼後面,我沒有反正...
你如何嵌入ResourceLibrary.dll到您的SomeOtherProject? –
@devhedgehog,你的意思是參考? – Tar
我的意思是在你的代碼中的某個地方有類似這樣的 '爲了將ResourceLibrary的資源與你的SomeOtherProject的資源合併。你能告訴我們 –