5

我是 WPF 的新手。我来自 C# 和 ASP.NET 背景。

我正在尝试创建一个非常基本的 WPF 应用程序,其中包含 2 个故事板,这些故事板通过交互式开始重载 .Begin(this,true) 以编程方式启动。

当引发 OnCompleted 事件时,将检查另一个故事板的状态。如果状态是情节提要没有运行,它应该开始情节提要。

我在 Completed 处理程序中收到以下错误:

抛出:无法执行操作,因为指定的 Storyboard 未应用于此对象以进行交互式控制。

我相信我使用了正确的 .Begin(this,true) 重载进行交互式控制。

我在下面包含了 MainWindow.cs 和 MainWindow.xaml 代码。我故意不通过 Xaml 中的触发器启动动画。我需要动态启动动画并检查其他多个动画的状态。示例被精简以关注主要问题。

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.Animation;

namespace StoryboardExample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public Storyboard Storyboard1 { get; set; }
    public Storyboard Storyboard2 { get; set; }

    public MainWindow()
    {
        InitializeComponent();

        Storyboard1 = (System.Windows.Media.Animation.Storyboard)FindResource("Storyboard1");
        Storyboard1.Name = "MegatronStoryboard";
        Storyboard1.Completed +=new EventHandler(Storyboard1_Completed);

        Storyboard2 = (System.Windows.Media.Animation.Storyboard)FindResource("Storyboard2");
        Storyboard2.Name = "TransformerStoryboard";
        Storyboard2.Completed += new EventHandler(Storyboard2_Completed);

        Storyboard2.Begin(this, true);
    }

    void Storyboard1_Completed(object sender, EventArgs e)
    {
        if (Storyboard2.GetCurrentState() == ClockState.Stopped)
        {
            Storyboard2.Begin(this, true);
            //Throws: Cannot perform action because the specified Storyboard was not applied to this object for interactive control.

            //I thought I was calling the Begin overload with the correct params for interactive control
            //I thought I was calling the Begin overload with the correct params for interactive control .Begin(this,true)
        }
    }

    void Storyboard2_Completed(object sender, EventArgs e)
    {
        if (Storyboard1.GetCurrentState() == ClockState.Stopped)
        {
            Storyboard1.Begin(this, true);
            //Throws: Cannot perform action because the specified Storyboard was not applied to this object for interactive control.

            //I thought I was calling the Begin overload with the correct params for interactive control .Begin(this,true)
        }
    }


}
}

<Window x:Name="MainWindow1" x:Class="StoryboardExample.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">
<Window.Resources>
    <Storyboard x:Key="Storyboard1">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="cnvsStoryboard1">
            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:3" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="cnvsStoryboard1">
            <EasingDoubleKeyFrame KeyTime="0" Value="0.2"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0.2"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:3" Value="0.2"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="cnvsStoryboard1">
            <EasingDoubleKeyFrame KeyTime="0" Value="0.2"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0.2"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:3" Value="0.2"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="cnvsStoryboard1">
            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="26"/>
            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="26"/>
            <EasingDoubleKeyFrame KeyTime="0:0:3" Value="18"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="cnvsStoryboard1">
            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="353"/>
            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="353"/>
            <EasingDoubleKeyFrame KeyTime="0:0:3" Value="5"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="Storyboard2">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="cnvsStoryboard2">
            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:3" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:4" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="cnvsStoryboard2">
            <EasingDoubleKeyFrame KeyTime="0" Value="0.2"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0.2"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:3" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:4" Value="0.2"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="cnvsStoryboard2">
            <EasingDoubleKeyFrame KeyTime="0" Value="0.2"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0.2"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:3" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:4" Value="0.2"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="cnvsStoryboard2">
            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="-230"/>
            <EasingDoubleKeyFrame KeyTime="0:0:3" Value="-230"/>
            <EasingDoubleKeyFrame KeyTime="0:0:4" Value="137"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="cnvsStoryboard2">
            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="-10"/>
            <EasingDoubleKeyFrame KeyTime="0:0:3" Value="-10"/>
            <EasingDoubleKeyFrame KeyTime="0:0:4" Value="-13"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>

<Canvas x:Name="LayoutRoot">
    <Canvas x:Name="cnvsStoryboard1" 
            Height="203" Canvas.Left="223" 
            Canvas.Top="-284" Width="253" 
            Opacity="0" 
            RenderTransformOrigin="0.5,0.5">
        <Canvas.RenderTransform>
            <TransformGroup>
                <ScaleTransform ScaleX="0.2" ScaleY="0.2"/>
                <SkewTransform/>
                <RotateTransform/>
                <TranslateTransform/>
            </TransformGroup>
        </Canvas.RenderTransform>
        <Image x:Name="imgTransformer" Height="148" 
               Canvas.Left="42" Source="Images/transformer.png" 
               Stretch="Fill" Canvas.Top="8" Width="176" 
               RenderTransformOrigin="0.5,0.5">
            <Image.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform X="1" Y="1"/>
                </TransformGroup>
            </Image.RenderTransform>
        </Image>
        <Label x:Name="lblTank" Content="Tank" Canvas.Left="101" Canvas.Top="160" FontSize="21.333">
            <Label.Foreground>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black"/>
                    <GradientStop Color="#FFCA2828" Offset="1"/>
                    <GradientStop Color="#FE412424" Offset="0.003"/>
                </LinearGradientBrush>
            </Label.Foreground>
        </Label>
    </Canvas>
    <Canvas x:Name="cnvsStoryboard2" Height="318" 
            Canvas.Left="41" Canvas.Top="229" 
            Width="215" Opacity="0" 
            RenderTransformOrigin="0.5,0.5">
        <Canvas.RenderTransform>
            <TransformGroup>
                <ScaleTransform ScaleX="0.2" ScaleY="0.2"/>
                <SkewTransform/>
                <RotateTransform/>
                <TranslateTransform/>
            </TransformGroup>
        </Canvas.RenderTransform>
        <Image x:Name="imgMegatron" Height="264" Canvas.Left="33" 
               Source="Images/Megatron.png" 
               Stretch="Fill" Canvas.Top="8" 
               Width="153"/>
        <Label x:Name="lblMegatron" 
               Content="Megatron" 
               Canvas.Left="56" 
               Canvas.Top="278" 
               FontSize="21.333" 
               Width="107.707" 
               RenderTransformOrigin="0.696,0.599" Height="40">
            <Label.Foreground>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black"/>
                    <GradientStop Color="#FFCA2828" Offset="1"/>
                    <GradientStop Color="#FE412424" Offset="0.003"/>
                </LinearGradientBrush>
            </Label.Foreground>
        </Label>
    </Canvas>
</Canvas>
</Window>

我一直在研究这个,我认为我在 storyboard.Begin(this,true) 重载中的第一个参数可能不正确?

任何帮助是极大的赞赏。

-亚伦

4

2 回答 2

7

如果您使用 .GetCurrentState(this) 是否有效?或者,也许调用 .Stop(this); 在调用 .Start(this, true) 之前立即调用?

于 2013-01-23T06:21:32.973 回答
0

另外,确保情节提要是可控的。来自 MSDN:“要使情节提要在代码中可控,您必须使用情节提要的 Begin 方法的适当重载并指定 true 以使其可控。”

更多信息:http: //msdn.microsoft.com/en-us/library/cc672521.aspx

于 2015-08-10T13:52:42.687 回答