C# 停止WPF动画,故事板在xaml中开始,但在codebehind中停止?

C# 停止WPF动画,故事板在xaml中开始,但在codebehind中停止?,c#,wpf,xaml,C#,Wpf,Xaml,我在xaml文件中创建了一个动画故事板。故事板从按钮开始。单击。但是为了停止动画,我试图在代码隐藏中停止自定义事件的情节提要。 代码没有抛出任何异常,但是当我的事件被触发时,动画仍然继续 我认为问题在于停止方法。停止需要开始动画的同一对象来停止动画。但这里的故事板是从WPF xaml开始的,我将在代码隐藏中停止它 任何解决方案,如何在代码隐藏中获取Xaml对象,或为此提供任何替代解决方案 XAML代码: <Canvas.Triggers> <EventT

我在xaml文件中创建了一个动画故事板。故事板从按钮开始。单击。但是为了停止动画,我试图在代码隐藏中停止自定义事件的情节提要。 代码没有抛出任何异常,但是当我的事件被触发时,动画仍然继续

我认为问题在于停止方法。停止需要开始动画的同一对象来停止动画。但这里的故事板是从WPF xaml开始的,我将在代码隐藏中停止它

任何解决方案,如何在代码隐藏中获取Xaml对象,或为此提供任何替代解决方案

XAML代码:

<Canvas.Triggers>
            <EventTrigger RoutedEvent="Button.Click" SourceName="ScanButton">
                <EventTrigger.Actions>
                    <BeginStoryboard >
                        <Storyboard  Name="MovingServer" Storyboard.TargetName="ImageMove" RepeatBehavior="Forever" >
                            <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:2" From="30" To="300" BeginTime="0:0:0" />
                            <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:5" From="300" To="300" BeginTime="0:0:5" />
                            <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:2" From="300" To="600" BeginTime="0:0:7" />
                            <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:2" From="1" To="0" BeginTime="0:0:7" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
    private void EventPublisher_OnScanningFinish(object sender, EventArgs args)
    {
        Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate() { this.StopScanningAnimation(); });
    }

    private void StopScanningAnimation()
    {

        ServerView.StoryBoardServerScrolling.Stop(this); //---------- Not Working

        //this.ServerView.Server1Static.Visibility = System.Windows.Visibility.Hidden;
        //this.ServerView.Server2Static.Visibility = System.Windows.Visibility.Hidden;
        //this.ServerView.Server3Scrolling.Visibility = System.Windows.Visibility.Hidden;
        //this.ServerView.SearchingGlass.Visibility = System.Windows.Visibility.Hidden;
    }

将情节提要定义为静态资源

<MyControl.Resources>
                        <Storyboard Key="MovingServer" Storyboard.TargetName="ImageMove" RepeatBehavior="Forever" >
                            <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:2" From="30" To="300" BeginTime="0:0:0" />
                            <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:5" From="300" To="300" BeginTime="0:0:5" />
                            <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:2" From="300" To="600" BeginTime="0:0:7" />
                            <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:2" From="1" To="0" BeginTime="0:0:7" />
                        </Storyboard>
</MyControl.Resources>
从按钮的“click”事件开始动画(我不知道是否在xaml中定义了,但下面是如何定义的)


受保护的空扫描按钮\u单击(对象发送者,事件参数e)
{
情节提要=(情节提要)this.FindResource(“MovingServer”);
board.start();
}

我感谢蒂莫西给了我一个好主意。在这里我张贴我的工作代码

   /*create this resources as global to that perticular xaml. Need not to be put it in App.xaml
     MyControl could be Window or Page or UserControl */

       <MyControl.Resources>
        <Storyboard x:Key="MovingServer" Storyboard.TargetName="MyImage" RepeatBehavior="Forever" >
            <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:2" From="30" To="300" BeginTime="0:0:0" />
            <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:5" From="300" To="300" BeginTime="0:0:5" />
            <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:2" From="300" To="600" BeginTime="0:0:7" />
            <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:2" From="1" To="0" BeginTime="0:0:7" />
        </Storyboard>
    </MyControl.Resources>

/* <!-- Now use those animation resources, the place where you want. You can use it as static resource and begin stop animation from code behind OR use it as trigger event --> */

/*    <!-- Static resources--> */
    <Canvas>
        <Image Canvas.Left="0" Canvas.Top="-2" Height="32" Name="MyImage" Width="32" Source="/CCTrayHelper;component/Images/ServerIcon.png" Visibility="Hidden"/>
     <Canvas.Resources>
        <BeginStoryboard x:Key="serverAnimate" Storyboard="{StaticResource MovingServer}" />
     </Canvas.Resources>
    </Canvas>
    <Button x:Name="ScanButton" onClick="Scanbutton_Click" />

/* ****************************************************************** */



  /*  Code behind to start/stop animation*/

//Get the resource value first on current object, so that when you start/stop the animation, it work only on current object
  Storyboard sbImageAnimate = (Storyboard)this.ServerView.FindResource("MovingServer");

//Start the animation on Button Click 
 protected void Scanbutton_Click(object Sender, EventArgs e)
  {   
   this.MyImage.Visibility = System.Windows.Visibility.Visible; 
   sbImageAnimate.Begin();
  } 

 //Stop animation on my own even. You can use it on any event
 private void EventPublisher_OnFinish(object sender, EventArgs args) 
 {   
      Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate() { this.StopScanningAnimation(); });  
 }

 private void StopScanningAnimation()  
   {
   sbImageAnimate.Stop();
   this.MyImage.Visibility = System.Windows.Visibility.Hidden; 
   } 
/*将此资源创建为该特定xaml的全局资源。不需要将其放在App.xaml中
MyControl可以是窗口、页面或用户控件*/
/*  */
/*     */
/* ****************************************************************** */
/*启动/停止动画的代码隐藏*/
//首先在当前对象上获取资源值,以便在启动/停止动画时,它仅在当前对象上工作
故事板sbImageAnimate=(故事板)this.ServerView.FindResource(“MovingServer”);
//单击按钮开始动画
受保护的空扫描按钮\u单击(对象发送者,事件参数e)
{   
this.MyImage.Visibility=System.Windows.Visibility.Visible;
sbImageAnimate.Begin();
} 
//停止我自己的动画甚至。你可以在任何活动中使用它
私有void EventPublisher_OnFinish(对象发送方,EventArgs args)
{   
Invoke(DispatcherPriority.Normal,(Action)delegate(){this.StopScanningAnimation();});
}
私有void StopScanningAnimation()
{
sbImageAnimate.Stop();
this.MyImage.Visibility=System.Windows.Visibility.Hidden;
} 

我使用像这样的脚本类的
Stop()方法解决了这个问题

myStoryBoard.Stop(this.LayoutRoot);

使用此解决方案,您不必在资源中声明情节提要。

您测试过吗<代码>静态资源
?你是说仅仅是
资源
?那么
x:Key
呢?我测试了一个有效的示例代码,但是我当前的真实代码太长了,所以我正在搜索alternatives@H.B.很抱歉,现在无法测试此代码。您使用静态资源是对的,它应该是“资源”,我现在就更改它。x:键;“x:”部分是可选的,只是指您需要添加一个键而不是名称。另外:我也有同样的想法并尝试过,但似乎也没有停止动画。@我尝试过使用windows资源。。。这不起作用。。。K Update itIn若要执行此操作,启动动画时必须将第二个参数设置为true:Begin(DependencyObject…,allowsModification)。
   /*create this resources as global to that perticular xaml. Need not to be put it in App.xaml
     MyControl could be Window or Page or UserControl */

       <MyControl.Resources>
        <Storyboard x:Key="MovingServer" Storyboard.TargetName="MyImage" RepeatBehavior="Forever" >
            <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:2" From="30" To="300" BeginTime="0:0:0" />
            <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:5" From="300" To="300" BeginTime="0:0:5" />
            <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:2" From="300" To="600" BeginTime="0:0:7" />
            <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:2" From="1" To="0" BeginTime="0:0:7" />
        </Storyboard>
    </MyControl.Resources>

/* <!-- Now use those animation resources, the place where you want. You can use it as static resource and begin stop animation from code behind OR use it as trigger event --> */

/*    <!-- Static resources--> */
    <Canvas>
        <Image Canvas.Left="0" Canvas.Top="-2" Height="32" Name="MyImage" Width="32" Source="/CCTrayHelper;component/Images/ServerIcon.png" Visibility="Hidden"/>
     <Canvas.Resources>
        <BeginStoryboard x:Key="serverAnimate" Storyboard="{StaticResource MovingServer}" />
     </Canvas.Resources>
    </Canvas>
    <Button x:Name="ScanButton" onClick="Scanbutton_Click" />

/* ****************************************************************** */



  /*  Code behind to start/stop animation*/

//Get the resource value first on current object, so that when you start/stop the animation, it work only on current object
  Storyboard sbImageAnimate = (Storyboard)this.ServerView.FindResource("MovingServer");

//Start the animation on Button Click 
 protected void Scanbutton_Click(object Sender, EventArgs e)
  {   
   this.MyImage.Visibility = System.Windows.Visibility.Visible; 
   sbImageAnimate.Begin();
  } 

 //Stop animation on my own even. You can use it on any event
 private void EventPublisher_OnFinish(object sender, EventArgs args) 
 {   
      Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate() { this.StopScanningAnimation(); });  
 }

 private void StopScanningAnimation()  
   {
   sbImageAnimate.Stop();
   this.MyImage.Visibility = System.Windows.Visibility.Hidden; 
   } 
myStoryBoard.Stop(this.LayoutRoot);