1

我正在处理Windows Phone 8项目。我与这个按钮有关的问题:动画按钮的按下VisualState不会触发

 <Button x:Name="decreaseFontButton" Foreground="{StaticResource GlobalBrush}"> 
      <VisualStateManager.VisualStateGroups> 
       <VisualStateGroup x:Name="CommonStates"> 
        <VisualState x:Name="Pressed"> 
         <Storyboard> 
          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Height" Storyboard.TargetName="decreaseFontButton"> 
           <DiscreteObjectKeyFrame KeyTime="0" Value="70"/> 
          </ObjectAnimationUsingKeyFrames> 
          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="decreaseFontButton" Completed="ObjectAnimationUsingKeyFrames_Completed_1"> 
           <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource GlobalBrush}"/> 
          </ObjectAnimationUsingKeyFrames> 
         </Storyboard> 
        </VisualState> 
       </VisualStateGroup> 
      </VisualStateManager.VisualStateGroups> 
      <Button.Content> 
       <Image Source="/Assets/Player/sub_minus.png" Width="28" Height="28"/> 
      </Button.Content> 
     </Button> 

是两个动画中没有一个被触发。我试图将VisualStateGroup的名称更改为FocusState和VisualState的名称为重点,但我没有看到任何更改。你能告诉我我做错了什么吗?

我很感谢您的关注!

+1

没有什么不对您的故事板,你想只是如何调用它。它只需要作为控制模板并声明为资源。我会建议寻找一个默认的按钮模板,以及它如何使用VisualStateManager,如果这不能很快地揭示你的答案,我们可以很容易地告诉你你错过了什么,但你并不遥远。 – 2013-03-11 16:34:15

+0

非常感谢您的努力,我已经做到了这一点,视觉效果如预期的那样,但我仍然没有发现为什么最初的解决方案不好。 – 2013-03-11 16:40:53

+1

这只是因为VisualStateManager不能直接应用到UIElement,只需要在UIElement TargetType的控件模板中。没什么大不了的,很高兴你找到了你的补救办法。干杯:) – 2013-03-11 18:12:22

回答

1

问题在于如何尝试修改视觉状态。这些实际上是按钮内部模板的一部分,不能以您尝试的方式进行配置。

相反,您应该看看创建一个新的样式,其中包括您想要的视觉状态更改以及他们将该样式应用于按钮。

像这样:

<phone:PhoneApplicationPage.Resources> 

    <SolidColorBrush Color="Aqua" x:Key="aquabrush" /> 

    <Style x:Key="AlternativeButtonStyle" TargetType="Button"> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/> 
     <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/> 
     <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/> 
     <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/> 
     <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/> 
     <Setter Property="Padding" Value="10,5,10,6"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <Grid Background="Transparent"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal"/> 
           <VisualState x:Name="MouseOver"/> 
           <VisualState x:Name="Pressed"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource aquabrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Height" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="70"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Disabled" /> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="FocusStates"/> 
         </VisualStateManager.VisualStateGroups> 
         <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}"> 
          <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/> 
         </Border> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</phone:PhoneApplicationPage.Resources> 

<Grid x:Name="LayoutRoot" Background="Transparent"> 
    <Button Content="pressed?" Style="{StaticResource AlternativeButtonStyle}" /> 
</Grid>