2015-03-19 79 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