Chr*_*667 6 c# wpf animation storyboard
我写了一个WindowExtension,它应该为窗口提供一个简单的Translate动画.但是这个动画总是在它到达目标坐标之前停止.任何人都可以给我一个建议吗?
最好的问候克里斯
public static class WindowExtensions
{
public static void Translate(this Window element, double x, double y, TimeSpan duration)
{
NameScope.SetNameScope(element, new NameScope());
var xAnimation = new DoubleAnimationUsingKeyFrames {Duration = duration};
xAnimation.KeyFrames.Add(new EasingDoubleKeyFrame(element.Left, KeyTime.FromPercent(0)));
xAnimation.KeyFrames.Add(new EasingDoubleKeyFrame(x, KeyTime.FromPercent(1)));
var yAnimation = new DoubleAnimationUsingKeyFrames {Duration = duration};
yAnimation.KeyFrames.Add(new EasingDoubleKeyFrame(element.Top, KeyTime.FromPercent(0)));
yAnimation.KeyFrames.Add(new EasingDoubleKeyFrame(y, KeyTime.FromPercent(1)));
var storyboard = new Storyboard()
{
Children = { xAnimation, yAnimation }
};
Storyboard.SetTargetProperty(xAnimation, new PropertyPath("(Window.Left)"));
Storyboard.SetTargetProperty(yAnimation, new PropertyPath("(Window.Top)"));
storyboard.Duration = duration;
storyboard.FillBehavior = FillBehavior.Stop;
storyboard.Completed += (sender, args) =>
{
storyboard.SkipToFill();
storyboard.Remove(element);
};
storyboard.Begin(element);
}
}
Run Code Online (Sandbox Code Playgroud)
它可以在WPF窗口中进行简单测试,如下所示:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Translate(10,10, TimeSpan.FromMilliseconds(250));
}
}
Run Code Online (Sandbox Code Playgroud)
看起来像一个错字。最有可能的是因为你的xAnimationanimatesWindow.Top和yAnimationanimates Window.Left:
Storyboard.SetTargetProperty(xAnimation, new PropertyPath("(Window.Left)"));
Storyboard.SetTargetProperty(yAnimation, new PropertyPath("(Window.Top)"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
811 次 |
| 最近记录: |