punker76 / MahApps.Metro.SimpleChildWindow

A simple child window for MahApps.Metro
MIT License
374 stars 55 forks source link

Passing ChildWindowResult back on close button pressed #91

Closed JohnFredMeyer closed 4 years ago

JohnFredMeyer commented 5 years ago

I may be wrong and please correct me if so. I am trying to pass a value back even if the user presses the close button. Here is code where that is occurring

image

It calls just this.Close(); I was thinking is there anyway we could make that public so it can be overridden? Or possibly having it Close with the ChildWindowResult as the parameter? If its never set it just passes null back? Or possible checks to see if ChildWindowResult is null and if so calls just Close() otherwise it calls Close(ChildWindowResult). I don't mind making changes if you approve or if there is a better way please let me know. Thanks again.

JohnFredMeyer commented 5 years ago

Ok figured out a way to do this. I am using ReactiveUI and Reactive extension but same can be done by intercepting on the closing event and using a bool within the view to determine if it is the first click of the closing button. If not I just return so it can close otherwise I cancel the close, set my bool, then close again. Still feels a little awkward. Let me know your thoughts?

public bool ClosingButtonClickProcessed { get; set; }

                Observable.FromEventPattern<CancelEventArgs>(this, nameof(this.Closing))
                    .Do(_ =>
                    {
                        if (ClosingButtonClickProcessed) return;

                        _.EventArgs.Cancel = true;
                        ClosingButtonClickProcessed = true;
                        this.Close(MyValueIWantToPassBack);
                    })
                    .Subscribe()
                    .DisposeWith(disposables);