Detect if a screen closed successfully

Apr 4, 2011 at 5:49 PM

I have a view model that is a conductor of screen, and the view for the model allows you to select an active screen. In some cases, I need to prevent the user from switching to another screen (by overriding CanClose). What I have tried to do is to call TryClose when the user attempts to switch, but I haven't found a way to detect whether the close operation was successful. Is there a way to detect this? Or is there a better way to accomplish this behavior?

Apr 4, 2011 at 9:05 PM

Can't you use the IsActive property? I know that it is not the same as checking if it was *really* closed, but I think it is the only way to have a bit of control over that.

Otherwise, you can extend the ICanClose interface with a proper IsClosed property, and change the WindowManager accordingly.

By the way, what platform are you developing on?

Apr 4, 2011 at 9:16 PM

I am developing on Silverlight. I'm sure there are properties to inspect to see if something has been closed. The problem that I have is that TryClose is an asynchronous call, so I don't know when it completes.

Apr 4, 2011 at 9:42 PM

Uhmm, I don't understand what you mean saying that TryClose is asynchronous. As far as I can see, the call to TryClose is definitely synchronous on the Dispatcher.

I could not understand something regarding your scenario: you say that you override TryClose to prevent the screen from closing and that you need to avoid switching under certain conditions. I am wondering why can't you embed this logic in a custom Conductor, since it should be the simplest way to handle such situation.

What I mean is that you could expose a property on your screens stating if they are ready to be closed, and create a custom conductor to check such property before performing the switch.

Can you see some issues with this approach?

Apr 5, 2011 at 6:03 PM

It appears as though TryClose is asynchronous since it certainly doesn't block until the screen has closed, which is what I was looking for.

I hadn't thought about a custom conductor since I assumed that would be more work, but after looking at the code it doesn't look too bad. It's probably a better design in the long run anyway.