TryClose() closes the view twice if a DialogResult is specified in WPF

Topics: UI Architecture
Sep 15, 2011 at 1:25 AM

I have not tested this in Silverlight/WP7, but I ran into an issue where the CanClose guard on my screens was always being called twice if the screen was being closed with TryClose.

 

After doing some investigating, it looks like this only happens when you specify a DialogResult. In the Screen.GetViewCloseAction method, when you set the value on the DialogResult property the view is automatically closed. Then it gets closed again when the close method is explicitly invoked.

 

I modified the Screen.GetViewCloseAction method so that it doesn't invoke the close method if the result property was set. I'm not sure if that causes any side-effects on any other platforms, but it seems to work for me.

Jul 12, 2013 at 11:52 AM
I know this is an old post but it seems to be relevant to my current issue.
I'm using WPF 4.0 and the CM 1.5.2.

When closing a dialog the window is not being closed. This only happens when i specify a DialogResult. I've been debugging the code in Screen.cs and following code seems to be the culprit;

(this code is found in screen.cs -> System.Action GetViewCloseAction(bool? dialogResult))
if(closeMethod != null)
                    return () => {
#if !SILVERLIGHT && !WinRT
                        var isClosed = false;
                        if(dialogResult != null) {
                            var resultProperty = contextualView.GetType().GetProperty("DialogResult");
                            if (resultProperty != null) {
                                resultProperty.SetValue(contextualView, dialogResult, null);
                                isClosed = true;
                            }
                        }

                        if (!isClosed) {
                            closeMethod.Invoke(contextualView, null);
                        }
#else
                        closeMethod.Invoke(contextualView, null);
#endif
                    };
Here's my question; why is isClosed being set to true? Doesn't this just prevent the closeMethod from being executed? Please enlighten me.

Cheers