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 view and autowiring its ViewModel, the WPF DialogService requires the view's DataContext to implement IDialogAware so Prism can drive open/close lifecycle (OnDialogOpened, RequestClose). Otherwise it throws NullReferenceException. WPF variant of the same contract enforced in the Uno DialogService.

Solutions

  1. Implement IDialogAware on the dialog's ViewModel (or code-behind class that serves as DataContext).
  2. Ensure the view has a DataContext: use prism:ViewModelLocator.AutowireViewModel="True" or set it explicitly.
  3. If you set DataContext manually, set it to the IDialogAware-implementing object.
  4. Check that ConfigureDialogWindowProperties/OnDialogOpened flow: the DataContext at ShowDialog time must already be the IDialogAware instance.

Example fix

<!-- before -->
<UserControl x:Class="MyDialogView" /> <!-- DataContext null or non-IDialogAware -->

<!-- after -->
<UserControl x:Class="MyDialogView"
             xmlns:prism="http://prismlibrary.com/"
             prism:ViewModelLocator.AutowireViewModel="True" /> <!-- with IDialogAware ViewModel -->
Defensive patterns

Strategy: type-guard

Validate before calling

// verify DataContext contract before showing (WPF)
var view = containerExtension.Resolve<object>(dialogName) as FrameworkElement;
if (!(view?.DataContext is IDialogAware))
    throw new InvalidOperationException($"Dialog '{dialogName}' DataContext must implement IDialogAware.");

Type guard

static bool IsDialogAwareBound(FrameworkElement view) =>
    view?.DataContext is IDialogAware;

Try / catch

try
{
    dialogService.ShowDialog(dialogName, parameters, callback);
}
catch (NullReferenceException ex) when (ex.Message.Contains("IDialogAware"))
{
    logger.LogError(ex, "Dialog {Dialog} lacks IDialogAware DataContext", dialogName);
}

Prevention

When it happens

Trigger: Calling IDialogService.ShowDialog for a dialog whose view's DataContext is null or whose ViewModel does not implement IDialogAware; ViewModelLocator resolving a type that lacks IDialogAware; manually setting DataContext to a model or service.

Common situations: Dialogs written for pre-IDialogAware Prism versions (before 8.0) upgraded to a version requiring IDialogAware; missing ViewModel binding so DataContext is null; implementing the interface on the View code-behind but DataContext ends up being the ViewModel (or vice versa); refactoring that removed the interface implementation.

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/6c893a2296910a61. Report an issue: GitHub.

Appendix: source

Thrown at src/Wpf/Prism.Wpf/Dialogs/DialogService.cs:85

                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 FrameworkElement dialogContent))
                throw new NullReferenceException("A dialog's content must be a FrameworkElement");

            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 = (r) =>
            {
                dialogWindow.Result = r;
                dialogWindow.Close();
            };

View on GitHub (pinned to 358118cd64)