Caliburn Micro: DialogResult

女生的网名这么多〃 提交于 2019-12-03 08:36:31

I tend to use the View Model to handle determining what happened in the dialog. For instance, you can have an IsCancelled property on your UserViewModel that you can interrogate after returning from the ShowDialog call. Something like:

public void UserNew() {
    var userViewModel = new UserViewModel(this._windowManager);
    this._windowManager.ShowDialog(userViewModel);
    if (userViewModel.IsCancelled) {
        // Handle cancellation
    } else {
        // Handle other case(s)
    }
}

In caliburn micro in your dialogviewmodel which inherits from Screen you can do:

TryClose(true); // for OK

or

TryClose(false); // for Cancel

then you could do:

var vm = IoC.Get<MyViewModel>();
var r = WindowManager.ShowDialog(vm, null, null);

if (r.HasValue && r.Value) {
  // do something on OK
}

your xaml of the dialog might look like this:

<Button Content="OK" cal:Message.Attach="[Event Click] = [AcceptButton()]" />
<Button Content="Cancel" cal:Message.Attach="[Event Click] = [CancelButton()]" />

using this namespace:

xmlns:cal="http://www.caliburnproject.org"

This is a detailed code sample of the dialog viewmodel implementation:

public bool CanAcceptButton
{
  get { return true; /* add logic here */ }
}

public void AcceptButton()
{
  TryClose(true);
}

public bool CanCancelButton
{
  get { return true; }
}

public void CancelButton()
{
  TryClose(false);
}

WPF dialogs return nullable bools instead of DialogResults. Caliburn's ShowDialog also returns bool?

From MSDN

Dialog boxes commonly allow users to accept or cancel the task for which they were shown before the dialog box is closed. ShowDialog returns a Nullable Boolean value that specifies whether the activity was accepted or canceled. The return value is the value of the DialogResult property before a window closes. For more information, see DialogResult.

DialogResult above refers to the bool property called DialogResult on System.Windows.Window.

If you want to return something more complex just define your own enum property on your window and read its value once the dialog has closed.

You can set DialogResult by using button click events in the view. This will be returned by Caliburn Micro's WindowManager.ShowDialog() method.

In the caller codebehind:

IWindowManager wm = AppContext.GetService<IWindowManager>();
var dialog = new DialogViewModel();
var result = wm.ShowDialog(dialog);
if (result.HasValue && result.Value)
{
    // Act on result
} 
// Ignore result

In the dialog codebehind:

private void Ok_OnClick(object sender, RoutedEventArgs e)
{
    DialogResult = true;
}

private void Cancel_OnClick(object sender, RoutedEventArgs e)
{
    DialogResult = false;
}

In the dialog XAML file:

<Button x:Name="Ok"
     Content="Ok"
     Click="Ok_OnClick" />
<Button x:Name="Cancel"
     Content="Cancel"
     Click="Cancel_OnClick"/>

In the ViewModel:

public void Ok()
{

}

public void Cancel()
{

}

I've verified that the Ok() and Cancel() calls are made, that the Ok_OnClick() Cancel_OnClick() events are fired, and that the return value from IWindowManager.ShowDialog() is correct.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!