locked
Animate Fill color of an Ellipse RRS feed

  • Question

  • Hi,

    I am trying to animate a change of color of a random Ellipse using code-behind.
    The last row sb.begin(); in my method raises an error. Unfortunately my debugging is not working (another story, but I will reinstall VS), so I cannot say the exact error.

            void tim_Tick(object sender, object e)
            {
                Random rnd = new Random();
                Canvas cv = (Canvas)rootgrid.Children[1];
                //int iobj = rnd.Next(0, cv.Children.Count-1);
                int iobj = rnd.Next(0, 5);
                Ellipse el = (Ellipse)cv.Children[iobj];
                Windows.UI.Color col = new Windows.UI.Color();
                byte r = (byte)rnd.Next(40, 255);
                byte g = (byte)rnd.Next(40, 255);
                byte b = (byte)rnd.Next(40, 255);
                col = Windows.UI.Color.FromArgb(100, r, g, b);
                SolidColorBrush br = new SolidColorBrush(col); 
                Storyboard sb = new Storyboard();
                ColorAnimation ca = new ColorAnimation();
                ca.EnableDependentAnimation = true;
                ca.To = col;
                ca.Duration = new TimeSpan(0, 0, 0, 2, 0);
                //sb.Duration = new TimeSpan(0, 0, 0, 2, 0);
                Storyboard.SetTarget(ca, br);
                Storyboard.SetTargetProperty(ca, "(Ellipse.Fill).(SolidColorBrush.Color)");
                
                sb.Children.Add(ca);
                sb.Begin();
            }

    I guess the SetTargetProperty is the major problem (but I'm not sure).
    What could be wrong?

    BR /Magnus

    Monday, November 4, 2013 12:04 AM

Answers

  • Hi Magnus,

    1. you're animating the SolidColorBrush that you're creating in that Method. The brush is a local variable and it's never assigned to any property of an UIElement, so nothing will happen in the UI. :-)

    2. Later you set the Target of the Storyboard to the SolidColorBrush, but I think you want to animate the Fill-Property of the Ellipse. The Error says on my machine that the "(Ellipse.Fill).(SolidColorBrush.Color)" is not found. Of course it is not found, cause you're animating a Brush and not an Ellipse. :-)

    If you solve these two points, it will work. I've changed your code and added comments to the changes starting with "//->Thomas:"

    Random rnd = new Random();
                Canvas cv = (Canvas)rootgrid.Children[1];
                //int iobj = rnd.Next(0, cv.Children.Count-1);
                int iobj = rnd.Next(0, 5);
                Ellipse el = (Ellipse)cv.Children[iobj];
                Windows.UI.Color col = new Windows.UI.Color();
                byte r = (byte)rnd.Next(40, 255);
                byte g = (byte)rnd.Next(40, 255);
                byte b = (byte)rnd.Next(40, 255);
                col = Windows.UI.Color.FromArgb(100, r, g, b);
    
                // -> Thomas: You were animating this Brush that is NOT part of any UIElement. :-)
                //SolidColorBrush br = new SolidColorBrush(col);
    
    
                Storyboard sb = new Storyboard();
                ColorAnimation ca = new ColorAnimation();
                ca.EnableDependentAnimation = true;
                ca.To = col;
                ca.Duration = new TimeSpan(0, 0, 0, 2, 0);
                //sb.Duration = new TimeSpan(0, 0, 0, 2, 0);
    
                // -> Thomas: SetTarget should set the Ellipse as target, so the line below is replaced with the line after
                //Storyboard.SetTarget(ca, br);
                Storyboard.SetTarget(ca, el);
    
                Storyboard.SetTargetProperty(ca, "(Ellipse.Fill).(SolidColorBrush.Color)");
    
                sb.Children.Add(ca);
                sb.Begin();


    Thomas Claudius Huber

    "If you can´t make your app run faster, make it at least look & feel extremly fast"

    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com
    author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook


    Monday, November 4, 2013 12:31 PM

All replies

  • Hi Magnus,

    1. you're animating the SolidColorBrush that you're creating in that Method. The brush is a local variable and it's never assigned to any property of an UIElement, so nothing will happen in the UI. :-)

    2. Later you set the Target of the Storyboard to the SolidColorBrush, but I think you want to animate the Fill-Property of the Ellipse. The Error says on my machine that the "(Ellipse.Fill).(SolidColorBrush.Color)" is not found. Of course it is not found, cause you're animating a Brush and not an Ellipse. :-)

    If you solve these two points, it will work. I've changed your code and added comments to the changes starting with "//->Thomas:"

    Random rnd = new Random();
                Canvas cv = (Canvas)rootgrid.Children[1];
                //int iobj = rnd.Next(0, cv.Children.Count-1);
                int iobj = rnd.Next(0, 5);
                Ellipse el = (Ellipse)cv.Children[iobj];
                Windows.UI.Color col = new Windows.UI.Color();
                byte r = (byte)rnd.Next(40, 255);
                byte g = (byte)rnd.Next(40, 255);
                byte b = (byte)rnd.Next(40, 255);
                col = Windows.UI.Color.FromArgb(100, r, g, b);
    
                // -> Thomas: You were animating this Brush that is NOT part of any UIElement. :-)
                //SolidColorBrush br = new SolidColorBrush(col);
    
    
                Storyboard sb = new Storyboard();
                ColorAnimation ca = new ColorAnimation();
                ca.EnableDependentAnimation = true;
                ca.To = col;
                ca.Duration = new TimeSpan(0, 0, 0, 2, 0);
                //sb.Duration = new TimeSpan(0, 0, 0, 2, 0);
    
                // -> Thomas: SetTarget should set the Ellipse as target, so the line below is replaced with the line after
                //Storyboard.SetTarget(ca, br);
                Storyboard.SetTarget(ca, el);
    
                Storyboard.SetTargetProperty(ca, "(Ellipse.Fill).(SolidColorBrush.Color)");
    
                sb.Children.Add(ca);
                sb.Begin();


    Thomas Claudius Huber

    "If you can´t make your app run faster, make it at least look & feel extremly fast"

    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com
    author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook


    Monday, November 4, 2013 12:31 PM
  • Hi,

    Thank you very much. Worked great!

    /Magnus

    Monday, November 4, 2013 12:44 PM