简体   繁体   English

WPF 第二个 Animation(故事板)不起作用

[英]WPF Second Animation (StoryBoard) doesn't work

This is the scenario: I Have a Grid and an Button .这是场景:我有一个Grid和一个Button the Grid have a Show Boolean property.网格有一个Show Boolean 属性。 When user click on button, if Show=false then Grid become Visible .当用户单击按钮时,如果Show=false则 Grid 变为Visible and when Show=true Grid become Collapse .Show=true Grid 变为Collapse时。 when it's visibility change, I do an animation for grid's Height with StoryBoard .当它的能见度改变时,我用 StoryBoard 为网格的Height做一个StoryBoard
OK.好的。 every thing is fine.一切都很好。 when user click the button, Grid appearing with animation.当用户单击按钮时, Grid出现 animation。 but when click again, the animation doesn't show and just Collapse occurred.但是当再次点击时,animation 没有显示,只是发生了崩溃。
This is my code:这是我的代码:

Storyboard growUpStory = new Storyboard();
    Storyboard growDownStory = new Storyboard();

    DoubleAnimation growUp, growDown;

In any method:在任何方法中:

    height = Container.DesiredSize.Height;
    growUp = new DoubleAnimation
                {
                    From = 0,
                    To = height > MaxHeight ? MaxHeight : height,
                    Duration = new Duration(TimeSpan.FromSeconds(1)),
                    AutoReverse = false
                };
               
                Container.Visibility = Visibility.Collapsed;

                growDown = new DoubleAnimation
                {
                    From = height > MaxHeight ? MaxHeight : height,
                    To = 0,
                    Duration = new Duration(TimeSpan.FromSeconds(1)),
                    AutoReverse = false
                };

                growUpStory.Children.Add(growUp);
                growDownStory.Children.Add(growDown);

and when I invoke the event:当我调用事件时:

if (Show)
            {
                Storyboard.SetTarget(growUpStory, Container);
                Storyboard.SetTargetProperty(growUpStory, new PropertyPath(Grid.HeightProperty));
                growUpStory.Begin();

                Container.Visibility = Visibility.Visible;
            }
            else
            {
                Storyboard.SetTarget(growDownStory, Container);
                Storyboard.SetTargetProperty(growDownStory, new PropertyPath(Grid.HeightProperty));
                growDownStory.Begin();
                Container.Visibility = Visibility.Collapsed;
            }

I Try many ways but was unable to resolve.我尝试了很多方法,但无法解决。 Where is my problem?我的问题在哪里? have you any idea?你有什么想法吗? thanks.谢谢。

  • I Can't use XAML here我不能在这里使用XAML

Animations are asynchronous, which means you are collapsing the Visibility at the same time that you are starting the "growDownStory" animation.动画是异步的,这意味着您在开始“growDownStory”animation 的同时折叠可见性。

Add some delay, based on how long you expect your "growDownStory" animation to run.根据您希望“growDownStory”animation 运行多长时间添加一些延迟。

Example:例子:

    growDownStory.Begin();
    await Task.Delay(1000); // allow time for the animation to perform its visual
    Container.Visibility = Visibility.Collapsed;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM