Search code examples
caliburn.microshowdialog

Caliburn Micro: Screen.TryClose does not work


I am running out of hair in frustration.

I have made a somewhat minimal example where the same viewmodel/view acts as main window and dialog. Hope this does not cause confusion.

class DialogViewModel : Screen
{
    private readonly IWindowManager _windowManager;

    public DialogViewModel(IWindowManager windowManager)
    {
        _windowManager = windowManager;
    }

    public void ShowDialog()
    {
        _windowManager.ShowDialog(new DialogViewModel(_windowManager));
    }

    //----------

    public DialogViewModel()
    {

    }

    public void Close()
    {
        TryClose();
        //TryClose(true);
        //TryClose(false);
    }
}

<Window x:Class="Views.DialogView"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
                xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro.Platform"
                mc:Ignorable="d" 
                d:DesignHeight="100" d:DesignWidth="200">
    <WrapPanel>
        <Button>
            Close
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <cal:ActionMessage MethodName="Close" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>
        <Button>
            Show Dialog
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <cal:ActionMessage MethodName="ShowDialog" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>
    </WrapPanel>
</Window>

public sealed class AppBootstrapper : BootstrapperBase
{
    private SimpleContainer _container;

    public AppBootstrapper() : base(true)
    {
        StartRuntime();
    }

    protected override void OnStartup(object sender, StartupEventArgs e)
    {
        DisplayRootViewFor<DialogViewModel>();
    }

    protected override void Configure()
    {
        _container = new SimpleContainer();
        _container.Singleton<IWindowManager, WindowManager>();
        _container.Singleton<IEventAggregator, EventAggregator>();

        _container.PerRequest<DialogViewModel>();
    }

    protected override object GetInstance(Type service, string key)
    {
        var instance = _container.GetInstance(service, key);
        if (instance != null)
            return instance;
        throw new InvalidOperationException("Could not locate any instances.");
    }

    protected override IEnumerable<object> GetAllInstances(Type service)
    {
        return _container.GetAllInstances(service);
    }

    protected override void BuildUp(object instance)
    {
        _container.BuildUp(instance);
    }
}

The call TryClose() does not initiate OnDeactivate. According to https://caliburnmicro.codeplex.com/wikipage?title=Screens,%20Conductors%20and%20Composition and the several posts here on stackoverflow the above should be enough. But nothing happens when I call TryClose().

Please note that code does run and show the dialogs on my machine. If something is missing, please let me know.

Any ideas why?

Thank you!


Solution

  • StartRuntime should be Initialize(), codeplex code is really really outdated with release of v2. Reference github.com/bluespire/Caliburn.Micro or CaliburnMicro.com. I assume this is a WPF App. Also your Button's don't appear to be correct.

    <Button x:Name="ShowDialog" Content="ShowDialog" />
    <Button x:Name="Close" Content="Close" />
    

    I am assuming this is a WPF application.