Laden...

Animation im C# Code funktioniert nicht

Erstellt von Briefkasten vor 14 Jahren Letzter Beitrag vor 14 Jahren 1.684 Views
Briefkasten Themenstarter:in
446 Beiträge seit 2004
vor 14 Jahren
Animation im C# Code funktioniert nicht

Hallo,

ich habe folgende Animation im XAML Definiert:

<!--<Window.Triggers>
        <EventTrigger RoutedEvent="Window.PreviewKeyDown">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation Storyboard.TargetProperty="Background.Color" From="Black" To="Transparent" Duration="00:00:01.000" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Window.Triggers>-->

Diese Funktioniert auch.

Die Animation im C# Code habe ich wie folgt definiert:


        private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
        {
         
            ColorAnimation colorAnimation = new ColorAnimation();
            colorAnimation.Duration = new Duration(new TimeSpan(0,0,10));
            colorAnimation.From = Colors.Black;
            colorAnimation.To = Colors.Transparent;
            this.BeginAnimation(Window., colorAnimation);
            //this.Close();
            
        }

Ich erhalte je doch immer die Exception

"AnimationTimeline" vom Typ "System.Windows.Media.Animation.ColorAnimation" kann nicht zum Animieren der Background-Eigenschaft vom Typ "System.Windows.Media.Brush" verwendet werden.
Parametername: animation

Ich weiß aber nicht wie ich die Animation auf das TargetProperty Background.Color ansetzen kann.

Hat jemand eine Idee, wie ich das mache bzw. mit welchem C# Code die Animation lauffähig ist?

lg

Schaut mal im IRC vorbei:
Server: https://libera.chat/ ##chsarp

6.862 Beiträge seit 2003
vor 14 Jahren

Hallo,

du bräuchtest ne Referenz auf deinen Background Brush und müsstest dann eher sowas schreiben:

myBackgroundBrush.BeginAnimation(SolidColorBrush.ColorProperty,colorAnimation);

Baka wa shinanakya naoranai.

Mein XING Profil.

Briefkasten Themenstarter:in
446 Beiträge seit 2004
vor 14 Jahren

Hallo,

das funktioniert wunderbar. Danke.

Ich führe zuerst die Animation aus, dann this.Close(). Allerdings scheint es so als würde die Animation Asynchron ausgeführt.

Man sieht nämlich nichts von der Animation das Fenster Beendet sich sofort.

Gibt es eine Möglichkeit, der Animation su sagen, dass zuerst die Animation ausgeführt wird und erst, dann alle weiteren Befehle verarbeitet werden?

lg

Schaut mal im IRC vorbei:
Server: https://libera.chat/ ##chsarp

6.862 Beiträge seit 2003
vor 14 Jahren

Hallo,

nein Animationen sind immer asynchron. Aber die haben Events an denen man sich ranhängen kann, wie z.B. das Completed Event. Fürchte da musst du drauf warten.

Baka wa shinanakya naoranai.

Mein XING Profil.

297 Beiträge seit 2008
vor 14 Jahren

Hi,

wie talla bereits gesagt hat, kannst du dich an das Completed-Event der Animation hängen. Würde dann ungefähr so aussehen:

private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
    ColorAnimation colorAnimation = new ColorAnimation();
    colorAnimation.Duration = new Duration(new TimeSpan(0,0,10));
    colorAnimation.From = Colors.Black;
    colorAnimation.To = Colors.Transparent;
    colorAnimation.Completed += new EventHandler(OnColorAnimationCompleted);
    myBackgroundBrush.BeginAnimation(SolidColorBrush.ColorProperty, colorAnimation);
}

private void OnColorAnimationCompleted(object sender, EventArgs e)
{
    // Hier den Code, der nach Abschluss der Animation ausgeführt werden soll
    // z.B. wie oben: Fenster schließen
    this.Close();
}

There are 10 kind of people, those who understand binary and those who don't.