首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VisualStateManager引发的异常

VisualStateManager引发的异常
EN

Stack Overflow用户
提问于 2015-03-19 11:17:07
回答 1查看 159关注 0票数 0

我目前正在实现一个按钮的自定义样式,并希望定义不同的状态(例如。(按下)用VisualStateManager。

代码语言:javascript
复制
<Style x:Name="customStyle" TargetType="Button">
<Setter Property="ClickMode" Value="Release"/>
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="Button">
            <Grid x:Name="RootElement" Background="{TemplateBinding Background}">
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal"/>
                        <VisualState x:Name="Pressed">
                            <Storyboard>
                                <ColorAnimation Storyboard.TargetName="RootElement"
                                                Storyboard.TargetProperty="Background"
                                                To="Red"
                            </Storyboard>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
                ...
            </Grid>
        </ControlTemplate>
    </Setter.Value>
</Setter>

如您所见,我希望将网格的背景属性更改为按下状态下的红色,但引发以下异常:

一次机会异常在0x7708210B (KERNELBASE.DLL)在gymlog.exe: 0x40080201: WinRT发起错误(参数: 0x800F1000,0x00000056,0x0178E4F4)。

如果我跳转到特定的内存中,将显示以下内容:

由于类型不兼容,ColorAnimation不能用于动画属性背景。

如何解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-19 12:58:41

Background属性不是颜色。这是一个画笔,所以你不能用ColorAnimation动画它。画笔可以用ObjectAnimationUsingKeyFrames动画化。但首先,您必须创建一个新的刷与目标颜色(在您的情况下,它是红色)。

您可以在您的样式相同的位置将SolidColorBrush添加到资源中:

代码语言:javascript
复制
<SolidColorBrush x:Name="RedBrush" Color="Red" />

<!-- And here goes your button style... -->

然后您可以在对象动画中使用它。

代码语言:javascript
复制
<VisualState x:Name="Pressed">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootElement"
                                       Storyboard.TargetProperty="Background">
            <DiscreteObjectKeyFrame KeyTime="0" 
                                    Value="{StaticResource RedBrush}"/>
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29143171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档