Kan*_*ane 5 .net c# wpf animation
我想在我的WPF项目中切换控件的不透明度(Button,TextBox,Panel等),并想检查我是否正确完成了它.
我的问题是:这是您通常在XAML中编写的功能类型,还是使用类似下面的代码来实现淡入/淡出结果?
internal static class AnimationExtensions
{
internal enum TransitionSpeed
{
Instant = 0,
Fast = 100,
Normal = 200,
Slow = 500
}
/// <summary>
/// Toggles the opacity of a control.
/// </summary>
/// <param name="control">The control.</param>
internal static void ToggleControlFade(this Control control)
{
control.ToggleControlFade(TransitionSpeed.Normal);
}
/// <summary>
/// Toggles the opacity of a control.
/// </summary>
/// <param name="control">The control.</param>
/// <param name="speed">The speed.</param>
internal static void ToggleControlFade(this Control control, TransitionSpeed speed)
{
Storyboard storyboard = new Storyboard();
TimeSpan duration = new TimeSpan(0, 0, 0, 0, (int)speed); //
DoubleAnimation animation = new DoubleAnimation { From = 1.0, To = 0.0, Duration = new Duration(duration) };
if (control.Opacity == 0.0)
{
animation = new DoubleAnimation { From = 0.0, To = 1.0, Duration = new Duration(duration) };
}
Storyboard.SetTargetName(animation, control.Name);
Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity", 0));
storyboard.Children.Add(animation);
storyboard.Begin(control);
}
}
Run Code Online (Sandbox Code Playgroud)
你可能会说我对WPF非常非常新.
谢谢
我倾向于发现在需要在动画之后执行操作的地方或者动画依赖于复杂触发器的地方,代码隐藏是最好的地方,否则 XAML 是放置动画的好地方。(我通常对诸如转换或简单的“onclick”事件之类的事情执行此操作。
| 归档时间: |
|
| 查看次数: |
8256 次 |
| 最近记录: |