Closing a window using ViewModel first MVVM and Stylet

224 Views Asked by At

I'm using a MVVM ViewModel first approach with Stylet and I'm struggling to close a window from it's ViewModel.

In the Stylet Wiki it states that I can use:

Screen.RequestClose

I have the following code:

public class MdExportViewModel : Screen
{
    public MdExportViewModel()
        {
            if(canExport == true)
            {
                this.RequestClose();
            }
        }
}

When I try to call the 'RequestClose' I get the following error:

System.InvalidOperationException: 'Unable to close ViewModel Drain.ViewModels.Windows.MdExportViewModel as it must have a conductor as a parent (note that windows and dialogs automatically have such a parent)'

I've tried adding the Conductor<T> as follows:

public class MdExportViewModel : Conductor<IScreen>

But I get the same error. I didn't really understand how a conductor should be used in this instance. I assumed my origional attempt would work since note that windows and dialogs automatically have such a parent.

What am I doing wrong here? Other answers to similar questions use complicated workarounds, but I'd like to use a Stylet method to keep things consistent and simple.

EDIT:

The window is opened in another viewmodel as follows:

    public void ExportMD()
    {

        MdExportViewModel MdExportViewModel = new(networkMain, DesignCriteriaViewModel)
        {
            Parent = this
        };
       
        this.windowManager.ShowWindow(MdExportViewModel);
    }
0

There are 0 best solutions below