c0D*_*g1c 3 .net wpf animation window
下面是窗口FadeIn和FadeOut动画的代码片段:
// Create the fade in storyboard
fadeInStoryboard = new Storyboard();
fadeInStoryboard.Completed += new EventHandler(fadeInStoryboard_Completed);
DoubleAnimation fadeInAnimation = new DoubleAnimation(0.0, 1.0, new Duration(TimeSpan.FromSeconds(0.30)));
Storyboard.SetTarget(fadeInAnimation, this);
Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath(UIElement.OpacityProperty));
fadeInStoryboard.Children.Add(fadeInAnimation);
// Create the fade out storyboard
fadeOutStoryboard = new Storyboard();
fadeOutStoryboard.Completed += new EventHandler(fadeOutStoryboard_Completed);
DoubleAnimation fadeOutAnimation = new DoubleAnimation(1.0, 0.0, new Duration(TimeSpan.FromSeconds(0.30)));
Storyboard.SetTarget(fadeOutAnimation, this);
Storyboard.SetTargetProperty(fadeOutAnimation, new PropertyPath(UIElement.OpacityProperty));
fadeOutStoryboard.Children.Add(fadeOutAnimation);
Run Code Online (Sandbox Code Playgroud)
以下是触发动画的辅助方法:
/// <summary>
/// Fades the window in.
/// </summary>
public void FadeIn()
{
// Begin fade in animation
this.Dispatcher.BeginInvoke(new Action(fadeInStoryboard.Begin), DispatcherPriority.Render, null);
}
/// <summary>
/// Fades the window out.
/// </summary>
public void FadeOut()
{
// Begin fade out animation
this.Dispatcher.BeginInvoke(new Action(fadeOutStoryboard.Begin), DispatcherPriority.Render, null);
}
Run Code Online (Sandbox Code Playgroud)
除了两个问题外,代码工作得很好:
为什么会这样?如何在没有黑色背景故障的情况下使这个动画顺利运行?
您需要设置AllowsTransparency到true在Window它从完全透明上来.
不幸的是,这只能用WindowStyle=None,所以你必须实现自己的标题栏.
这带来了一些讨厌的性能问题,因为窗口不能再进行硬件渲染.如果你这样做,我强烈建议在UI线程上设置RenderOptions.ProcessRenderMode为RenderMode.SoftwareOnly(.NET 4.0或更高版本),以便在简单的合成中获得可接受的性能.
| 归档时间: |
|
| 查看次数: |
5351 次 |
| 最近记录: |