无法理解为什么书中的动画样本不起作用

时间:2009-10-13 11:54:35

标签: silverlight

为什么此代码不起作用?

<UserControl x:Class="slv.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Rectangle Fill="#FFFF0000" Stroke="#FF000000" Width="40" Height="40" Canvas.Top="40" x:Name="rect">
    <Rectangle.Triggers>
        <EventTrigger RoutedEvent="Rectangle.Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetName="rect" Storyboard.TargetProperty="(Canvas.Left)" >
                        <DiscreteDoubleKeyFrame KeyTime="0:0:1" Value="300" />
                        <DiscreteDoubleKeyFrame KeyTime="0:0:9" Value="600" />
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Rectangle.Triggers>
</Rectangle>

这本代码来自本书银幕3,作者劳伦斯莫罗尼

1 个答案:

答案 0 :(得分:0)

好吧,如果这是一本书,这就是确切的XAML,那就有点奇怪了。我设法让这个例子工作得很好,但是你缺少的是画布。

这是我通过在矩形周围添加画布来修改的代码。即使它显示任何内容,如果你没有用画布包装它也不会有效,因为在动画中它专门引用矩形相对于画布的转换。

<UserControl x:Class="SilverlightApplication2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
  <Canvas x:Name="LayoutRoot">
        <Rectangle Fill="#FFFF0000" Stroke="#FF000000" Width="40" Height="40" Canvas.Top="40" x:Name="rect">
            <Rectangle.Triggers>
                <EventTrigger RoutedEvent="Rectangle.Loaded">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="rect" Storyboard.TargetProperty="(Canvas.Left)" >
                                <DiscreteDoubleKeyFrame KeyTime="0:0:1" Value="300" />
                                <DiscreteDoubleKeyFrame KeyTime="0:0:9" Value="600" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Rectangle.Triggers>
        </Rectangle>
    </Canvas>
</UserControl>