none
Setting up color spline key frame animation in C#

    General discussion

  • Hey, I'm trying to set up a color spline key frame animation directly in code, so I'm doing something like this:

    mouseEnterStoryboard = new Storyboard();
    ColorAnimationUsingKeyFrames colorAnimation = new ColorAnimationUsingKeyFrames();
    colorAnimation.SetValue<
    string>(Storyboard.TargetNameProperty, "rectangle");
    colorAnimation.SetValue<
    string>(Storyboard.TargetPropertyProperty, "(Shape.Fill).(SolidColorBrush.Color)");

    SplineColorKeyFrame startFrame = new SplineColorKeyFrame();
    startFrame.KeyTime =
    KeyTime.FromTimeSpan(TimeSpan.FromSeconds(.5));
    startFrame.SetValue<
    string>(SplineColorKeyFrame.ValueProperty, "#FF0000FF"); // Issue is here
    colorAnimation.KeyFrames.Add(startFrame); // When I try to add, startFrame.Value throws an ArgumentException (when viewed in debugger).  Crashes silently here

    How can I correctly set the Value property?  I've tried everything from using the dependency properties to directly setting Value, all seem to pass that line fine, but the Value property isn't what I want it to be.  Any ideas?

    Thanks!

    Matt

    Friday, May 18, 2007 3:18 PM

All replies

  • Hello.

    There's a nice question. I've noticed some weird behaviors when comparing what you'd be writing in c# with what you normally write in xaml (for instance, if you have a storyboard defined on xaml with several timeline objects placed inside, you ccan't access them through the Children property). What i have seen is that people are using xaml strings that get inserted on the page on the fly and normally this solves the problem. Here's a quick example of what i mean:

    <Canvas

    xmlns="http://schemas.microsoft.com/client/2007"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    x:Name="parentCanvas"

    Loaded="Page_Loaded"

    x:Class="SilverlightTests.Page;assembly=ClientBin/SilverlightTests.dll"

    Width="640"

    Height="480"

    Background="White"

    >

     

    <
    Rectangle x:Name="rectangle" Width="100" Height="200" Fill="#FFC13131" />

    </Canvas>

    and for the code:

    namespace SilverlightTests

    {

    public partial class Page : Canvas

    {

    public void Page_Loaded(object o, EventArgs e)

    {

    try

    {

    // Required to initialize variables

    InitializeComponent();

    string xaml = "<Storyboard><ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"rectangle\" Storyboard.TargetProperty=\"(Shape.Fill).(SolidColorBrush.Color)\"><SplineColorKeyFrame KeyTime=\"00:00:00\" Value=\"#FF2C0606\"/><SplineColorKeyFrame KeyTime=\"00:00:01\" Value=\"#FFDD4848\"/></ColorAnimationUsingKeyFrames></Storyboard>";

    Storyboard anim = (Storyboard)XamlReader.Load(xaml);

    this.Resources.Add(anim);

    anim.Begin();

    }

    catch (Exception ex)

    {

    Debug.WriteLine(ex.ToString());

    }

    }

    }

    }

     

    Saturday, May 19, 2007 7:57 AM