Jump to content



Photo

c# metro fade multiple objects

c# metro doubleanimation

  • Please log in to reply
4 replies to this topic

#1 James Rose

James Rose

    Software Developer

  • Tech Issues Solved: 1
  • Joined: 20-January 04
  • Location: New York City

Posted 02 July 2013 - 00:52

Hello gang,

 

I am able to make single object fade in and fade out as needed.  However, when I attempt the following code it fails on this line:  MenuFadeStoryboard.Begin();   It fails with "Unspecified Error"    How helpful is that?

 

Any thoughts?

                    Storyboard MenuFadeStoryboard = new Storyboard();
                    DoubleAnimation FadeOutMenus = new DoubleAnimation();
                    FadeOutMenus.From = 1.0;
                    FadeOutMenus.To = 0.0;
                    FadeOutMenus.Duration = new TimeSpan(0, 0, 0, 1, 600); //TODO: needs to be shorter
                    
                    MenuFadeStoryboard.Children.Add(FadeOutMenus);

                    foreach (BediaNVMenu objBediaMenuUI in BediaMenuItems.Items)
                    {
                        Storyboard.SetTarget(FadeOutMenus, objBediaMenuUI);
                        
                        
                    }
                    Storyboard.SetTargetProperty(FadeOutMenus, "Opacity");  
                    MenuFadeStoryboard.Children.Add(FadeOutMenus);  //fails  here
                    MenuFadeStoryboard.Begin();



#2 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 12
  • Joined: 26-October 05

Posted 02 July 2013 - 03:46

Hm, does a Storyboard really have an "Opacity" property? I don't see it in the class description.

 

By the way, local variables should be in camelCase in C#, stuff like 

DoubleAnimation FadeOutMenus = new DoubleAnimation();

 really confuses me. :p



#3 greenwizard88

greenwizard88

    Neowinian Senior

  • Tech Issues Solved: 1
  • Joined: 28-November 04

Posted 02 July 2013 - 04:03

It looks like you're adding an animation where you should be adding objects. I think you'd want to create a bunch of storyboards - one for each object - and then set the storyboard target to be the object you're fading out, not the other way around.

 

Of course I could never get storyboards to work with > 5 items without getting stutter, so what do I know? :D



#4 +virtorio

virtorio

    4089 III

  • Tech Issues Solved: 12
  • Joined: 28-April 03
  • Location: New Zealand
  • OS: OSX 10.9, Windows 8.1
  • Phone: Samsung Galaxy SIII

Posted 02 July 2013 - 04:37

I would assume you're getting that error because that animation has already been added to the Storyboard (Line 6).

 

I also believe this is not the correct way to animate multiple objects. If I understand correctly it should be something more like this:

Storyboard storyboard = new Storyboard();
 
foreach (BediaNVMenu objBediaMenuUI in BediaMenuItems.Items)
{
  DoubleAnimation fadeOutMenus = new DoubleAnimation();
  fadeOutMenus.From = 1.0;
  fadeOutMenus.To = 0.0;
  fadeOutMenus.Duration = new TimeSpan(0, 0, 0, 1, 600);
  Storyboard.SetTarget(fadeOutMenus, objBediaMenuUI);
  Storyboard.SetTargetProperty(fadeOutMenus, "Opacity");  
  MenuFadeStoryboard.Children.Add(fadeOutMenus);
}
storyboard.Begin();

I've got a pretty limited understanding of how this works so, I might be wrong.

 

Edit: Also, shouldn't you also be using PropertyPath instead of just "Opacity", e.g.

  Storyboard.SetTargetProperty(fadeOutMenus, new PropertyPath(BediaNVMenu.Opacity)); 

I don't know - really not a fan of this API/framework.



#5 ~Johnny

~Johnny

    Earthling

  • Joined: 10-August 08
  • Location: London, England, Earth, Milky Way

Posted 02 July 2013 - 21:31

Yep - as mentioned - a double animation can only target a single "physical" property as it were. I.E, with one double animation you can animate one property of one element. Ergo, you'll have to create DoubleAnimations in loops for each element and add them into the Storyboard.

 

However, if you're not staggering the animations but rather fading them all out at the same time - it will be more efficient if you animate the actual ItemsControl or ItemsPanel instead rather than each individual element.

 

(In regards to the PropertyPath, as far as I'm aware the framework will work with any of the strings you'll see in XAML storyboards created by Blend for TargetProperties, and just "Opacity" is valid - though "FrameworkElement.Opacity" would also work it makes no difference)

 

(Both of the apps in my signature, especially the second, have all of their animations generated procedurally in C# in loops rather than using XAML storyboards or ThemeAnimations, which is why I jump on these animation question so much - I've spent a lot of time beating it with hammers. I have my own library that is meant to make things *slightly* easier that I might share publicly at some point if I tidy it up  :p )