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 :

WPF C# Animation RenderTransform


Sujet :

C#

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Avril 2015
    Messages : 7
    Points : 5
    Points
    5
    Par défaut WPF C# Animation RenderTransform
    Bonjour à tous,
    J'ai un problème au niveau des animations sous WPF :
    J'ai pas su translater un rectangle sur l'axe des 'X',
    S'il vous plait merci de m'aider à detecter l'erreur dans ce 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
    42
    43
    44
    45
    46
    47
    //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    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.Animation;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
     
    namespace Animation
    {
        /// <summary>
        /// Logique d'interaction pour MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
     
            private void btn_Click(object sender, RoutedEventArgs e)
            {
                Storyboard str = new Storyboard();
                DoubleAnimationUsingKeyFrames da = new DoubleAnimationUsingKeyFrames();
                da.Duration = new Duration(new TimeSpan(0, 0, 2));
                DoubleKeyFrame dkf = new EasingDoubleKeyFrame(0, new TimeSpan(0, 0, 0));
                DoubleKeyFrame dkf1 = new EasingDoubleKeyFrame(0, new TimeSpan(0, 0, 2));
     
                da.KeyFrames.Add(dkf);
                da.KeyFrames.Add(dkf1);
                Storyboard.SetTarget(da, rect);
                Storyboard.SetTargetProperty(da, new PropertyPath(TranslateTransform.XProperty));
                str.Children.Add(da);
                str.Begin();
            }
        }
    }
    //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

  2. #2
    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
    Bon chance!

  3. #3
    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 442
    Points
    4 442
    Par défaut
    bonjour
    Les rectangles et autres shapes , les types resources (brush,color ),les transformations , qui ne sont pas des Controls en WPF necessitent dans le code behind :
    - de declarer un NameScope (ou portee de nom) pour le Form..
    - de les enregistrer dans ce NameScop...
    pour etre anime ...
    Ensuite est mine et comporte beaucoup d'erreur (if faut examiner serieusement le xaml)...

    code behind .cs modifie fonctionnel:
    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
     
    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.Shapes;
    using System.Windows.Media.Animation;
     
    namespace WpfAnimation
    {
        /// <summary>
        /// Logique d'interaction pour Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
     
     
            }
     
            private void btn_Click(object sender, RoutedEventArgs e)
            {
                //portee de nom du form
                NameScope.SetNameScope(this, new NameScope());
     
                //ce bout de code est necessaire si le rectangle est declare dans le code et
                //ajoute au canvas 
                //this.RegisterName("myRectangle", this.rect);
     
                DoubleAnimationUsingKeyFrames da = new DoubleAnimationUsingKeyFrames();
                da.Duration = new Duration(new TimeSpan(0, 0, 2));
                DoubleKeyFrame dkf = new EasingDoubleKeyFrame(0, new TimeSpan(0, 0, 0));
                //corrige moi-ca sinon il se deplacera de 0 à 0 c.à.d oualou
                DoubleKeyFrame dkf1 = new EasingDoubleKeyFrame(50, new TimeSpan(0, 0, 2));
     
                da.KeyFrames.Add(dkf);
                da.KeyFrames.Add(dkf1);
                da.RepeatBehavior = RepeatBehavior.Forever;
     
                //omission 
                TranslateTransform tr = new TranslateTransform();
                this.RegisterName("myRectTranslate", tr);
                rect.RenderTransform = tr;
     
                Storyboard sb = new Storyboard();
                sb.Children.Add(da);
     
                ////Storyboard.SetTarget(da, rect); 
                Storyboard.SetTargetName(da, "myRectTranslate");
                Storyboard.SetTargetProperty(da, new PropertyPath(TranslateTransform.XProperty));
                sb.Children.Add(da);
     
                //sb.Begin( );
                sb.Begin(rect);
            }
        }
    }
    et xaml du form:
    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
     
    <Window x:Class="WpfAnimation.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window1" Height="300" Width="300">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Button x:Name="btn" Content="AnimateRect" Click="btn_Click"></Button>
            <Canvas 
                Grid.Row="1"
                x:Name="myPanel">
                <Rectangle 
                    x:Name="rect" Fill="LimeGreen" Stroke="Red" StrokeThickness="4" Width="100" Height="50">
                </Rectangle>
     
            </Canvas >
        </Grid>
    </Window>

    bon code....

  4. #4
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Avril 2015
    Messages : 7
    Points : 5
    Points
    5
    Par défaut
    Baraka laho fikoum Mabrouki, ça marche parfaitement.

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

Discussions similaires

  1. [WPF] ItemsControl & Animation
    Par NeoKript dans le forum Windows Presentation Foundation
    Réponses: 8
    Dernier message: 19/07/2011, 11h25
  2. [WPF Double Animation]
    Par GregLeOuf dans le forum Windows Presentation Foundation
    Réponses: 9
    Dernier message: 22/02/2011, 14h37
  3. WPF - Animer un objet
    Par window62123 dans le forum C#
    Réponses: 7
    Dernier message: 16/04/2009, 10h37
  4. Animation wpf besoin d'aide
    Par HighTouch dans le forum Windows Presentation Foundation
    Réponses: 8
    Dernier message: 21/11/2008, 13h57
  5. [WPF-Blend] Plusieurs objets mais une seule animation
    Par Tuizi dans le forum Framework .NET
    Réponses: 12
    Dernier message: 11/12/2007, 17h10

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