Caliburn Micro: DialogResult

I can not find a solution for the following task:

I open a dialog using WindowManager from caliburn micro:

public void UserNew() { this._windowManager.ShowDialog(new UserViewModel(this._windowManager)); } 

Now I need DialogResult when the user closes the dialog with OK. The ShowDialog WindowManager method does not return DialogResult ...

Can anybody help me?

+4
source share
4 answers

I am using the View Model to determine what happened in the dialog box. For example, you might have the IsCancelled property on your UserViewModel , which you can poll after returning from the ShowDialog . Sort of:

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

In caliburn micro in the interactive mode that inherits from Screen , you can:

 TryClose(true); // for OK 

or

 TryClose(false); // for Cancel 

then you can do:

 var vm = IoC.Get<MyViewModel>(); var r = WindowManager.ShowDialog(vm, null, null); if (r.HasValue && r.Value) { // do something on OK } 

your xaml dialog box 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 example of the implementation code for a dialog box:

 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); } 
+6
source

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

From MSDN

Dialogs typically allow users to accept or cancel a task for which they were displayed before the dialog was closed. ShowDialog returns a Nullable Boolean that indicates whether activity has been accepted or canceled. The return value is the value of the DialogResult property until the window closes. For more information, see DialogResult.

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

If you want to return something more complex, just define your own enum property in your window and read its value after closing the dialog.

0
source

You can install DialogResult by clicking the click events button in the view. This will be returned by the Caliburn Micro WindowManager.ShowDialog() method.

In caller code:

 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 codebehind dialog code:

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

In the XAML dialog file:

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

In ViewModel:

 public void Ok() { } public void Cancel() { } 

I checked that the Ok() and Cancel() calls were made, that the Ok_OnClick() Cancel_OnClick() events Ok_OnClick() fired, and that the return value from IWindowManager.ShowDialog() correct.

-1
source

Source: https://habr.com/ru/post/1412525/


All Articles