Stop WPF animation, storyboard begin in xaml but stopping it in codebehind?

后端 未结 3 568
北荒
北荒 2020-12-01 16:59

I created an animation storyboard in xaml file. That story board begins on Button.Click. But to stop the animation I am trying to stop storyboard on my custom event in code

相关标签:
3条回答
  • 2020-12-01 17:38

    Define the storyboard as a static resource,

    <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>
    

    and reference it from your backend code as follows :

    StoryBoard board = (StoryBoard)this.FindResource("MovingServer");
    board.stop();
    

    start the animation from the 'click' event of the button (i don't know if you defined in xaml, but here's how it would be done if you did)

    <Button x:Name="ScanButton" onClick="Scanbutton_Click"></button>
    
    
    protected void Scanbutton_Click(object Sender, EventArgs e)
    {
        StoryBoard board = (StoryBoard)this.FindResource("MovingServer");
        board.start();
    }
    
    0 讨论(0)
  • 2020-12-01 17:46

    I am thankful for Timothy for giving nice Idea. Here I am posting my working code

       /*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; 
       } 
    
    0 讨论(0)
  • 2020-12-01 17:54

    I solve the problem with using the Stop() method of Storyboard class like this

    myStoryBoard.Stop(this.LayoutRoot);
    

    with this solution you don't have to declare Storyboard at the resource.

    0 讨论(0)
提交回复
热议问题