首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >故事板DoubleAnimation以编程方式

故事板DoubleAnimation以编程方式
EN

Stack Overflow用户
提问于 2021-06-13 04:06:59
回答 1查看 162关注 0票数 0

我尝试以编程的方式来执行这个xaml代码。

代码语言:javascript
复制
<Grid.Triggers>
    <EventTrigger RoutedEvent="UIElement.MouseEnter">
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation Storyboard.TargetName="tranny" Storyboard.TargetProperty="ScaleX" To="1.2" Duration="0:0:1">
                    <DoubleAnimation.EasingFunction>
                         <ElasticEase Oscillations="1" Springiness="8"/>
                     </DoubleAnimation.EasingFunction>
                 </DoubleAnimation>
                 <DoubleAnimation Storyboard.TargetName="tranny" Storyboard.TargetProperty="ScaleY" To="1.2" Duration="0:0:1">
                     <DoubleAnimation.EasingFunction>
                         <ElasticEase Oscillations="1" Springiness="8" />
                     </DoubleAnimation.EasingFunction>
                 </DoubleAnimation>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</Grid.Triggers>

它这样做了,但它没有工作,但我不知道为什么,但没有错误,如果有任何错误,我们可以用C#编写这个xaml,以编程方式回答我

如果你有答案,可以帮我添加,如果没有,请不要给-1,这是我写的编程代码。

代码语言:javascript
复制
var secondryGrid = new Grid();
var elasticEase = new ElasticEase();
elasticEase.Oscillations = 1;
elasticEase.Springiness = 8;
var timeSpan = new TimeSpan(0, 0, 0, 100);
var duration = new Duration(timeSpan);
var doubleAnimation = new DoubleAnimation();
doubleAnimation.To = 1.2;
doubleAnimation.Duration = duration;
doubleAnimation.EasingFunction = elasticEase;
var path = new PropertyPath("ScaleX");
var storyBoard = new Storyboard();
Storyboard.SetTargetProperty(doubleAnimation, path);
storyBoard.Children.Add(doubleAnimation);
var beginStoryBoard = new BeginStoryboard();
beginStoryBoard.Storyboard = storyBoard;
elasticEase = new ElasticEase();
elasticEase.Oscillations = 1;
elasticEase.Springiness = 8;
timeSpan = new TimeSpan(0, 0, 0, 100);
duration = new Duration(timeSpan);
doubleAnimation = new DoubleAnimation();
doubleAnimation.To = 1.2;
doubleAnimation.Duration = duration;
doubleAnimation.EasingFunction = elasticEase;
path = new PropertyPath("ScaleY");
Storyboard.SetTargetProperty(doubleAnimation, path);
storyBoard.Children.Add(doubleAnimation);
var gridEventTrigger = new EventTrigger();
gridEventTrigger.RoutedEvent = MouseEnterEvent;
gridEventTrigger.Actions.Add(beginStoryBoard);
secondryGrid.Triggers.Add(gridEventTrigger);

抱歉,我说错话了

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-13 10:40:01

在代码中,这样做稍微容易一些。您不需要实例化BeginStoryboard和Storyboard;这些类的设计目的是使XAML中的动画更容易使用。在代码中,可以直接将动画设置为所需的属性。这需要大大减少代码。

示例:

代码语言:javascript
复制
        {
            Grid grid = new Grid();
            grid.MouseEnter += OnGridMouseEnter;
        }

        // An animation instance is created once and can then be reused.
        private static readonly DoubleAnimation xyAnimation = new DoubleAnimation(1.2, TimeSpan.FromSeconds(1))
        {
            EasingFunction = new ElasticEase()
            {
                Oscillations = 1,
                Springiness = 8
            }
        };

        private void OnGridMouseEnter(object sender, MouseEventArgs e)
        {
            tranny.BeginAnimation(ScaleTransform.ScaleXProperty, xyAnimation);
            tranny.BeginAnimation(ScaleTransform.ScaleYProperty, xyAnimation);
        }

P.S.从您的代码中不清楚如何创建tranny转换。

创建我的示例的前提是,有一个包含需要动画的转换的tranny字段。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67954763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档