我想要对一个WPF窗口的宽度和高度进行动画处理。我已经尝试了下面的代码,但是很遗憾只有宽度会被动画处理,而窗口的高度没有变化。我相信我错过了一些细节,希望在这里发布后能够有人看到我的错误!下面是一个简单窗口的代码,其中包含一个按钮,我已经将其连接到了调整大小的功能:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.AnimateWindowSize(ActualWidth + 200, ActualHeight + 200);
}
}
这里是我编写的动画代码,作为扩展方法,可以应用于任何窗口...
public static class WindowUtilties
{
public static void AnimateWindowSize(this Window target, double newWidth, double newHeight)
{
var sb = new Storyboard {Duration = new Duration(new TimeSpan(0, 0, 0, 0, 200))};
var aniWidth = new DoubleAnimationUsingKeyFrames();
var aniHeight = new DoubleAnimationUsingKeyFrames();
aniWidth.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 200));
aniHeight.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 200));
aniHeight.KeyFrames.Add(new EasingDoubleKeyFrame(target.ActualHeight, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, 00))));
aniHeight.KeyFrames.Add(new EasingDoubleKeyFrame(newHeight, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, 200))));
aniWidth.KeyFrames.Add(new EasingDoubleKeyFrame(target.ActualWidth, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, 00))));
aniWidth.KeyFrames.Add(new EasingDoubleKeyFrame(newWidth, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, 200))));
Storyboard.SetTarget(aniWidth, target);
Storyboard.SetTargetProperty(aniWidth, new PropertyPath(Window.WidthProperty));
Storyboard.SetTarget(aniHeight, target);
Storyboard.SetTargetProperty(aniHeight, new PropertyPath(Window.HeightProperty));
sb.Children.Add(aniWidth);
sb.Children.Add(aniHeight);
sb.Begin();
}
}
提前感谢任何帮助。