Frage

I have a UserControl that I am showing with the ShowDialog method of the WindowManager. The User Control has two buttons (Ok, Cancel) as well as some input.

I would like to also run some code when the User selects the red X in the upper right of the Window. Is there a way to wire an event to that with Caliburn Micro?

Thanks

Chuck

War es hilfreich?

Lösung

I've done something similar with the ShowWindowmethod from the WindowManager, the ShowDialog method should work in the same way when you're passing a ViewModel (which I'm assuming you're doing in this case).

It's worth reading this page in the documentation for a more complete explanation, but essentially a Screen has a CanClose method, which you can override with custom behaviour.

CanClose – The default implementation always allows closing. Override this method to add custom guard logic.

So, within the ViewModel class that you're passing to ShowDialog, you could do something like:

public override void CanClose(Action<bool> callback)
{
    bool canClose = false;

    // if (yourConditionHere)
        canClose = true;

    callback(closeDialog);
}

Where canClose could just as easily be a private field updated elsewhere in the class.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top