2015-03-19 75 views
0

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

<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不能用于动画属性Background由于不兼容的类型

如何解决这个问题?

回答

0

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

<SolidColorBrush x:Name="RedBrush" Color="Red" /> 

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

然后你就可以在对象动画使用它:

你可以在你的风格相同的地方添加SolidColorBrush资源。

<VisualState x:Name="Pressed"> 
    <Storyboard> 
     <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootElement" 
             Storyboard.TargetProperty="Background"> 
      <DiscreteObjectKeyFrame KeyTime="0" 
            Value="{StaticResource RedBrush}"/> 
     </ObjectAnimationUsingKeyFrames> 
    </Storyboard> 
</VisualState> 
+0

不错,现在它的作品,感谢您的答案。我只有另一个关于VisualStateManager的离题问题。是否可以更改图片颜色(由于它是非alpha通道)? – neuronalbit 2015-03-19 16:01:57