在Style.Resources

时间:2016-12-09 13:57:15

标签: wpf xaml resourcedictionary dynamicresource

我在模板中使用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>

2 个答案:

答案 0 :(得分:4)

据我所知StaticResourceExtensionsome situations中无效。

闻起来就像你发现了类似的情况:

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

我希望这可以帮助您解决问题。

答案 1 :(得分:0)

很确定这是与BAML编译相关的错误。如果使用AnotherForm在运行时动态编译,代码将起作用。我想这也可以用作解决方法。