Caliburn ShowDialog and MessageBox

后端 未结 3 1006
陌清茗
陌清茗 2021-02-10 11:21

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

3条回答
  •  失恋的感觉
    2021-02-10 11:45

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

提交回复
热议问题