question

EmonHaque-1485 avatar image
0 Votes"
EmonHaque-1485 asked EmonHaque-1485 edited

How to animate Window before closing?

On the Loaded handler of the Window, I've this:

 Loaded += scaleUp;
 anim = new DoubleAnimation() {
     Duration = TimeSpan.FromSeconds(0.5),
     EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseIn }
 };

 void scaleUp(object sender, RoutedEventArgs e) {
     RenderTransform = new ScaleTransform(1, 1, ActualWidth/2, ActualHeight/2);
     anim.From = 0;
     RenderTransform.BeginAnimation(ScaleTransform.ScaleYProperty, anim);
 }

so it scales up when it opens:

96719-test.gif

and when I click on cancelButton:

 var cancelButton = new ActionButton() {
     Icon = Icons.CloseCircle,
     HorizontalAlignment = HorizontalAlignment.Right,
     Command = () => {
         DialogResult = false;
         scaleDown();
     }
 };

it sets the DialogResult and calls scaleDown:

 void scaleDown() {
     anim.Completed += onAnimationCompleted;
     anim.To = 0;
     //anim.From = ActualHeight;
     //BeginAnimation(HeightProperty, anim);
     RenderTransform.BeginAnimation(ScaleTransform.ScaleYProperty, anim);
 }

 void onAnimationCompleted(object sender, EventArgs e) {
     anim.Completed -= onAnimationCompleted;
     Close();
 }

BUT it doesn't animate!


EDIT
The DialogResult actually closes the window so with this in cancelButton:

 var cancelButton = new ActionButton() {
     Icon = Icons.CloseCircle,
     HorizontalAlignment = HorizontalAlignment.Right,             
     Command = () => scaleDown(false)
 };

and this in scaleDown:

 void scaleDown(bool value) {
     anim.Completed += (s,e) => DialogResult = value;
     anim.To = 0;
     anim.From = ActualHeight;
     BeginAnimation(HeightProperty, anim);
     //RenderTransform.BeginAnimation(ScaleTransform.ScaleYProperty, anim);
 }

it does animate the Height. ScaleTransform.ScaleYProperty doesn't work

windows-wpf
test.gif (372.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SimpleSamples avatar image
1 Vote"
SimpleSamples answered EmonHaque-1485 edited

No, DialogResult does not close the window. I do not know what cancelButton is but I assume the handler for it is being called such that the window will close when the handler finishes (returns) unless that is overridden. In this case, you want it to close, you only want a delay prior to closing.

Prior to calling scaleDown create an event (non-signaled) as in the AutoResetEvent Class. After calling scaleDown wait for the event using WaitOne. In onAnimationCompleted set (signal) the event. See c# - Synchronous / blocking animations in WPF? - Stack Overflow; someone else had a similar probblem and that did not work for them but I think that was because the animation in scaleDown must be done in a separate thread. But that would violate the rule that only the UI thread for a window can access the window, so I might be wrong.



· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@SimpleSamples, it does close the window even if I don't have any animation. For example, If I've these in the MainWindow:

 public partial class MainWindow : Window
 {
     public MainWindow() => InitializeComponent();
     private void Button_Click(object sender, RoutedEventArgs e) {
         var w = new Second();
         if (w.ShowDialog().Value) { }
     }
 }

and these in the Second Window:

 public partial class Second : Window
 {
     public Second() => InitializeComponent();
     private void Button_Click(object sender, RoutedEventArgs e) => DialogResult = true;
 }

when I click the button of the MainWondow, Second window pops up and as soon as I click on the button of the Second Window, it closes that window.

0 Votes 0 ·

Your sample code in the original question calls Close(). That is what closes the window. I assume that is still in your program somewhere. DialogResult is just a property.


0 Votes 0 ·

@SimpleSamples, no, I don't have Close() anywhere. See here:

96853-test.gif

0 Votes 0 ·
test.gif (554.9 KiB)
Show more comments
SimpleSamples avatar image
1 Vote"
SimpleSamples answered EmonHaque-1485 commented

No, DialogResult does not close the window. I do not know what cancelButton is but I assume the handler for it is being called such that the window will close when the handler finishes (returns) unless that is overridden. In this case, you want it to close, you only want a delay prior to closing. You need a delay that allows the UI thread to continue, something as in c# - How to put delay before doing an operation in WPF - Stack Overflow. Put the delay just prior to calling scaleDown so that the delay occurs during the animation and the delay is done in parallel.



· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I deleted this but now it is undeleted. I am not sure however that it is possible to do what I say here.

1 Vote 1 ·

@SimpleSamples, ActionButton's Command Property takes an Action and on click, it just calls Command.Invoke(). Here, in the first block of answer section, you can see the code.

0 Votes 0 ·

Is there no XAML for ActionButton? If there is XAML for it then that is important; you should post that too.

1 Vote 1 ·

@SimpleSamples, no xaml at all in the whole project up until now.

0 Votes 0 ·
Show more comments