简体   繁体   中英

Storyboard DoubleAnimation programmatically

i try to do this xaml code programmatically

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

it do it but it didn't work with i don't know why but there is no errors if there is any onwe can write this xaml in C# programmatically answer me

please if you have answer can help me add it if no please don't give -1 that is programmatically code that i wrote

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

sorry for my bad language

In code, this is done somewhat easier. You don't need to instantiate BeginStoryboard and Storyboard; these classes are designed to make it easier to use animations in XAML. In code, you can set the animation directly to the desired property. This requires significantly less code.

Example:

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

PS It is not clear from your code how you create the tranny transformation.
My example is created on the assumption that there is a tranny field that contains the transformation that needs to be animated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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