0

テンプレートで DynamicResource を使用し、そのテンプレートを使用して各スタイル内のリソースとして StaticResourceExtensions を使用しているため、DynamicResource はそれらのそれぞれで異なる方法で評価されます。

問題は、次のエラーが発生することです。

Unable to cast object of type 'System.Windows.Media.Effects.DropShadowEffect' to type 'System.Windows.ResourceDictionary'

これが私のコードです:

<DropShadowEffect 
    x:Key="Sombra" 
    Opacity="0.5" 
    ShadowDepth="3" 
    BlurRadius="5"
/>

<ControlTemplate 
    x:Key="ControleGeometriaTemplate"
    TargetType="{x:Type Control}"
>
    <Border
        x:Name="border"
        Background="{TemplateBinding Background}"
        Width="{TemplateBinding Width}"
        Height="{TemplateBinding Height}"
    />
        <Path
            x:Name="ícone"
            Fill="{TemplateBinding Foreground}"
            Effect="{DynamicResource PathShadow}"
        />
    </Border>
</ControlTemplate>

<Style x:Key="BotãoGeometria" TargetType="{x:Type ButtonBase}">
    <Setter Property="Template" Value="{StaticResource ControleGeometriaTemplate}"/>
</Style>

<Style 
    x:Key="BotãoNavegaçãoBase" 
    TargetType="{x:Type ButtonBase}" 
    BasedOn="{StaticResource BotãoGeometria}"
>
    <Style.Resources>
        <StaticResource x:Key="PathShadow" ResourceKey="Sombra"/>
    </Style.Resources>      
</Style>
4

2 に答える 2

5

私の知る限り、StaticResourceExtensionは状況によっては正しく動作しません。

同様の状況を見つけたようなにおいがします:

<SolidColorBrush x:Key="RedBrush" Color="Red" />
<Style TargetType="TextBox" x:Key="Test">
    <Style.Resources>
        <StaticResourceExtension x:Key="NewRedBrushKey" ResourceKey="RedBrush" />
    </Style.Resources>
</Style>

あなたの問題を再現するにはTest、あなたのスタイルを使用するだけで十分です。Window

したがって、私の提案は、独自の拡張機能を使用することです。

public class ResourceFinder : System.Windows.Markup.MarkupExtension
{
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        FrameworkElement frameworkElement;
        IDictionary dictionary;
        IRootObjectProvider rootObjectProvider = (IRootObjectProvider)
            serviceProvider.GetService(typeof(IRootObjectProvider));

        if (rootObjectProvider != null) 
        {
            dictionary = rootObjectProvider.RootObject as IDictionary;

            if (dictionary != null)
            {
                return dictionary[ResourceKey];
            }
            else
            {
                frameworkElement = rootObjectProvider.RootObject as FrameworkElement;
                if (frameworkElement != null)
                {
                    return frameworkElement.TryFindResource(ResourceKey);
                }
            }

        }

        return null;
    }


    public object ResourceKey
    {
        get;
        set;
    }
}

次に、あなたのスタイルは次のようになります。

<Style 
    x:Key="BotãoNavegaçãoBase" 
    TargetType="{x:Type ButtonBase}" 
    BasedOn="{StaticResource BotãoGeometria}">
    <Style.Resources>
        <local:ResourceFinder x:Key="PathShadow" ResourceKey="Sombra" />
    </Style.Resources>
</Style>

これが問題の解決に役立つことを願っています。

于 2016-12-09T20:32:47.320 に答える