PrismLibrary/Prism · error · NullReferenceException

A dialog's ViewModel must implement the IDialogAware…

Error message

A dialog's ViewModel must implement the IDialogAware interface

What it means

After resolving the dialog's view, Prism autowires its ViewModel and requires the view's DataContext to implement IDialogAware so the service can drive dialog lifecycle (OnDialogOpened, RequestClose, etc.). If the resolved DataContext does not implement IDialogAware, DialogService throws this NullReferenceException.

Solutions

  1. Make the dialog's ViewModel implement IDialogAware (OnDialogOpened, CanCloseDialog, OnDialogClosed, RequestClose).
  2. Remove explicit DataContext assignments so Prism's MvvmHelpers.AutowireViewModel wires the correct ViewModel.
  3. Register the dialog as a pair RegisterDialog<MyDialogView, MyDialogViewModel>() so Prism knows the ViewModel type.
  4. Verify ViewModelLocator.AutowireViewModel setting on the view doesn't bind an unintended ViewModel.

Example fix

// before
public class MyDialogViewModel { }
// after
public class MyDialogViewModel : IDialogAware
{
    public bool CanCloseDialog() => true;
    public void OnDialogClosed() { }
    public void OnDialogOpened(IDialogParameters parameters) { }
    public string Title { get; set; }
    public event EventHandler<IDialogResult> RequestClose;
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (myDialogViewModel is not IDialogAware)
    throw new InvalidOperationException("Dialog ViewModel must implement IDialogAware");

Type guard

bool IsDialogViewModel(object vm) => vm is IDialogAware;

Prevention

When it happens

Trigger: ShowDialog("MyDialog") where MyDialogView's ViewModel (autowired or explicitly set as DataContext) does not implement IDialogAware, or the view sets its own DataContext to a non-IDialogAware class overriding the autowired one, or AutowireViewModel fails to wire a ViewModel leaving DataContext null or wrong type.

Common situations: ViewModel was written before migrating to Prism dialogs and lacks IDialogAware; view explicitly sets DataContext in XAML/code-behind to a plain VM; ViewModelLocator binds a VM that doesn't implement the interface; refactoring renamed IDialogAware implementation methods so the interface is no longer implemented.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/d71c2d72f503fd33. Report an issue: GitHub.

Appendix: source

Thrown at src/Avalonia/Prism.Avalonia/Dialogs/DialogService.cs:94

                return _containerExtension.Resolve<IDialogWindow>(name);
        }

        /// <summary>
        /// Configure <see cref="IDialogWindow"/> content.
        /// </summary>
        /// <param name="dialogName">The name of the dialog to show.</param>
        /// <param name="window">The hosting window.</param>
        /// <param name="parameters">The parameters to pass to the dialog.</param>
        protected virtual void ConfigureDialogWindowContent(string dialogName, IDialogWindow window, IDialogParameters parameters)
        {
            var content = _containerExtension.Resolve<object>(dialogName);
            if (!(content is Avalonia.Controls.Control dialogContent))
                throw new NullReferenceException("A dialog's content must be an Avalonia.Controls.Control");

            MvvmHelpers.AutowireViewModel(dialogContent);

            if (!(dialogContent.DataContext is IDialogAware viewModel))
                throw new NullReferenceException("A dialog's ViewModel must implement the IDialogAware interface");

            ConfigureDialogWindowProperties(window, dialogContent, viewModel);

            MvvmHelpers.ViewAndViewModelAction<IDialogAware>(viewModel, d => d.OnDialogOpened(parameters));
        }

        /// <summary>
        /// Configure <see cref="IDialogWindow"/> and <see cref="IDialogAware"/> events.
        /// </summary>
        /// <param name="dialogWindow">The hosting window.</param>
        /// <param name="callback">The action to perform when the dialog is closed.</param>
        protected virtual void ConfigureDialogWindowEvents(IDialogWindow dialogWindow, DialogCallback callback)
        {
            Action<IDialogResult> requestCloseHandler = (result) =>
            {
                dialogWindow.Result = result;
                dialogWindow.Close();
            };

View on GitHub (pinned to 358118cd64)