【发布时间】:2017-08-08 16:29:44
【问题描述】:
我正在尝试使用Storyboard 调整 WPF 窗口的大小。当我为窗口宽度或高度设置动画时,我的代码可以完美运行,但是当我在它们两个上应用情节提要时,它只会为第一个属性(在我的情况下为宽度)设置动画并跳过第二个。
我的代码是:
private void AnimateWindowSize(double width, double height, bool isRelative = false)
{
//---------------------------------------------------
// Settings
//---------------------------------------------------
var duration = TimeSpan.FromSeconds(0.2);
var newWidth = isRelative ? this.Width - width : width;
var newHeight = isRelative ? this.Height - height : height;
Console.WriteLine($"Animating window from {this.Width}x{this.Height} to {newWidth}x{newHeight}");
this.Dispatcher.BeginInvoke(new Action(() =>
{
var storyboard = new Storyboard();
//---------------------------------------------------
// Animate Width
//---------------------------------------------------
var widthAnimation = new DoubleAnimation()
{
Duration = new Duration(duration),
From = this.ActualWidth,
To = newWidth
};
Storyboard.SetTarget(widthAnimation, this);
Storyboard.SetTargetProperty(widthAnimation, new PropertyPath(Window.WidthProperty));
storyboard.Children.Add(widthAnimation);
//---------------------------------------------------
// Animate Width
//---------------------------------------------------
var heightAnimation = new DoubleAnimation()
{
Duration = new Duration(duration),
From = this.ActualHeight,
To = newHeight
};
Storyboard.SetTarget(heightAnimation, this);
Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(Window.HeightProperty));
storyboard.Children.Add(heightAnimation);
//---------------------------------------------------
// Play
//---------------------------------------------------
//storyboard.Begin();
this.BeginStoryboard(storyboard, HandoffBehavior.SnapshotAndReplace, false);
}), null);
}
顺便说一句,我已经创建了这个方法的通用版本,用于为我的菜单等大小设置动画,并且效果很好。这似乎只是 Window 动画的问题。有谁知道如何修复它并为窗口宽度和高度应用动画?我关注了 SO 中的答案,但所有这些都与 Window 以外的元素有关。
【问题讨论】: