Search code examples
wpfdialogwindowcaliburn.microcaliburn

How to auto-close the Caliburn Dialog window?


I have a ViewModel defined like

public class PlayerViewModel : Screen, IDiscoverableViewModel

I am showing a dialog pop up as

var result = await _dialogManager.ShowDialogAsync(item, new List<DialogResult>() { DialogResult.Cancel });

Here item is the another ViewModel which shows UI from the related View. This pop up is showing some information and needs to be auto closed after few seconds in case user doesn't select Cancel button.

Following is the Timer tick event that is getting fired after 10 seconds.

void timer_Tick(object sender, EventArgs e)
{
    this.DialogHost().TryClose(DialogResult.Cancel);
}

But it's not working and throwing exception as this.DialoHost() is getting null always. I tried this solution but it is closing the whole ViewModel instead I want to close only the dialog window.


Solution

  • Could you confirm if your 'pop-up viewmodel' is deriving from Screen ? If so, TryClose should work. Could you please verify it ? Sample code for closing.

    public class CreatePersonViewModel:Screen
    {
    
        System.Timers.Timer _timer = new Timer(5000);
        public CreatePersonViewModel()
        {
    
            _timer.Elapsed += (sender, args) =>
            {
                _timer.Enabled = false;
                TryClose(true);
            };
            _timer.Start();
        }
    }