Error when creating animation in XAML

New to XAML... i want to do some simple animations. I was trying to follow MSDN's example code from this link: Click Here

<StackPanel>
    <StackPanel.Resources>
        <Storyboard x:Name="myStoryboard">
            <DoubleAnimation
          Storyboard.TargetName="MyAnimatedRectangle"
          Storyboard.TargetProperty="Opacity"
          From="1.0" To="0.0" Duration="0:0:5" 
          AutoReverse="True" RepeatBehavior="Forever" />
        </Storyboard>
    </StackPanel.Resources>

    <Rectangle Loaded="Start_Animation" x:Name="MyAnimatedRectangle"
     Width="100" Height="100" Fill="Blue" />

</StackPanel>

I keep getting this error: "All objects added to an IDictionary must have a Key attribute associated with them. Line 11 Position 18"

What does this error mean? How do i fix it?

Answers


The StackPanel as every FrameworkElement has a property Recources which is a ResourceDictionary and you can only add elements to there with the x:Key attribute to identify them (basically this a IDictioanry<string, object>). So you need to add a x:Key to your storyboard:

<Storyboard x:Name="myStoryboard" x:Key="myStoryboardKey">

The sample on MSDN is wrong or they meant x:Key="myStoryboard" instead of x:Name="myStoryboard".


Replacing x:Name="myStoryboard" with x:Key="myStoryboard" should be enough to fix this issue.

The reason you need a Key in this case is because the code-behind is using a IDictionary, which requires an unique key for identification. http://msdn.microsoft.com/en-us/library/xfhwa508.aspx


Need Your Help

ThreadLocal has strange behavior when used in the field of an anonymous Iterator

java multithreading thread-local

I have an inner class that implements Iterable with a method similar to the following:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.