Table of Contents

Interface IClosableViewModel

Namespace
EasySoftware.MvvmMini.Core
Assembly
EasySoftware.MvvmMini.Core.dll

A view model with a lifecycle that ends when it is closed.

public interface IClosableViewModel : IViewModel, IModel, INotifyPropertyChanged, INotifyDataErrorInfo, IErrorContainer
Inherited Members

Examples

public override async Task OnClosing(CancelEventArgs e)
{
    if (!this.IsDirty)
        return;

    var answer = await this._dialogs.AskAsync("Do you want to save changes?");
    if (answer == Answer.Yes)
        await this.SaveAsync();
    else if (answer == Answer.Cancel)
        e.Cancel = true;
}

Remarks

A closable view model is not limited to windows and dialogs. It can be anything the user opens, works with and then closes, for example a document tab in an editor, a panel or a wizard page.

Before closing, the view model gets a chance to stop it in OnClosing(CancelEventArgs). A typical use is a document with unsaved changes: it asks the user "Do you want to save changes?", saves on Yes, closes on No, and cancels closing on Cancel.

When closing goes ahead, Closed is raised so the owner, such as a shell that keeps the list of open documents, can remove the view model and release it.

Properties

CloseCommand

Gets the command that asks the view model to close. Bind it to a Close button, a tab's close icon or a menu item. Closing still goes through OnClosing(CancelEventArgs) and can be cancelled there.

ICommand CloseCommand { get; }

Property Value

ICommand

Methods

OnClosing(CancelEventArgs)

Called before the view model is closed, so it can decide whether closing may go ahead. For example, a view model with unsaved changes can ask the user whether to save them, and set Cancel to true to stay open if the user cancels.

Task OnClosing(CancelEventArgs e)

Parameters

e CancelEventArgs

The event data. Set Cancel to true to cancel closing.

Returns

Task

A task that completes when the view model has decided whether to close.

Events

Closed

Occurs after the view model has been closed. The owner of the view model, for example the shell that hosts it as a document, can handle it to remove and release the view model.

event EventHandler Closed

Event Type

EventHandler