SilverLight学习笔记--建立Silverlight自定义控件(5)--绑定动画效果
有了上述的基础,我们进一步完善我们的自定义控件,在此我们将创建Storyboard和前面的添加事件处理方法为我们的自定义控件加上动画效果。
1、首先,在MyDesignButton项目中进一步完善我们的generic.xaml文件。为其加入storyboard,其中,我们可以自己利用Expression blend来帮助我们生成任何我们想要的动画效果,然后截取动画代码到generic.xaml文件中,只是要注意其放置的位置,这样便于我们在代码操作中定位。两段storyboard代码如下:
_root = (FrameworkElement)GetTemplateChild("RootElement");
_body = (FrameworkElement)GetTemplateChild(MySilverButton.BodyElement);
if (_root != null)
{
_enter = (Storyboard)_root.Resources[MySilverButton.MouseEnterAnimation];
_leave = (Storyboard)_root.Resources[MySilverButton.MouseLeaveAnimation];
}
前往:Silverlight学习笔记清单
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>
Code
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事件的委托处理函数
void SimpleButton_MouseEnter(object sender, MouseEventArgs e)
{
try
{
Rectangle BgRect = GetTemplateChild("BodyElement") as Rectangle;
BgRect.Fill = new SolidColorBrush(Colors.Blue);
if (_enter != null)
_enter.Begin();
}
catch
{ }
}
void SimpleButton_MouseLeave(object sender, MouseEventArgs e)
{
try
{
Rectangle BgRect = GetTemplateChild("BodyElement") as Rectangle;
BgRect.Fill = new SolidColorBrush(Colors.Brown);
if (_leave != null)
_leave.Begin();
}
catch
{ }
}
{
try
{
Rectangle BgRect = GetTemplateChild("BodyElement") as Rectangle;
BgRect.Fill = new SolidColorBrush(Colors.Blue);
if (_enter != null)
_enter.Begin();
}
catch
{ }
}
void SimpleButton_MouseLeave(object sender, MouseEventArgs e)
{
try
{
Rectangle BgRect = GetTemplateChild("BodyElement") as Rectangle;
BgRect.Fill = new SolidColorBrush(Colors.Brown);
if (_leave != null)
_leave.Begin();
}
catch
{ }
}
(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代码如下:
Code
回到MySLbutton项目,测试结果,可以看到我们加入的动画效果。
前往:Silverlight学习笔记清单