停止WPF动画,故事情节开始在XAML但在codebehind停止呢? [英] Stop WPF animation, storyboard begin in xaml but stopping it in codebehind?
问题描述
我在创建XAML文件的动画故事板。这个故事板开始于Button.Click。但要停止我试图在code我的自定义事件停止故事板后面的动画。
在code未抛出任何异常,但是当我的事件被解雇了动画却还在继续下去。
我认为这个问题是停止方法。停止要求开始动画停止它同一个对象。但这里的故事板开始WPF XAML,我在code背后停止它。
任何解决方案,如何让XAML中的对象身后code或任何对此的替代解决方案?
XAML code:
< Canvas.Triggers>
<的EventTrigger RoutedEvent =Button.ClickSOURCENAME =ScanButton>
< EventTrigger.Actions>
< BeginStoryboard>
<情节提要NAME =MovingServerStoryboard.TargetName =ImageMove的repeatBehavior =永远>
&所述; DoubleAnimation是Storyboard.TargetProperty =(Canvas.Left)持续时间=0:0:2从=30为=300的BeginTime =0:0:0/>
&所述; DoubleAnimation是Storyboard.TargetProperty =(Canvas.Left)持续时间=0:0:5从=300为了=300的BeginTime =0:0:5/>
&所述; DoubleAnimation是Storyboard.TargetProperty =(Canvas.Left)持续时间=0:0:2从=300为了=600的BeginTime =0:0:7/>
&所述; DoubleAnimation是Storyboard.TargetProperty =透明度持续时间=0:0:2从=1要=0的BeginTime =0:0:7/>
< /故事板>
< / BeginStoryboard>
< /EventTrigger.Actions>
< /&的EventTrigger GT;
code背后:
私人无效EventPublisher_OnScanningFinish(对象发件人,EventArgs参数)
{
Dispatcher.Invoke(DispatcherPriority.Normal,(动作)委托(){this.StopScanningAnimation();});
} 私人无效StopScanningAnimation()
{ ServerView.StoryBoardServerScrolling.Stop(本); // ----------不工作 //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;
}
我感谢蒂莫西给了不错的主意。在这里,我张贴我的工作code
/ *创建这个资源为全球该perticular XAML。不需要是把它的App.xaml
MyControl可能是窗口或页面或用户控件* / < MyControl.Resources>
<情节提要X:键=MovingServerStoryboard.TargetName =MYIMAGE的repeatBehavior =永远>
&所述; DoubleAnimation是Storyboard.TargetProperty =(Canvas.Left)持续时间=0:0:2从=30为=300的BeginTime =0:0:0/>
&所述; DoubleAnimation是Storyboard.TargetProperty =(Canvas.Left)持续时间=0:0:5从=300为了=300的BeginTime =0:0:5/>
&所述; DoubleAnimation是Storyboard.TargetProperty =(Canvas.Left)持续时间=0:0:2从=300为了=600的BeginTime =0:0:7/>
&所述; DoubleAnimation是Storyboard.TargetProperty =透明度持续时间=0:0:2从=1要=0的BeginTime =0:0:7/>
< /故事板>
< /MyControl.Resources>/ *<! - 现在使用这些动画资源,在那里你想要的地方。您可以使用它作为静态的资源,并从code开始停止动画背后或将其作为触发事件 - > * // *<! - 静态资源 - > * /
<&帆布GT;
<图像Canvas.Left =0Canvas.Top = - 2HEIGHT =32NAME =MYIMAGEWIDTH =32源=/ CCTrayHelper;组件/图片/ ServerIcon.png能见度=隐藏/>
< Canvas.Resources>
< BeginStoryboard X:键=serverAnimate故事板={StaticResource的MovingServer}/>
< /Canvas.Resources>
< /帆布>
<按钮X:NAME =ScanButton的onClick =Scanbutton_Click/>/ * ************ ****************** * / / * code后面的启动/停止动画* ///首先获取当前对象的资源价值,所以,当你开始/停止动画,这对当前对象只工作
故事板sbImageAnimate =(故事板)this.ServerView.FindResource(MovingServer);//开始上按钮单击动画
保护无效Scanbutton_Click(对象发件人,EventArgs的发送)
{
this.MyImage.Visibility = System.Windows.Visibility.Visible;
sbImageAnimate.Begin();
} //我自己甚至停止动画。你可以用它在任何情况下
私人无效EventPublisher_OnFinish(对象发件人,EventArgs参数)
{
Dispatcher.Invoke(DispatcherPriority.Normal,(动作)委托(){this.StopScanningAnimation();});
} 私人无效StopScanningAnimation()
{
sbImageAnimate.Stop();
this.MyImage.Visibility = System.Windows.Visibility.Hidden;
}
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 behind. The code is not throwing any exception but When my event got fired the animation still goes continue.
I think the issue is with the Stop method. Stop required the same object that begins the animation to stop it. But here the storyboard is begin in WPF xaml and I am stopping it in code behind.
Any Solution, how to get Xaml object in code behind or Any alternative solution for this??
XAML CODE:
<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>
Code Behind:
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;
}
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;
}
这篇关于停止WPF动画,故事情节开始在XAML但在codebehind停止呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!