How to catch the ending resize window?

后端 未结 5 644
[愿得一人]
[愿得一人] 2020-12-08 05:42

I need catch the event endresize in WPF.

5条回答
  •  囚心锁ツ
    2020-12-08 05:55

    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 ObservableSizeChanges = Observable
        .FromEventPattern(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
    }
    

提交回复
热议问题