Alexandr Laketych

Alexandr Laketych

  • NA
  • 17
  • 1.3k

How to call Storyboard in .xaml.cs ?

Jun 16 2018 12:50 AM
There is a circle that has events to move along the X axis behind the bounded coordinates.
If the circle is not brought to the end of one of the final coordinates - the condition if -> it moves to the end.
BUT! Because it happens abruptly. I decided to add the animation:
  1. <!--ANIMATED ELLIPSE TO CORNERS-->  
  2.     <Storyboard x:Key="EllipseInite">  
  3.         <DoubleAnimation To="15" Duration="00:00:4">  
  4.             <DoubleAnimation.EasingFunction>  
  5.                 <ElasticEase Oscillations="1"   
  6.                              Springiness="27"   
  7.                              EasingMode="EaseOut">  
  8.                 </ElasticEase>  
  9.             </DoubleAnimation.EasingFunction>  
  10.         </DoubleAnimation>  
  11.     </Storyboard>  
  12.   
  13.     <Storyboard x:Key="EllipseFinite">  
  14.         <DoubleAnimation To="420" Duration="00:00:4" >  
  15.             <DoubleAnimation.EasingFunction>  
  16.                 <ElasticEase Oscillations="1"   
  17.                              Springiness="27"   
  18.                              EasingMode="EaseOut">  
  19.                 </ElasticEase>  
  20.             </DoubleAnimation.EasingFunction>  
  21.         </DoubleAnimation>  
  22.     </Storyboard>  
The problem is that I can not understand why I can not or it is not called in Switcher (MainWindow) .xaml.cs?
  1. public partial class Switcher : Window  
  2.     {  
  3.         public Switcher()  
  4.         {  
  5.             InitializeComponent();  
  6.         }  
  7.   
  8.         bool Action = false;  
  9.         double   InitPxls = 15;  
  10.         double FinitePxls = 420;  
  11.         double Movement;  
  12.         Point Point;  
  13.          
  14.         private void CircleKnob_MouseDown(object sender, MouseButtonEventArgs e)  
  15.         {  
  16.             Action = true;  
  17.             Point = Mouse.GetPosition(CircleKnob);  
  18.             Mouse.Capture(CircleKnob);  
  19.         }  
  20.   
  21.         private void CircleKnob_MouseMove(object sender, MouseEventArgs e)  
  22.         {  
  23.             if (Action)  
  24.             {  
  25.                 Movement = Canvas.GetLeft(CircleKnob) + Mouse.GetPosition(CircleKnob).X - Point.X;  
  26.   
  27.                 if (Movement > InitPxls && Movement < FinitePxls) {  
  28.                     Canvas.SetLeft(CircleKnob,   Movement);  
  29.                     Canvas.SetLeft(CircleShadow, Movement - 15);  
  30.                 }  
  31.             }  
  32.         }  
  33.   
  34.         private void CircleKnob_MouseUp(object sender, MouseButtonEventArgs e)  
  35.         {  
  36.             Action = false;  
  37.             Mouse.Capture(null);  
  38.   
  39.             if (Movement < InitPxls + 210)  
  40.             {  
  41.                 //Canvas.SetLeft(CircleKnob, InitPxls);  
  42.                 //Canvas.SetLeft(CircleShadow, InitPxls - 15);  
  43.                 this.Loaded += new RoutedEventHandler(InitAnimation);  
  44.             }  
  45.             else  
  46.             {  
  47.                 //Canvas.SetLeft(CircleKnob, InitPxls);  
  48.                 //Canvas.SetLeft(CircleShadow, InitPxls - 15);  
  49.                 this.Loaded += new RoutedEventHandler(FinitAnimation);  
  50.             }  
  51.         }  
  52.   
  53.         private void InitAnimation(object sender, RoutedEventArgs e) {  
  54.             Storyboard  Init = this.FindResource("EllipseInite" ) as Storyboard;  
  55.             Storyboard.SetTarget(Init, this.CircleKnob);  
  56.             Init.Begin();  
  57.         }  
  58.   
  59.         private void FinitAnimation(object sender, RoutedEventArgs e)  
  60.         {  
  61.             Storyboard Init = this.FindResource("EllipseInite") as Storyboard;  
  62.             Storyboard.SetTarget(Init, this.CircleKnob);  
  63.             Init.Begin();  
  64.         }  
  65.     }  
And here is the circle itself: 
  1. <Ellipse   x:Name="CircleKnob" Width="230" Height="230"  Style="{StaticResource EllipseEffect}"  
  2.                        Fill="{StaticResource Circle}"   
  3.                        Canvas.Left="14" Canvas.Top="15"  
  4.                          
  5.                        MouseDown="CircleKnob_MouseDown"  
  6.                        MouseMove="CircleKnob_MouseMove"  
  7.                        MouseUp  ="CircleKnob_MouseUp"   >  
  8. </Ellipse>  
 

Answers (1)