• 0

c# metro fade multiple objects


Question

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();
Link to comment
https://www.neowin.net/forum/topic/1162326-c-metro-fade-multiple-objects/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

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

  • 0

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.

  • 0

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 )

This topic is now closed to further replies.
  • Posts

    • Fresh CachyOS install with Niri - I guess it's a little orange, but I'm working on it
    • FastStone Image Viewer 8.5 by Razvan Serea FastStone Image Viewer is a fast, stable, user-friendly image browser, converter and editor. It has a nice array of features that include image viewing, management, comparison, red-eye removal, emailing, resizing, cropping, retouching and color adjustments. Its innovative but intuitive full-screen mode provides quick access to EXIF information, thumbnail browser and major functionalities via hidden toolbars that pop up when your mouse touches the four edges of the screen. Other features include a high quality magnifier and a musical slideshow with 150+ transitional effects, as well as lossless JPEG transitions, drop shadow effects, image annotation, scanner support, histogram and much more. It supports all major graphic formats (BMP, JPEG, JPEG 2000, animated GIF, PNG, PCX, PSD, EPS, TIFF, WMF, ICO and TGA) and popular digital camera RAW formats (CRW, CR2, NEF, PEF, RAF, MRW, ORF, SRF, ARW, SR2, RW2 and DNG). FastStone Image Viewer features: Image browser and viewer with a familiar Windows Explorer-like user interface Support for many popular image formats and PDF viewing True Full Screen viewer with convenient image zoom support and unique fly-out menu panels Crystal-clear and customizable one-click image magnifier Powerful image editing tools: Resize/resample, rotate/flip, crop, sharpen/blur, adjust lighting/colors/curves/levels etc. Eleven re-sampling algorithms to choose from when resizing images Image color effects: gray scale, sepia, negative, Red/Green/Blue adjustment Image special effects: drop shadow, framing, bump map, sketch, oil painting, lens Draw texts, lines, highlights, rectangles, ovals and callout objects on images Clone Stamp and Healing Brush Superior red-eye effect removal/reduction with completely natural looking end result Multi-level Undo/Redo capability Single click to switch between best fit and actual size mode Image management, including file tagging, rating and drag-and-drop to copy/move/re-arrange files Histogram display with color counter feature Compare images side-by-side (up to 4 at a time) to easily cull those forgettable shots Image EXIF metadata support (plus comment editing for JPEGs) Configurable batch processing to convert/rename large or small collections of images Slideshow with 150+ transition effects and music support (MP3, WMA, WAV...) Create efficient image attachments for emailing to family and friends Print images with full page-layout control Create fully configurable contact sheets Create memorable artistic image montages from your family photos for personalized desktop wallpapers (Wallpaper Anywhere) Acquire images from scanners. Support batch scanning to PDF, TIFF, JPEG and PNG Versatile screen capture capability Powerful Save As interface to compare image quality and control generated file size Run favorite external editors with one keystroke from within Image Viewer Offer portable version of the program which can be run from a removable storage device Configurable mouse wheel support Support themes (bright, gray and dark) Support dual-monitor configurations Support touch interface (tap, swipe, pinch) Support dual instances Play video and audio files (Third party codecs may be required for old versions of Windows) And much more... FastStone Image Viewer 8.5 changelog: Added support for SVG format Added Start importing automatically and Handle duplicate file names automatically options to the Import Photos and Videos tool WebP files can now be rotated and saved with a single click Enhanced dark theme support in the PDF viewer Fixed a bug where some links in PDF files were not clickable Other improvements and bug fixes Download: FastStone Image Viewer 8.5 | Portable | ~15.0 MB (Freeware) View: FastStone Image Viewer Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Yup, broke my comp… again. its times like this when I regret AMD. This just never happens on NV.
    • Huh? You're delusional calling the Steam Deck dead. It is so successful that it has sold out multiple times. Even after the price hike this year it sold out again with 24 hours of being back in stock. The demand is real and has not died down even after four years.
  • Recent Achievements

    • Rookie
      DaviKar went up a rank
      Rookie
    • Dedicated
      HidekoYamamoto94 earned a badge
      Dedicated
    • One Month Later
      timbobit earned a badge
      One Month Later
    • One Month Later
      nates earned a badge
      One Month Later
    • Week One Done
      Almohandis earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      460
    2. 2
      +Edouard
      160
    3. 3
      PsYcHoKiLLa
      110
    4. 4
      Michael Scrip
      85
    5. 5
      Steven P.
      69
  • Tell a friend

    Love Neowin? Tell a friend!