Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caliburn ShowDialog and MessageBox

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.

like image 701
Mare Infinitus Avatar asked Jul 07 '13 09:07

Mare Infinitus


2 Answers

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).

like image 164
Patryk Ćwiek Avatar answered Oct 05 '22 17:10

Patryk Ćwiek


In the article A Billy Hollis Hybrid Shell (written by the framework coordinator) the author showed a nice way to handle both dialog and message boxes, but he used dependency injection (you can go without DI of course but it makes things simpler). The main idea is that you can let your main window, the one used as the application shell implement an interface that looks something like this:

public interface IDialogManager
    {

        void ShowDialog(IScreen dialogModel);
        void ShowMessageBox(string message, string title = null, MessageBoxOptions options = MessageBoxOptions.Ok, Action<IMessageBox> callback = null);

    }

and then he registers this interface with the IoC container, I guess you can use your imagination from there on and if you don't have time then you can look at the source code that accompanies the article.

like image 30
Ibrahim Najjar Avatar answered Oct 01 '22 17:10

Ibrahim Najjar