Guard Activate/Deactivate in SimpleMDI Sample

Jun 16, 2011 at 3:27 PM

Hello! I have a question concerning the SimpleMDI sample. I see that it is possible to control (to prevent or to allow) tab closing from TabViewModel class code. I just need to override TabViewModel.CanClose method. It work perfectly well.. But it seems to me there is no way to control activation and deactivation of the tab. Yes, it is possible to receive notifications of its activation and deactivation, but there are no methods to control the process. I want to be able to allow or deny tab activation from TabViewModel.

I mean there is no out-of-box method to do this. It seems to me, I can think out some homemade way to solve the problem, like define and implement over the Screen, particular TabViewModel, a pair of interfaces like IGuardActivate and IGuardDeactivate, then override a ShellViewModel.ChangeActiveItem method to check, is activation/deactivation allowed or not. Then I need some solution to issue of ShellViewModel's ActiveItem not changing its value being binded to SelectedItem property of TabControl. But I am not sure. I want to know, is there any standard Caliburn framework's way to solve the problem, and what it should look like?

PS. Indeed another question. I have tried to play with the issue, and face a strange problem. I overrode the TabViewModel.CanClose method in such a way:

public class TabViewModel : Screen
{
    public override void CanClose(System.Action<bool> callback)
    {
        callback.Invoke(false);
    }
}

And then I overrode ShellViewModel.ChangeActiveItem method:

protected override void ChangeActiveItem(IScreen newItem, bool closePrevious)
{
    var guardClose = newItem as IGuardClose;

    if (guardClose != null)
    {
        guardClose.CanClose(canClose =>
        {
            if (canClose)
            {
                base.ChangeActiveItem(newItem, closePrevious);
            }
        });
    }
}
Everything must be OK, and operable. But I was knocked by the strange BadImageFormatException on the line callback.Invoke(false). Still can't figure the problem. Here is the modified SimpleMDI project for download. May be you will be able to reproduce the exception, and/or to explain me, what is going on?

Coordinator
Jun 16, 2011 at 3:31 PM

There's no standard way of doing that in CM. You can search the forums. There is some past discussion and implementation info from other users. I've seen this request a lot lately, so I'm thinking about how I might add it in a future version.

Jun 17, 2011 at 1:49 PM

Maybe this helps

http://caliburnmicro.codeplex.com/discussions/258210