Espero que te pueda ayudar, es algo complejo pero util.
Puedes utilizar esto:
public class MainVm
{
private IPresenter<ClientVm> clientPresenter;
//Utiliza dentro de un comando
private void OpenCommandExecute(object obj)
{
var vM = new ClientVm();
this.clientPresenter = new ClientPresenter(vM);
this.clientPresenter.Closed += this.ClientPresenterClosed;
this.clientPresenter.OpenModal();
}
//
private async void ClientPresenterClosed(object sender, EventArgs e)
{
var p = sender as ClientPresenter;
if (p != null)
{
//Detectar si se guardo. para llamar algun metodo
if (p.ViewModel.IsSaved)
{
await this.Load();
}
}
this.DisposePresenter();
}
private void DisposePresenter()
{
try
{
if (this.clientPresenter != null)
{
this.clientPresenter.Closed -= this.ClientPresenterClosed;
this.clientPresenter.Dispose();
}
}
finally
{
this.clientPresenter = null;
}
}
}
Es necesario crear el presenter:
internal class ClientPresenter : Presenter<ClientWindow, ClientVm>
{
public ClientPresenter(ClientVm viewModel) : base(new ClientWindow(), viewModel)
{
}
}
ClientWindow necesita heredar de BaseWindow
<utils:BaseWindow>
...
</utils:BaseWindow>
El ClientVm necesita heredar de BaseEditableViewModel
public class ClientVm : BaseEditableViewModel{}
Pero primero necesitas implementar:
public interface IOpenable
{
void Open();
bool? OpenModal();
void Focus();
}
Implementar:
public interface IPresenter<T> : IPresenter
where T : IBaseViewModel
{
T ViewModel { get; }
void ReplaceViewModel(T viewModel);
}
Implementar:
public interface IPresenter : IClosable, IOpenable
{
}
Implementar:
public class Presenter<TW, TVM> : IPresenter<TVM>
where TW : BaseWindow where TVM : class, IBaseViewModel
{
private TVM viewModel;
public Presenter(TW window, TVM viewModel)
{
// Check
if (window == null)
{
throw new ArgumentNullException("window");
}
if (viewModel == null)
{
throw new ArgumentNullException("viewModel");
}
//ElementHost.EnableModelessKeyboardInterop(window);
// Assign
Window = window;
Window.Closed += OnWindowClosed;
ViewModel = viewModel;
}
public TW Window { get; set; }
public TVM ViewModel
{
get
{
return this.viewModel;
}
private set
{
if (this.viewModel != null)
{
this.viewModel.Closed -= this.OnViewModelClosed;
this.viewModel.Dispose();
}
this.viewModel = value;
if (this.viewModel != null)
{
this.viewModel.Closed += this.OnViewModelClosed;
this.Window.DataContext = this.ViewModel;
}
}
}
public void ReplaceViewModel(TVM vm)
{
this.ViewModel = vm;
}
public event EventHandler Closed;
public virtual void Dispose()
{
viewModel.Closed -= OnViewModelClosed;
viewModel.Dispose();
viewModel = null;
Window.DataContext = null;
Window.Closed -= OnWindowClosed;
Window.Close();
Window = null;
}
public virtual void Open()
{
Window.Show();
}
public virtual bool? OpenModal()
{
if (Window != null)
{
try
{
var dialogResult = Window.ShowDialog();
var vm = ViewModel as BaseEditableViewModel;
if (vm != null)
{
dialogResult = vm.IsSaved;
}
return dialogResult;
}
catch (Exception)
{
return Window.Focus();
}
}
return false;
}
public void Focus()
{
this.Window.Focus();
this.Window.WindowState = WindowState.Normal;
}
protected void OnClosed()
{
if (Closed != null)
{
Closed(this, EventArgs.Empty);
}
}
/// <summary>
/// If we Close the dialog via |X| button,
/// If it's and EditableVM && we did not save, we force to execute Cancel Command -> It'll call at OnViewModelClosed
/// Else, we fire Closed event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnWindowClosed(object sender, EventArgs e)
{
var editableVm = ViewModel as BaseEditableViewModel;
if (editableVm != null && !editableVm.IsSaved)
{
editableVm.CancelCommand.Execute(null);
}
else
{
OnClosed();
}
}
/// <summary>
/// If we Close the dialog via ViewModel Commnd, we manually close the window && fire Closed event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void OnViewModelClosed(object sender, EventArgs e)
{
Window.Closed -= OnWindowClosed;
Window.Close();
OnClosed();
}
}
BaseViewModel
public abstract class BaseViewModel : IBaseViewModel, INotifyPropertyChanged
{
#region Fields
private bool isBusy;
private string title;
#endregion
#region Properties
public bool IsBusy
{
get { return isBusy; }
set { SetProperty(ref isBusy, value); }
}
public string Title
{
get { return title; }
set { SetProperty(ref title, value); }
}
#endregion
protected bool SetProperty<T>(ref T backingStore, T value, [CallerMemberName]string propertyName = "", Action onChanged = null)
{
if (EqualityComparer<T>.Default.Equals(backingStore, value))
return false;
backingStore = value;
onChanged?.Invoke();
this.OnPropertyChanged(propertyName);
return true;
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
var changed = PropertyChanged;
if (changed == null)
return;
changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
#region Implementation of IDisposable
/// <summary>
/// The dispose.
/// </summary>
public abstract void Dispose();
#endregion Implementation of IDisposable
public event EventHandler Closed;
protected virtual void OnClosed()
{
if (this.Closed != null)
{
this.Closed(this, new EventArgs());
}
}
protected virtual void OnClosed(bool saved)
{
if (this.Closed != null)
{
this.Closed(saved, new EventArgs());
}
}
protected virtual void OnClosed(object @return)
{
if (this.Closed != null)
{
this.Closed(@return, new EventArgs());
}
}
}
BaseEditableViewModel
public abstract class BaseEditableViewModel : BaseViewModel
{
/// <summary>
/// The save command.
/// </summary>
private ICommand saveCommand;
/// <summary>
/// The cancel command.
/// </summary>
private ICommand cancelCommand;
/// <summary>
/// Gets or sets a value indicating whether is saved.
/// </summary>
public virtual bool IsSaved { get; protected set; }
/// <summary>
/// Gets the save.
/// </summary>
public ICommand SaveCommand
{
get
{
return this.saveCommand ?? (this.saveCommand = new RelayCommand(async (x) => await this.SaveCommandExecute(), x => this.SaveCanExecute()));
}
}
/// <summary>
/// Gets the cancel.
/// </summary>
public ICommand CancelCommand
{
get
{
return this.cancelCommand ?? (this.cancelCommand = new RelayCommand(x => this.CancelCommandExecute(), x => this.CancelCanExecute()));
}
}
/// <summary>
/// The save can execute.
/// </summary>
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
public virtual bool SaveCanExecute()
{
return true;
}
/// <summary>
/// The save execute.
/// </summary>
/// <returns>
/// The <see cref="Task"/>.
/// </returns>
public virtual async Task SaveCommandExecute()
{
this.IsSaved = true;
this.OnClosed(this);
}
/// <summary>
/// The cancel can execute.
/// </summary>
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
protected virtual bool CancelCanExecute()
{
return true;
}
/// <summary>
/// The cancel execute.
/// </summary>
protected virtual void CancelCommandExecute()
{
this.IsSaved = false;
this.OnClosed(this);
}
/// <summary>
/// The is state valid.
/// </summary>
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
public virtual bool IsStateValid()
{
return true;
}
/// <summary>
/// The save changes.
/// </summary>
/// <returns>
/// The <see cref="Task"/>.
/// </returns>
public async Task SaveChanges()
{
await this.SaveCommandExecute();
}
}
BaseWindow
public class BaseWindow : Window
{
private readonly ICommand _closeWindow;
public ICommand CloseWindow
{
get { return this._closeWindow; }
}
public BaseWindow()
: this(-1)
{
}
public BaseWindow(int pluginId)
{
this.ApplyCustomization();
this.SetCulture();
this._closeWindow = new RelayCommand(e => this.CloseWindowCmd(), c => true);
this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
}
private void CloseWindowCmd()
{
this.Close();
}
/// <summary>
/// Applies customization to the base form
/// </summary>
private void ApplyCustomization()
{
}
private void SetCulture()
{
}
}
event aggregator
. Te dejo este enlace que espero te aclare un poco el tema.