Print Page | Close Window

Does DialogManager have access to the button that is clicked?

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=3390
Printed Date: 25-Oct-2020 at 5:35am


Topic: Does DialogManager have access to the button that is clicked?
Posted By: JohnBloom
Subject: Does DialogManager have access to the button that is clicked?
Date Posted: 12-Apr-2012 at 11:29am
We have been moving over our popups to cocktail and we have a question.
 
Question:
The DialogManager has access to the IsEnabled property of the button so that it can disable the ok button. Is it possible to tie into the button clicked event too?
 
Example:
We have a popup that is an island. We want to be able to pop it up from anywhere and let it save if the user clicks ok or call rejectchanges if the user clicks cancel. It doesnt appear, from what we can see, that the DialogManager allows the view that is being shown to know about the button click. This would require that everywhere we show this box we have implement what happens with ok and what happens when cancelled. Rather we would like the view model that is being shown in the popup to manage its own button clicks.

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



Replies:
Posted By: mgood
Date Posted: 12-Apr-2012 at 12:20pm
The proper way to do this is to override CanClose in your ViewModel and perform the save, then let your VM close if the save was successful or prevent the closing if the save failed. This approach properly allows you to save asynchronously, wait for the completion and then let the dialog close if successful. Same way you have access to the buttons you have access to the DialogResult to find out what button the user clicked and go down a different path in your CanClose method. Here's a simple example which always lets the dialog close if the user clicked "Cancel", but if they click "Ok", the dialog only closes if IsComplete is true.
 
        public override void CanClose(Action<bool> callback)
        {
            if (!this.DialogHost().DialogResult.Equals(DialogResult.Cancel))
            {
                callback(IsComplete);
            }
            else
                base.CanClose(callback);
        }
 
 
Here's an example of a CanClose method from TempHire, that performs a Save or RejectChanges. This is not from a dialog, but it's gonna be the same thing for you, except you won't ask the user first if they want to save.
 
        public override void CanClose(Action<bool> callback)
        {
            if (UnitOfWork.HasChanges())
            {
                var dialogResult =
                    _dialogManager.ShowMessage("There are unsaved changes. Would you like to save your changes?",
                                               DialogResult.Yes, DialogResult.Cancel, DialogButtons.YesNoCancel);
                dialogResult.OnComplete(delegate
                                            {
                                                if (dialogResult.DialogResult == DialogResult.Yes)
                                                {
                                                    Busy.AddWatch();
                                                    UnitOfWork.CommitAsync(saveResult => callback(true),
                                                                           _errorHandler.HandleError)
                                                        .OnComplete(args => Busy.RemoveWatch());
                                                }
 
                                                if (dialogResult.DialogResult == DialogResult.No)
                                                {
                                                    UnitOfWork.Rollback();
                                                    callback(true);
                                                }
 
                                                if (dialogResult.DialogResult == DialogResult.Cancel)
                                                    callback(false);
                                            });
            }
            else
                base.CanClose(callback);
        }


Posted By: JohnBloom
Date Posted: 12-Apr-2012 at 12:37pm
Thanks Marcel! You have been very quick with answers and we appreciate it.

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


Posted By: JChris
Date Posted: 13-Apr-2012 at 8:04am
I notice that CanClose() is being called twice, even in the simplest CanClose() method, like the one mentioned above. Can you elucidate?


Posted By: JChris
Date Posted: 13-Apr-2012 at 8:18am
In a simplified test, the both CanClose()'s run successfully, however in my implementation (below), the second call errs on the callback(true) with an "Object reference not set".  Thanks,  Chris
        public override void CanClose(Action<bool> callback)
        {
            if (this.DialogHost().DialogResult.Equals(DialogResult.Cancel) == true)
            {
                base.CanClose(callback);
            }
            else
            {
                Save((y) =>
                    {
                        CurrentStuff.StuffItems.Reload(IdeaBlade.EntityModel.MergeStrategy.OverwriteChanges);
                        callback(true);
                    }
                    );
                
            }
        }


Posted By: mgood
Date Posted: 13-Apr-2012 at 11:19am
Better twice than never. :-) Keen observation. That is a bug. I checked in a fix.


Posted By: JChris
Date Posted: 13-Apr-2012 at 11:51am
Sounds great. thanks!


Posted By: JohnBloom
Date Posted: 16-Apr-2012 at 7:05am

Marcel, any idea when version 0.6 will release with the bug fix?



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


Posted By: mgood
Date Posted: 16-Apr-2012 at 9:20am
I haven't set a release date yet. I'm working on something that may or may not require additional enhancements to Cocktail. In the meantime, you can build the assemblies yourself or I can build them and send to you. If you use NuGet, I can generate pre-release NuGet packages that you could deploy to your own local repository.


Posted By: JohnBloom
Date Posted: 16-Apr-2012 at 10:08am
We use NuGet so that would be the easiest for us.

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


Posted By: mgood
Date Posted: 17-Apr-2012 at 6:05pm
John,
You can download the pre-release NuGet packages for Cocktail and CocktailContrib from the following link.

https://skydrive.live.com/redir.aspx?cid=b37183c914f0fbe8&resid=B37183C914F0FBE8!210&parid=B37183C914F0FBE8!208&authkey=!AFFzCj8dMKSPTNg


Posted By: JohnBloom
Date Posted: 18-Apr-2012 at 8:21am
Thanks Marcel. I installed it this morning and we are moving forward.

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


Posted By: JohnBloom
Date Posted: 18-Apr-2012 at 12:39pm
Because we have logic in the can close there is a little pause before the dialog window disappears. If the user clicks the ok button a second time the dialog manager throws an exception.
System.NotSupportedException: TryClose requires a parent IConductor or a view with a Close method or IsOpen property.
at Caliburn.Micro.Screen.<GetViewCloseAction>b__11()
at Caliburn.Micro.Screen.<TryClose>b__1b()
at Caliburn.Micro.Execute.<>c__DisplayClass2.<InitializeWithDispatcher>b__0(Action action)
at Caliburn.Micro.Execute.OnUIThread(Action action)
at Caliburn.Micro.Screen.TryClose()
at Cocktail.DialogHostBase.Close(DialogButton dialogButton)
In the TemphHire app the view closes too fast to repo this. I added this code to PhoneTypeSelectorViewModel to simulate a canclose that runs async:
public override void CanClose(System.Action<bool> callback)
        {
            System.Threading.Thread.Sleep(1000);
            callback(true);
        }
 
If you click on the ok button a few times while the window is sleeping you will see the error. 
I got around this by disabling all of the buttons when the CanClose is called but that is a lot of maintenance code per view model. 
Maybe the dialog manager can do it automatically?


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


Posted By: mgood
Date Posted: 18-Apr-2012 at 2:32pm
I have to think about this. Disabling the buttons is easy. Enabling them again if the dialog box doesn't close is a whole other story. The asynchronous nature of the closing makes this very difficult to handle outside of the CanClose where the developer knows when the buttons should be enabled again.


Posted By: JohnBloom
Date Posted: 19-Apr-2012 at 6:46am
Thinking about it more, it would be fine if the buttons didn't disable as long as they didnt throw the exception when they were clicked the second time.

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


Posted By: mgood
Date Posted: 19-Apr-2012 at 11:26am
Neither is a quick fix. When a button is clicked it simply calls TryClose on the outer VM, from that point on it's all Caliburn.Micro. To change the current behavior I have to look into replacing the default closing strategy or worst case implementing my own conductor from scratch. I can't make any promises at this point. Have a bunch of higher prioritiy items on my plate currently.
 
For now the workaround is to disable the buttons yourself or better yet, display a busy indicator. If there's a noticable lag to the user, you should probabaly indicate that the application is doing something.


Posted By: JohnBloom
Date Posted: 19-Apr-2012 at 11:56am
Ok that is fine. We had seen this problem before we updated to Cocktail and we were secretly hoping that you guys had solved it. We can still move forward so it is not a show stopper. The lag is only a split second but enough that people who double click on buttons would have a problem (I know some people who dont see a difference between icons and buttons so they just double click everything). :)

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


Posted By: mgood
Date Posted: 08-May-2012 at 4:01pm
John,
I've pushed a minor update (v0.6.1) to NuGet with a fix for the double-click issue. You should no longer see the exception if a user double-clicks on a button. You will also have to update CocktailContrib to 1.0.3 to keep your references straight.


Posted By: JohnBloom
Date Posted: 11-May-2012 at 6:17am
Thanks for the update. Ill get in there and see what I can do.

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



Print Page | Close Window