Search code examples
c#asynchronouscaliburn.micro

Caliburn how to load data when app is starting?


Net6. I'd like to show MessageBox after long operation (e.g. fetching data) when app is starting.

  • In version 3.2.0 this works fine. When app is starting, main view shows up, then MessageBox is made visible.
  • In version 4.0.173 this doesn't work: MessageBox is visible and blocks main view.

My code:

Caliburn.Micro 3.2.0
    internal class ShellViewModel : Screen
    {
        protected override async void OnInitialize()
        {
            base.OnInitialize();
            await Task.Delay(1000);
            System.Windows.MessageBox.Show("hello");
        }
    }
    
Caliburn.Micro 4.0.173
    internal class ShellViewModel : Screen
    {
        protected override async Task OnInitializeAsync(CancellationToken cancellationToken)
        {
            await base.OnInitializeAsync(cancellationToken);
            await Task.Delay(1000);
            System.Windows.MessageBox.Show("hello");
        }
    }

Solution

  • I've solved this problem by event Loaded: view:

    xmlns:cal="http://caliburnmicro.com"
    cal:Message.Attach="[Event Loaded] = [Action Window_Loaded()]"
    

    vm:

        public async Task Window_Loaded()
        {
            await Task.Delay(1000);
            System.Windows.MessageBox.Show("hello");
        }