【问题标题】:Triggering a Silverlight Storyboard Behavior on Demand按需触发 Silverlight 故事板行为
【发布时间】:2010-10-02 06:12:55
【问题描述】:

我正在搞乱 Silverlight,试图找出如何让它运行,关于情节提要和获得按需启动的行为,我似乎没有意识到一件事。下面描述了一个蓝色椭圆,它在 Y 轴上有一个变换 Y 轴投影,因此当被触发时,它看起来像一个在其垂直轴上以 3D 形式旋转的圆圈。单击椭圆时会触发该行为。如果 RepeatBehavior 设置为 3x,则重复 3 次并停止。

我想弄清楚的是如何在每次单击椭圆时重新触发此行为,因为在该行为第一次运行后它不会再次触发。我试图通过创建一个 MouseLeftButtonDown 事件并用

填充它来让它再次触发
Storyboard1.Begin();

但这无济于事。事实上,如果我在那里设置断点,它会执行,但没有效果。这是 Xaml:

<UserControl 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" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" mc:Ignorable="d"
    x:Class="UsingStoryboards.MainPage"
    Width="640" Height="480">
    <UserControl.Resources>
        <Storyboard x:Name="Storyboard1" RepeatBehavior="3x">
            <DoubleAnimation Duration="0:0:2" To="160" Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)" Storyboard.TargetName="ellipse" d:IsOptimized="True"/>
        </Storyboard>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Loaded">
                <ei:ChangePropertyAction PropertyName="Background">
                    <ei:ChangePropertyAction.Value>
                        <SolidColorBrush Color="#FFE50D0D"/>
                    </ei:ChangePropertyAction.Value>
                </ei:ChangePropertyAction>
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <Ellipse x:Name="ellipse" Fill="#FF000BF5" HorizontalAlignment="Left" Height="80" Margin="54,75,0,0" Stroke="Black" VerticalAlignment="Top" Width="80" MouseLeftButtonDown="ellipse_MouseLeftButtonDown">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseLeftButtonDown">
                    <ei:ControlStoryboardAction Storyboard="{StaticResource Storyboard1}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <Ellipse.Projection>
                <PlaneProjection/>
            </Ellipse.Projection>
        </Ellipse>
    </Grid>
</UserControl>

【问题讨论】:

    标签: silverlight


    【解决方案1】:

    尝试在动画上指定一个“From”值。您可以为相同的值设置动画属性吗?

    【讨论】:

    • 哇,是的,做到了。我在 DoubleAnimation 属性中添加了 From="0" 属性,果然,现在每次单击它时它都会动画!非常感谢!
    【解决方案2】:

    KeithMahoney 是正确的。您正在从当前值动画到 160 度

    故事板在结束时不会停止。它们保留最后的值,除非您明确阻止它们

    这意味着第二个循环是从 160 到 160 的动画,所以它什么也不做。

    按照建议使用 From 将起始值设置为 0 或将鼠标事件处理程序更改为:

    private void ellipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        Storyboard1.Stop();
        Storyboard1.Begin();
    }
    

    【讨论】:

    • 流畅。解释也很好。
    猜你喜欢
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    • 2017-02-09
    相关资源
    最近更新 更多