AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.63k stars 2.22k forks source link

Animation created in code-behind is not working. #4042

Open SparkyTD opened 4 years ago

SparkyTD commented 4 years ago

I am trying to animate an Ellipse in a custom control. If I create the animation in XAML, it works, but if I try to recreate the same animation in c#, it has no effect.

This is the working XAML code:

<Style Selector="Ellipse.e1">
    <Style.Animations>
        <Animation Duration="0:0:0.5" IterationCount="INFINITE">
            <KeyFrame Cue="0%">
                <Setter Property="Opacity" Value="0"/>
            </KeyFrame>
            <KeyFrame Cue="100%">
                <Setter Property="Opacity" Value="1"/>
            </KeyFrame>
        </Animation>
    </Style.Animations>
</Style>

And this is the not working C# code:

var animDuration = TimeSpan.FromMilliseconds(500);

var style = new Style(x => x.OfType<Ellipse>().Class("e1"));
var animation = new Animation();
animation.Duration = animDuration;
animation.IterationCount = IterationCount.Infinite;
animation.Children.Add(new KeyFrame
{
    Cue = Cue.Parse("0%", CultureInfo.CurrentCulture),
    Setters = {new Setter(OpacityProperty, 0)}
});
animation.Children.Add(new KeyFrame
{
    Cue = Cue.Parse("100%", CultureInfo.CurrentCulture),
    Setters = {new Setter(OpacityProperty, 1)}
});
style.Animations.Add(animation);
grid.Styles.Add(style);

The grid variable is correctly set to the same grid object that holds the XAML style. The C# code is called in OnInitialized(), but I have also tried calling it from the constructor after AvaloniaXamlLoader.Load(this);.

Am I missing something?

Klaasgow commented 1 year ago

Same here: A AXAML opacity animation for an image works, but in code nothing happens after animation.RunAsync(image)

timunie commented 1 year ago

@Klaasgow if you have a sample to share and if you can tell us the Avalonia version you use, it may help us to tickle down the issue