日期:2014-05-18  浏览次数:20773 次

提示"上下文不存在名称myStoryboard"
XML code

<Window x:Class="动画.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Canvas Background="#CDFCAE">
        <Canvas.Resources>
            <Storyboard x:Name="myStoryboard">
                <DoubleAnimation 
                    Storyboard.TargetName="myTransform" 
                    Storyboard.TargetProperty="Angle" 
                        From="0" To="180" Duration="0:0:5" 
                    RepeatBehavior="Forever" />
            </Storyboard>
            </Canvas.Resources>
        
        <Image x:Name="imgTarget" Source="a1.png" Canvas.Left="180" Canvas.Top="80" 
               MouseEnter="imgTarget_MouseEnter" MouseLeave="imgTarget_MouseLeave">
            <Image.RenderTransform>
                <RotateTransform x:Name="myTransform" Angle="15" CenterX="120" CenterY="68"/>
            </Image.RenderTransform>
        </Image>
        </Canvas>
</Window>











C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 动画
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }

        private void imgTarget_MouseEnter(object sender, MouseEventArgs e)
        {
            myStoryboard.Begin();
        }
        private void imgTarget_MouseLeave(object sender, MouseEventArgs e)
        {
            myStoryboard.Stop();
        }
    }
}










------解决方案--------------------
A Storyboard can be used to animate dependency properties of animatable classes (for more information about what makes a class animatable, see the Animation Overview). However, because storyboarding is a framework-level feature, the object must belong to the NameScope of a FrameworkElement or a FrameworkContentElement.