故事板动画

发布于 2024-12-29 12:16:50 字数 1431 浏览 2 评论 0原文

我制作了一个小型 WPF 应用程序来滑入和滑出 UserControl,我遇到的问题是当 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);
}

I made a small WPF application to slide UserControls in and out, the problem I have is when a UserControl animated to slide in, then I click the button to slide another UserControl in, they both slide out - I'm not sure why this is?

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拒绝两难 2025-01-05 12:16:50

您是否在某处设置了 UserControl 的 Name 属性?或者更准确地说,您是否为第二个 UserControl 指定了与第一个 UserControl 不同的名称?您的动画设置了目标,

Storyboard.SetTargetName(tAnimation, uc.Name);

因此名称应该不同。或者,您可以致电

Storyboard.SetTarget(tAnimation, uc);

指定目标。

Do you set the Name property of the UserControl somewhere? Or to be more precise, do you give the second UserControl a different Name than the first one? Your animations get their target set by

Storyboard.SetTargetName(tAnimation, uc.Name);

so names should be different. Alternatively you may call

Storyboard.SetTarget(tAnimation, uc);

to specify the target.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文