1、首先,在MyDesignButton项目中进一步完善我们的generic.xaml文件。为其加入storyboard,其中,我们可以自己利用Expression blend来帮助我们生成任何我们想要的动画效果,然后截取动画代码到generic.xaml文件中,只是要注意其放置的位置,这样便于我们在代码操作中定位。两段storyboard代码如下:
<
Storyboard x:Name
=
"
MouseEnterAnimation
"
>
< ColorAnimationUsingKeyFrames BeginTime = " 00:00:00 " Storyboard.TargetName = " BodyElement " Storyboard.TargetProperty = " (Shape.Fill).(SolidColorBrush.Color) " >
< SplineColorKeyFrame KeyTime = " 00:00:00 " Value = " #FFA52A2A " />
< SplineColorKeyFrame KeyTime = " 00:00:01 " Value = " #FF49A52A " />
</ ColorAnimationUsingKeyFrames >
</ Storyboard >
< Storyboard x:Name = " MouseLeaveAnimation " >
< ColorAnimationUsingKeyFrames BeginTime = " 00:00:00 " Storyboard.TargetName = " BodyElement " Storyboard.TargetProperty = " (Shape.Fill).(SolidColorBrush.Color) " >
< SplineColorKeyFrame KeyTime = " 00:00:00 " Value = " #FF5AA52A " />
< SplineColorKeyFrame KeyTime = " 00:00:01 " Value = " #FFA52A2A " />
</ ColorAnimationUsingKeyFrames >
</ Storyboard >
此两段代码(两段storyboard分别命名为:MouseEnterAnimation和 MouseLeaveAnimation)用于定义当我们的鼠标移入和移出我们的自定义控件时的外观动画表现。 在此,我们只是定义了其不同的颜色变化。到此generic.xaml的完整代码如下:
< ColorAnimationUsingKeyFrames BeginTime = " 00:00:00 " Storyboard.TargetName = " BodyElement " Storyboard.TargetProperty = " (Shape.Fill).(SolidColorBrush.Color) " >
< SplineColorKeyFrame KeyTime = " 00:00:00 " Value = " #FFA52A2A " />
< SplineColorKeyFrame KeyTime = " 00:00:01 " Value = " #FF49A52A " />
</ ColorAnimationUsingKeyFrames >
</ Storyboard >
< Storyboard x:Name = " MouseLeaveAnimation " >
< ColorAnimationUsingKeyFrames BeginTime = " 00:00:00 " Storyboard.TargetName = " BodyElement " Storyboard.TargetProperty = " (Shape.Fill).(SolidColorBrush.Color) " >
< SplineColorKeyFrame KeyTime = " 00:00:00 " Value = " #FF5AA52A " />
< SplineColorKeyFrame KeyTime = " 00:00:01 " Value = " #FFA52A2A " />
</ ColorAnimationUsingKeyFrames >
</ Storyboard >


<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:custom="clr-namespace:MyDesignButton">
<Style TargetType="custom:MySilverButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="custom:MySilverButton">
<Grid x:Name="RootElement">
<Grid.Resources>
<Storyboard x:Name="MouseEnterAnimation">
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="BodyElement" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
<SplineColorKeyFrame KeyTime="00:00:00" Value="#FFA52A2A"/>
<SplineColorKeyFrame KeyTime="00:00:01" Value="#FF49A52A"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Name="MouseLeaveAnimation">
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="BodyElement" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
<SplineColorKeyFrame KeyTime="00:00:00" Value="#FF5AA52A"/>
<SplineColorKeyFrame KeyTime="00:00:01" Value="#FFA52A2A"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</Grid.Resources>
<Rectangle x:Name="BodyElement" Width="200" Height="100" Fill="Brown" Stroke="Purple" RadiusX="16" RadiusY="16" />
<TextBlock x:Name="ButtonCaption" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="26" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
2、在C#代码中定义和绑定我们的动画事件
(1)、首先在MySilverButton.cs中定义一系列的变量,这些变量将用于我们对自定义控件内部进行操作
private
const
string
RootElement
=
"
RootElement
"
;
private const string BodyElement = " BodyElement " ;
private const string MouseEnterAnimation = " MouseEnterAnimation " ;
private const string MouseLeaveAnimation = " MouseLeaveAnimation " ;
private FrameworkElement _root;
private FrameworkElement _body;
private Storyboard _enter;
private Storyboard _leave;
private const string BodyElement = " BodyElement " ;
private const string MouseEnterAnimation = " MouseEnterAnimation " ;
private const string MouseLeaveAnimation = " MouseLeaveAnimation " ;
private FrameworkElement _root;
private FrameworkElement _body;
private Storyboard _enter;
private Storyboard _leave;
(2)、定义MouseEnter和MouseLeave事件的委托处理函数







































(3)、在OnApplyTemplate函数中加入下述代码。
_root = (FrameworkElement)GetTemplateChild( " RootElement " );
_body = (FrameworkElement)GetTemplateChild(MySilverButton.BodyElement);
if (_root != null )
{
_enter = (Storyboard)_root.Resources[MySilverButton.MouseEnterAnimation];
_leave = (Storyboard)_root.Resources[MySilverButton.MouseLeaveAnimation];
}
修改后,完整的MySilverButton.cs代码如下:



















































































































































































































回到MySLbutton项目,测试结果,可以看到我们加入的动画效果。
前往:Silverlight学习笔记清单