Print Page | Close Window

DialogManager closing problems

Printed From: IdeaBlade
Category: Cocktail
Forum Name: Community Forum
Forum Discription: A professional application framework using Caliburn.Micro and DevForce
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3398
Printed Date: 12-Mar-2025 at 5:35am


Topic: DialogManager closing problems
Posted By: JohnBloom
Subject: DialogManager closing problems
Date Posted: 19-Apr-2012 at 8:34am
We have a dialog that needs to close when the user clicks something other than the "Ok" or "Cancel" buttons. Calling TryClose() only closes the vm and not the childwindow.  Is there any way that the child window can close itself?

-------------
-John Bloom



Replies:
Posted By: JohnBloom
Date Posted: 24-Apr-2012 at 12:57pm
Bump :) We have a dialog that needs to be able to close the parent VM. It gives the user a list of options and as soon as they click an option it needs to close. Speed is the key for this dialog so we would like to avoid the user having to hit enter. Everything about the dialog works with the new stuff except we lost the ability to call TryClose() on the Manager's VM.

-------------
-John Bloom


Posted By: mgood
Date Posted: 24-Apr-2012 at 2:04pm
Missed this post somehow. If your VM implements IChild, which it automatically does if you extend from Screen or Conductor, you have a Parent property. The parent in this case is the ChildWindow's VM. Just cast it to Screen and call TryClose on it.


Posted By: JohnBloom
Date Posted: 24-Apr-2012 at 2:09pm
Thanks. Looking back, that should have been obvious. Your help is much appreciated. 

-------------
-John Bloom


Posted By: JohnBloom
Date Posted: 26-Apr-2012 at 9:58am
Here is what I tried from the child vm:

((Screen)(this.Parent)).TryClose();

The problem that I had is that the _dialogButton in the DialogHostBase is null so when it enters the canclose function it callsback with false: 

 public override void CanClose(Action<bool> callback)
        {
            try
            {
                if (_dialogButton != null)
                {
                    base.CanClose(callback);
                    return;
                }

                if (_cancelButton != null)
                {
                    DialogButton button = _dialogButtons.FirstOrDefault(b => b.Value.Equals(_cancelButton));
                    if (button == null || !button.Enabled)
                        return;
                    DialogResult = button.Value;
                    base.CanClose(callback);
                    return;
                }

                callback(false);
            }
            finally 
            {
                _dialogButton = null;
            }
        }

What I ended up doing was casting the parent as a DialogHostBase and then calling the Close function since the close function sets up the _dialogButton. Here is my new code in the child vm:
var parent = ((DialogHostBase)(this.Parent));
 
parent.Close(parent.DialogButtons.First());
Since I know that I only have one "OK" button I am just calling first(). It works great unless you can think of a way to make it easier.


-------------
-John Bloom


Posted By: mgood
Date Posted: 26-Apr-2012 at 10:28am
I'm adding a TryClose method to the IDialogHost interface, so you'll be able to call this.DialogHost().TryClose(DialogResult.Ok).
 
I'm planning to officially release Cocktail v0.6 on May 2 together with DevForce 6.1.7. I believe we are still on track for the DF 6.1.7 release.


Posted By: JChris
Date Posted: 12-Jul-2012 at 1:08pm
We have a situation where we:
yield return DialogManager.ShowDialogAsync(vm, new List<DialogResult>() { DialogResult.Cancel });
//Check vn properties and do more stuff...
When the DialogResult.OkCancel buttons are shown, and the Ok button clicked, all works as expected, however when we do not show the DialogResult.OkCancel button, but only the Cancel (as above), and then use our own button to handle Ok... as in:
this.DialogHost().TryClose(DialogResult.Ok);
the next iteration block never fires. It seems like TryClose of the DialogHost() does not honor the DialogResult passed to it.
Thanks for your help


Posted By: mgood
Date Posted: 12-Jul-2012 at 4:36pm
That is a bug. I've checked in a fix. Unfortunately, it just missed the 0.7 release I pushed out this morning. I can push out a 0.7.1 NuGet package with the fix if desired.


Posted By: JChris
Date Posted: 13-Jul-2012 at 7:06am
Thanks.   If you could, that would be really helpful.  We don't have a very good workaround.


Posted By: mgood
Date Posted: 13-Jul-2012 at 12:51pm
It's done. I've pushed Cocktail 0.7.1 and the corresponding CocktailContrib 1.0.9 to NuGet.



Print Page | Close Window