2014-03-02 38 views
2

我試圖用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?」的,但在這種情況下,問題是在代碼後面,我沒有反正...

+1

你如何嵌入ResourceLibrary.dll到您的SomeOtherProject? –

+0

@devhedgehog,你的意思是參考? – Tar

+0

我的意思是在你的代碼中的某個地方有類似這樣的'爲了將ResourceLibrary的資源與你的SomeOtherProject的資源合併。你能告訴我們 –

回答

1

當您使用ComponentResourceKey確保在XMLNS前綴是從該.dll類文件不同

((DLL =「地方」 - 這個類=「資源」)

<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文件詞典

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

,然後我actualy窗口/用戶控件裏面,我合併了窗口/用戶控件資源,上述資源字典和它的工作

希望這有助於