I'm making a small demo application for MVVM with caliburn.
Now I want to show a MessageBox
, but the MVVM way.
For dialogs I created an event, that is handled in the ShellView
(the root view)
and just calls WindowManager.ShowDialog
with a Dialogs ViewModel
type.
Seems to stick to MVVM for me.
But what is the way to show a messagebox and get its result (Okay or cancel)?
I already saw this question, but it contains no answer either.
Mr Eisenberg hisself answers with
"Caliburn has services built-in for calling custom message boxes."
Can anyone tell what he means with that? I don't see it in the samples.
As you mentioned, you just prepare the view model (e.g. ConfirmationBoxViewModel
) and an appropriate view. You'll have to create two actions (after inheriting the view model from Screen
, which is necessary to use TryClose
. You can always implement IScreen
instead, but that would be more work):
public void OK()
{
TryClose(true);
}
public void Cancel()
{
TryClose(false);
}
and then in your other view model:
var box = new ConfirmationBoxViewModel()
var result = WindowManager.ShowDialog(box);
if(result == true)
{
// OK was clicked
}
Notice that after the dialog closes, you can access the view model properties if you need to pull additional data from the dialog (e.g. Selected item, display name etc).