我做了一個小的WPF應用程序來滑動UserControls進出,我遇到的問題是UserControl動畫滑動時,然後單擊按鈕滑動另一個UserControl,它們都滑出 - 我是不知道這是爲什麼?故事板動畫
public UserControl1()
{
InitializeComponent();
}
public void SlideIn(UserControl uc)
{
ThicknessAnimation tAnimation = new ThicknessAnimation();
tAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));
tAnimation.From = new Thickness(500, 0, -500, 0);
tAnimation.To = new Thickness(0);
tAnimation.DecelerationRatio = 0.9;
Storyboard.SetTargetName(tAnimation, uc.Name);
Storyboard.SetTargetProperty(tAnimation, new PropertyPath(MarginProperty));
Storyboard storyboard = new Storyboard();
storyboard.Children.Add(tAnimation);
storyboard.Begin(uc);
this.Content = uc;
}
public void SlideOut(UserControl uc)
{
ThicknessAnimation tAnimation = new ThicknessAnimation();
tAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));
tAnimation.To = new Thickness(-500, 0, 500, 0);
tAnimation.DecelerationRatio = 0.9;
Storyboard.SetTargetName(tAnimation, uc.Name);
Storyboard.SetTargetProperty(tAnimation, new PropertyPath(MarginProperty));
Storyboard storyboard = new Storyboard();
storyboard.Children.Add(tAnimation);
storyboard.Begin(this);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
SlideOut(userControl1);
UserControl2 uc2 = new UserControl2();
SlideIn(uc2);
}
我試過'storyboard.Begin(uc);'但結果相同。 – Michael 2012-01-29 16:07:13