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;
}
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();
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With