Implement INotifyPropertyChanged on the ViewModel.
To map with a View, see ViewLocator
To open dialogs, see Dialogs
To display a list, see CollectionView
Implement IModalDialogViewModel for a dialog opened with ShowDialogAsync.
bool? DialogResult returns true for acceptance, false for an explicit cancel, or null for dismissal without a result. The caller can read edited fields from the same ViewModel after acceptance.
Set DialogResult before raising ICloseable.RequestClose from the dialog's Accept or Cancel command.
public bool? DialogResult { get; private set; }
public event EventHandler? RequestClose;
private void Complete(bool accepted)
{
DialogResult = accepted;
RequestClose?.Invoke(this, EventArgs.Empty);
}Use the lifecycle interfaces below for callbacks and requests on the View.
Open the main ViewModel through IDialogService.Show to receive these callbacks (except in MAUI). In single-page navigation, the ViewLocator determines whether a child opens as a page or popup.
void IViewLoaded.OnLoaded(): Runs when the ViewModel is first presented. Returning to a page or recreating its view continues the same opening. Closing and reopening the same ViewModel instance runs the callback again.
void IViewClosing.OnClosing(CancelEventArgs e): Leave e.Cancel as false to approve closure, or set it to true to run OnClosingAsync.
Task IViewClosing.OnClosingAsync(CancelEventArgs e): Set e.Cancel = false to approve closure. Leave it true to keep the view open.
void IViewClosed.OnClosed(): Runs after the view closes. Use it to release resources or unregister listeners.
event IActivable.RequestActivate: Asks the host to activate the view when raised.
event ICloseable.RequestClose: Asks the host to close the view when raised.
This example asks for confirmation when there are unsaved changes. Yes approves the request. No keeps the view open.
public void OnClosing(CancelEventArgs e) => e.Cancel = HasUnsavedChanges;
public async Task OnClosingAsync(CancelEventArgs e)
{
e.Cancel = await dialogs.ShowMessageBoxAsync(this, "Discard changes?", "Close",
MessageBoxButton.YesNo, defaultResult: false) != true;
}This example applies to managed windows and navigation pages. For a popup, show the confirmation inside the open popup to avoid a stall.
When WorkspaceCollectionView invokes a tab’s close callback, pass the containing window or navigation page’s registered view model as the first argument to ShowMessageBoxAsync.
Let's say you have an application open with 5 document tabs. Closing can ask whether to save or veto each document closure before closing the app.
On desktop, the dialog manager asks each managed descendant to close before closing its owner. The window's Close button and dialogService.Close(ownerViewModel) request the same sequence. In single-page navigation, closing a page that owns modal pages asks those pages for approval.
On desktop, the children close one by one after confirmation. In single-page navigation, the closure waits until all children are confirmed.
On Avalonia desktop, the dialog manager checks descendants only when the owner window’s ClosingBehavior is OwnerAndChildWindows.
Use a data template to display a ViewModel as content inside an existing view, such as a settings panel inside a window.
Use WorkspaceCollectionView to manage ViewModel lifecycle and close vetoes for document tabs bound through ItemsSource. Add starts the lifecycle, RequestCloseAsync asks the ViewModel to close, and a data template renders the tab's content.
Implement IView when the dialog service needs to open, activate, or close a custom host. Register the adapter with RegisterCustom<TViewModel, TView>(). Use the factory overload when the adapter needs constructor arguments.
Bind document tabs and tool panels to workspace collections in the Workspace demo. The PhotoCraft demo uses a custom IView to control an external editor.