2

を使用してカスタム リソースを再利用しようとして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? "を認識していることに注意してください。

4

1 に答える 1

1

ComponentResourceKey を使用する場合は、xmlns プレフィックスが .dll クラス ファイルと異なることを確認してください。

((DLL = 'Local' - このクラス = 'res')

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

そして、実際のウィンドウ/ユーザーコントロール内で、ウィンドウ/ユーザーコントロールリソースを上記のリソースディクショナリとマージし、機能しました

お役に立てれば

于 2014-04-01T07:16:57.073 に答える