【问题标题】:Storyboard DoubleAnimation programmatically故事板 DoubleAnimation 以编程方式
【发布时间】:2021-08-29 10:41:39
【问题描述】:

我尝试以编程方式执行此 xaml 代码

<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,这是我编写的编程代码

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

抱歉我的语言不好

【问题讨论】:

    标签: c# wpf wpf-controls


    【解决方案1】:

    在代码中,这更容易完成。 您不需要实例化 BeginStoryboard 和 Storyboard;这些类旨在使在 XAML 中使用动画更容易。 在代码中,您可以将动画直接设置为所需的属性。 这需要更少的代码。

    例子:

            {
                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 字段包含需要动画的转换的情况下创建的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 2013-04-30
      • 1970-01-01
      相关资源
      最近更新 更多