Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch the ending resize window?

Tags:

I need catch the event endresize in WPF.

like image 880
Mediator Avatar asked Dec 17 '10 20:12

Mediator


People also ask

How do I capture a Windows resize event?

Answer: Use the addEventListener() Method You can simply use the addEventListener() method to register an event handler to listen for the browser window resize event, such as window. addEventListener('resize', ...) . The following example will display the current width and height of the browser window on resize.

How do I force a Game window to resize?

Press Alt + Spacebar again to open the window menu, arrow down to Size, and press Enter . Press the up or down arrow key if you want to resize the window vertically or the left or right arrow key if you want to resize horizontally.

What happen when window is resized?

The resize event fires when the document view (window) has been resized. This event is not cancelable and does not bubble. In some earlier browsers it was possible to register resize event handlers on any HTML element.


2 Answers

WPF doesn't provide an event that solely fires at the end of the resize process. SizeChanged is the only event associated with Window resizing - and it will fire multiple times during the resizing process.

A total hack would be to constantly set a timer ticking when the SizeChanged event fires. Then timer will not get a chance to tick until resizing ends and at that point do your one time processing.

public MyUserControl() {     _resizeTimer.Tick += _resizeTimer_Tick; }  DispatcherTimer _resizeTimer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 0, 0, 1500), IsEnabled = false };  private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e) {     _resizeTimer.IsEnabled = true;     _resizeTimer.Stop();     _resizeTimer.Start(); }  void _resizeTimer_Tick(object sender, EventArgs e) {     _resizeTimer.IsEnabled = false;          //Do end of resize processing } 
like image 199
Martin Avatar answered Sep 18 '22 08:09

Martin


Reactive Extensions for .NET provides some really cool capabilities for dealing with standard event patterns including being able to throttle events. I had a similar problem in dealing with size changed events and while the solution is still somewhat "hacky" I think that Reactive Extensions provides a much more elegant way of implementing it. Here is my implementation:

IObservable<SizeChangedEventArgs> ObservableSizeChanges = Observable     .FromEventPattern<SizeChangedEventArgs>(this, "SizeChanged")     .Select(x => x.EventArgs)     .Throttle(TimeSpan.FromMilliseconds(200));  IDisposable SizeChangedSubscription = ObservableSizeChanges     .ObserveOn(SynchronizationContext.Current)     .Subscribe(x => {         Size_Changed(x);     }); 

This will effectively throttle the SizeChanged event such that your Size_Changed method (where you can execute custom code) will not be executed until 200 milliseconds (or however long you would like to wait) have passed without another SizeChanged event being fired.

private void Size_Changed(SizeChangedEventArgs e) {     // custom code for dealing with end of size changed here } 
like image 29
Jesse Carter Avatar answered Sep 20 '22 08:09

Jesse Carter