Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Animation problem

Animation problem

Scheduled Pinned Locked Moved WPF
questioncsharpwpfhelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fjparisIII
    wrote on last edited by
    #1

    I use animations all the time in WPF, but they seem to work completely different in Silverlight, principally because the elements being animated don't have a BeginAnimation method. So, given the element being animated, how can I find the Storyboard defining the animation?

    A 1 Reply Last reply
    0
    • F fjparisIII

      I use animations all the time in WPF, but they seem to work completely different in Silverlight, principally because the elements being animated don't have a BeginAnimation method. So, given the element being animated, how can I find the Storyboard defining the animation?

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      fjparisIII wrote:

      Storyboard defining the animation

      Not quite sure I understand your question. What you need to do is create a Storyboard - see here[^].

      Me, I'm dishonest. And a dishonest man you can always trust to be dishonest.
      Honestly. It's the honest ones you want to watch out for...

      F 1 Reply Last reply
      0
      • A Abhinav S

        fjparisIII wrote:

        Storyboard defining the animation

        Not quite sure I understand your question. What you need to do is create a Storyboard - see here[^].

        Me, I'm dishonest. And a dishonest man you can always trust to be dishonest.
        Honestly. It's the honest ones you want to watch out for...

        F Offline
        F Offline
        fjparisIII
        wrote on last edited by
        #3

        I thought it was clear that I had created my Storyboard in XAML. This is what I had before I made my first post:

        navigation:Page.Resources
        <Storyboard x:Name="image01fadeIn">
        <DoubleAnimation
        Storyboard.TargetName="image01"
        Storyboard.TargetProperty="Opacity"
        From="0" To="1"
        Duration="0:0:0.5"
        Completed="imag01FadeInAnimation_Completed"
        >
        </DoubleAnimation>
        </Storyboard>
        </navigation:Page.Resources>

        Now I'm getting an event where I have a reference to the element I want to start the above animation on. Given the reference to that the element (image01), how do I find the above Storyboard so I can start the animation? It was simple in WPF, because I could create the animation in code and use BeginAnimation() right there in the code. But in Silverlight, apparently you have to create the Storyboard in a XAML resource. Maybe my question should be: how would I replace the above XAML with in-line C# code?

        A 1 Reply Last reply
        0
        • F fjparisIII

          I thought it was clear that I had created my Storyboard in XAML. This is what I had before I made my first post:

          navigation:Page.Resources
          <Storyboard x:Name="image01fadeIn">
          <DoubleAnimation
          Storyboard.TargetName="image01"
          Storyboard.TargetProperty="Opacity"
          From="0" To="1"
          Duration="0:0:0.5"
          Completed="imag01FadeInAnimation_Completed"
          >
          </DoubleAnimation>
          </Storyboard>
          </navigation:Page.Resources>

          Now I'm getting an event where I have a reference to the element I want to start the above animation on. Given the reference to that the element (image01), how do I find the above Storyboard so I can start the animation? It was simple in WPF, because I could create the animation in code and use BeginAnimation() right there in the code. But in Silverlight, apparently you have to create the Storyboard in a XAML resource. Maybe my question should be: how would I replace the above XAML with in-line C# code?

          A Offline
          A Offline
          Abhinav S
          wrote on last edited by
          #4

          Your code in C# will look something very similar to - Storyboard sb = new Storyboard(); DoubleAnimation objAnimation = new DoubleAnimation(); objAnimation.From = 0; objAnimation.To = 1; objAnimation.Duration = new Duration(new TimeSpan(500)); objAnimation.Completed +=new EventHandler(imag01FadeInAnimation_Completed); Storyboard.SetTargetProperty(objAnimation, new PropertyPath("(Opacity)")); Storyboard.SetTarget(objAnimation, image1); sb.Children.Add(objAnimation); sb.Begin();

          Me, I'm dishonest. And a dishonest man you can always trust to be dishonest.
          Honestly. It's the honest ones you want to watch out for...

          F 1 Reply Last reply
          0
          • A Abhinav S

            Your code in C# will look something very similar to - Storyboard sb = new Storyboard(); DoubleAnimation objAnimation = new DoubleAnimation(); objAnimation.From = 0; objAnimation.To = 1; objAnimation.Duration = new Duration(new TimeSpan(500)); objAnimation.Completed +=new EventHandler(imag01FadeInAnimation_Completed); Storyboard.SetTargetProperty(objAnimation, new PropertyPath("(Opacity)")); Storyboard.SetTarget(objAnimation, image1); sb.Children.Add(objAnimation); sb.Begin();

            Me, I'm dishonest. And a dishonest man you can always trust to be dishonest.
            Honestly. It's the honest ones you want to watch out for...

            F Offline
            F Offline
            fjparisIII
            wrote on last edited by
            #5

            Exactly. Just after I posted my reply, I woke up and banged the side of my head with the palm of my hand. I work too many hours in the day and my brain stops working after about 12 hours of headscratching at my desk. My original post was written after 16 hours at the keyboard. Then when I woke up this morning I was still thinking I didn't have a clue. But my brain started waking up at the very end to my reply to your first answer. Then I sat down and wrote the following code (amazingly similar to yours):

            ImageBeingDisplayed.Source = BitmapImage;
            DoubleAnimation fadeInAnimation = new DoubleAnimation();
            fadeInAnimation.From = 0;
            fadeInAnimation.To = 1;
            fadeInAnimation.Duration = TimeSpan.FromSeconds(.5);
            fadeInAnimation.Completed += new EventHandler(fadeInAnimation_Completed);
            Storyboard storyboard = new Storyboard();
            Storyboard.SetTarget(fadeInAnimation, ImageBeingDisplayed);
            Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath("Image.Opacity"));
            storyboard.Children.Add(fadeInAnimation);
            storyboard.Begin();

            Then I wrote the fadeInAnimation_Completed event as follows:

            void fadeInAnimation_Completed(object sender, EventArgs e)
            {
            Storyboard storyboard = (Storyboard)sender;
            storyboard.Stop();
            string imageName = Storyboard.GetTargetName(storyboard);
            Image image = FindName(imageName) as Image;
            if (image != null)
            {
            image.Opacity = 1;
            }
            }

            I don't know whether all this will work because the first block of code is in a WebClient.DownloadDataCompleted event handler, which inexplicably does not exist in Silverlight (I'm trying to port my WPF application to Silverlight), so my Silverlight code does not compile. I asked how to solve that problem in a post immediately preceding the post that started this thread.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups