WPF storyboards non interactive error when correct .Begin(this,true) used

核能气质少年 提交于 2020-02-24 05:40:39

问题


I am new to WPF. I come from a C# and ASP.NET background.

I am attempting to create a very basic WPF application with 2 storyboards that are started programmatically through the interactive Begin overload .Begin(this,true).

When the OnCompleted event is raised the status of the other storyboard is checked. If the status is that the storyboard is not running it should begin the storyboard.

I receive the following error inthe Completed handler:

Throws: Cannot perform action because the specified Storyboard was not applied to this object for interactive control.

I believe I used the correct .Begin(this,true) overload for interactive control.

I've included the MainWindow.cs and MainWindow.xaml code below. I am purposely not starting the animations through a Trigger in Xaml. I need to start the animations dynamically and check states of other multiple animations. Example stripped down to focus on main issue.

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>

I've been looking into this and I think that my first parameter in the storyboard.Begin(this,true) overload may be incorrect?

Any help is greatly appreciated.

-Aaron


回答1:


Does it work if you use .GetCurrentState(this)? Alternately, perhaps call .Stop(this); immediately prior to calling .Start(this, true)?




回答2:


Also, Make sure the Storyboard is controllable. From MSDN: "To make a storyboard controllable in code, you must use the appropriate overload of the storyboard's Begin method and specify true to make it controllable."

More info: http://msdn.microsoft.com/en-us/library/cc672521.aspx



来源:https://stackoverflow.com/questions/14271420/wpf-storyboards-non-interactive-error-when-correct-beginthis-true-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!