IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Besoin d'aide - Animation wpf [Débutant]


Sujet :

C#

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Inscrit en
    Octobre 2013
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Octobre 2013
    Messages : 11
    Points : 7
    Points
    7
    Par défaut Besoin d'aide - Animation wpf
    Bonsoir tous le monde,
    je vient de débuté a faire des animations avec wpf, je veux faire une animation qui fait changer la couleur d'un rectangle, je n'arrive pas a trouvé la propriété approprier, voici mon code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
     public partial class MainWindow : Window
        {
            private Storyboard myStoryboard;
     
            public MainWindow()
            {
                InitializeComponent();
     
                StackPanel myPanel = new StackPanel();
                myPanel.Margin = new Thickness(10);
     
                Rectangle myRectangle = new Rectangle();
                myRectangle.Name = "myRectangle";
                this.RegisterName(myRectangle.Name, myRectangle);
                myRectangle.Width = 100;
                myRectangle.Height = 100;
                myRectangle.Fill = Brushes.Green;
     
                ColorAnimation myDoubleAnimation = new ColorAnimation();
                myDoubleAnimation.From = Colors.Blue ;
                myDoubleAnimation.To = Colors.Yellow;
                myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));
                myDoubleAnimation.AutoReverse = true;
                myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
     
                myStoryboard = new Storyboard();
                myStoryboard.Children.Add(myDoubleAnimation);
                Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name);
                Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.ColorProperty));
     
                // Use the Loaded event to start the Storyboard.
                myRectangle.Loaded += new RoutedEventHandler(myRectangleLoaded);
                myPanel.Children.Add(myRectangle);
                this.Content = myPanel;
            }
     
            private void myRectangleLoaded(object sender, RoutedEventArgs e)
            {
                myStoryboard.Begin(this);
            }
        }

    Merci d'avance.

  2. #2
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut
    bonjour
    Il faut utiliser les balises code
    c'est la prop Fill (qui est un Brush) du Rectangle que tu dois animer et ensuite il est applique au rectangle...Il n' y a de prop color dans un Rectagle,Ellipse ou Line...
    Code exemple MSDN a etudier attentivement (c'est sur button mais c'est kif-kif):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
     
     
    /*
     
       This sample demonstrates how to apply non-storyboard animations to a property.
       To animate in markup, you must use storyboards.
     
    */
     
    using System;
    using System.Windows;
    using System.Windows.Navigation;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Windows.Controls;
     
    namespace Microsoft.Samples.Animation.LocalAnimations
    {
     
        // Create the demonstration.
        public class LocalAnimationExample : Page 
        {
     
     
     
     
            public LocalAnimationExample()
            {
     
     
                WindowTitle = "Local Animation Example";
                StackPanel myStackPanel = new StackPanel();
                myStackPanel.Margin = new Thickness(20);                     
     
     
                // Create and set the Button.
                Button aButton = new Button();
                aButton.Content = "A Button";
     
                // Animate the Button's Width.
                DoubleAnimation myDoubleAnimation = new DoubleAnimation();
                myDoubleAnimation.From = 75;
                myDoubleAnimation.To = 300;
                myDoubleAnimation.Duration =  new Duration(TimeSpan.FromSeconds(5));
                myDoubleAnimation.AutoReverse = true;
                myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
     
                // Apply the animation to the button's Width property.
                aButton.BeginAnimation(Button.WidthProperty, myDoubleAnimation);       
     
                // Create and animate a Brush to set the button's Background.
                SolidColorBrush myBrush = new SolidColorBrush();
                myBrush.Color = Colors.Blue;            
     
                ColorAnimation myColorAnimation = new ColorAnimation();
                myColorAnimation.From = Colors.Blue;
                myColorAnimation.To = Colors.Red;
                myColorAnimation.Duration =  new Duration(TimeSpan.FromMilliseconds(7000));
                myColorAnimation.AutoReverse = true;
                myColorAnimation.RepeatBehavior = RepeatBehavior.Forever;
     
                // Apply the animation to the brush's Color property.
                myBrush.BeginAnimation(SolidColorBrush.ColorProperty, myColorAnimation);           
                aButton.Background = myBrush;
     
                // Add the Button to the panel.
                myStackPanel.Children.Add(aButton);
                this.Content = myStackPanel;
            }
        }
     
    }
    bon code.........

  3. #3
    Futur Membre du Club
    Homme Profil pro
    Inscrit en
    Octobre 2013
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Octobre 2013
    Messages : 11
    Points : 7
    Points
    7
    Par défaut
    Bonjour,
    merci MABROUKI pour votre aide, mais j'ai pas pu régler le problème .

  4. #4
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut
    re
    J'ai envoye une animation par code avec le timeline directement sans utiliser une table de montage sequentiel (storyboard )qui est plus adapte specialement pour le xaml que le code behind .cs...

    C'est faisable mais ton code comporte des lacunes notables à cause des objets Shapes (rectangle ,ellipse et autres ) ,des objets Brushes ,et des objets Geometry qui necessitent de declarer une portee de nom du Form et l'enregistrement de ces objets dans la portee...

    Ce qui est animee bien sur c'est toujours la prop Color du Brush qui sera affectee à la prop Fill du rectangle...Sauf que pour un control (comme un button) on n'as pas besoin du Namescope...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
     
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Windows.Media.Animation;
     
    namespace WpfAnimationCode
    {
        /// <summary>
        /// Logique d'interaction pour MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            private Storyboard myStoryboard;
            private Rectangle myRectangle;
            public MainWindow()
            {
                InitializeComponent();
                //rajout********************
                NameScope.SetNameScope(this, new NameScope());
     
                StackPanel myPanel = new StackPanel();
                myPanel.Margin = new Thickness(10);
     
                myRectangle = new Rectangle();
                myRectangle.Name = "myRectangle";
                myRectangle.Width = 100;
                myRectangle.Height = 100;
                this.RegisterName("myRectangle", myRectangle);
     
     
                // Create and animate a Brush to set the rectangle's Fill.
                SolidColorBrush mySolidColorBrush = new SolidColorBrush();
                mySolidColorBrush.Color = Colors.Blue;
                //rajout********************
                this.RegisterName("mySolidColorBrush", mySolidColorBrush);
                myRectangle.Fill = mySolidColorBrush;
     
                //color for the brush
                ColorAnimation myColorAnimation = new ColorAnimation();
                myColorAnimation.From = Colors.Blue ;
                myColorAnimation.To = Colors.Yellow;
                myColorAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));
                myColorAnimation.AutoReverse = true;
                myColorAnimation.RepeatBehavior = RepeatBehavior.Forever;
     
                myStoryboard = new Storyboard();
                myStoryboard.Children.Add(myColorAnimation);
                //corrige ceci
                Storyboard.SetTargetName(myColorAnimation, "mySolidColorBrush");
                Storyboard.SetTargetProperty(myColorAnimation, new PropertyPath(SolidColorBrush.ColorProperty)); 
     
     
     
     
     
     
                // Use the Loaded event to start the Storyboard.
                myRectangle.Loaded += new RoutedEventHandler(myRectangleLoaded);
                myPanel.Children.Add(myRectangle);
                this.Content = myPanel;
            }
     
            private void myRectangleLoaded(object sender, RoutedEventArgs e)
            {
                myStoryboard.Begin(myRectangle);
            }
        }
    }
    Il faut faire attention à distinguer une animation par code avec le timeline et par un storyboard...
    bon code

  5. #5
    Futur Membre du Club
    Homme Profil pro
    Inscrit en
    Octobre 2013
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Octobre 2013
    Messages : 11
    Points : 7
    Points
    7
    Par défaut
    Merci Bien MABROUKI, et désolé pour le dérangement

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Animation wpf besoin d'aide
    Par HighTouch dans le forum Windows Presentation Foundation
    Réponses: 8
    Dernier message: 21/11/2008, 14h57
  2. Réponses: 1
    Dernier message: 27/10/2008, 15h27
  3. Réponses: 4
    Dernier message: 25/06/2007, 19h21
  4. [FLASH MX2004] Besoin d'aide créer animation
    Par Taz_8626 dans le forum Flash
    Réponses: 7
    Dernier message: 29/03/2006, 08h52

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo