PrismLibrary/Prism · error · NullReferenceException

A dialog's ViewModel must implement the IDialogAware…

Error message

A dialog's ViewModel must implement the IDialogAware interface ({dialogContent.DataContext})

What it means

After resolving and autowiring the dialog view, DialogService requires the view's DataContext (the ViewModel) to implement IDialogAware so Prism can invoke OnDialogOpened/OnDialogClosed and request closing. If the DataContext is null or does not implement IDialogAware, this NullReferenceException is thrown. The message includes the offending DataContext value to help identify it.

Solutions

  1. Make the dialog's ViewModel implement IDialogAware (OnDialogOpened, CanCloseDialog, OnDialogClosed, RequestClose) and set it as the view's DataContext.
  2. If using ViewModelLocator, ensure the naming convention resolves the correct ViewModel that implements IDialogAware.
  3. Ensure the view's DataContext is not null: use prism:ViewModelLocator.AutowireViewModel="True" or set DataContext in code/constructor.
  4. Call d.OnDialogOpened(parameters) expectations: verify the parameters passed to ShowDialog can be consumed by your IDialogAware implementation.

Example fix

// before
public class MyDialogViewModel { /* no IDialogAware */ }

// after
public class MyDialogViewModel : IDialogAware
{
    public event EventHandler<DialogResult> RequestClose;
    public bool CanCloseDialog() => true;
    public void OnDialogClosed() { }
    public void OnDialogOpened(IDialogParameters parameters) { }
    public string Title => "My Dialog";
}
Defensive patterns

Strategy: type-guard

Validate before calling

// verify before showing
var view = containerProvider.Resolve<object>(dialogName) as FrameworkElement;
var vm = MvvmHelpers.GetImplementer(view?.DataContext) ?? view?.DataContext;
if (vm is not IDialogAware)
    throw new InvalidOperationException($"ViewModel for dialog '{dialogName}' must implement IDialogAware.");

Type guard

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

Try / catch

try
{
    dialogService.ShowDialog(dialogName, parameters, callback);
}
catch (NullReferenceException ex) when (ex.Message.StartsWith("A dialog's ViewModel must implement"))
{
    logger.LogError(ex, "Dialog {Dialog} ViewModel lacks IDialogAware", dialogName);
}

Prevention

When it happens

Trigger: Calling IDialogService.ShowDialog for a dialog whose ViewModel (or view with code-behind DataContext) does not implement IDialogAware; a view whose DataContext is null because no ViewModel was bound and AutowireViewModel did not produce an IDialogAware object.

Common situations: Upgrading Prism versions where dialogs newly require IDialogAware; creating a view without a ViewModel or with a ViewModel lacking OnDialogOpened/RequestClose; ViewModelLocator resolving the wrong type so DataContext is a plain model; forgetting to implement the IDialogAware interface members after refactoring.

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/56793b9ca9db4c0f. Report an issue: GitHub.

Appendix: source

Thrown at src/Uno/Prism.Uno/Dialogs/DialogService.cs:66

            if (string.IsNullOrWhiteSpace(name))
                return _containerProvider.Resolve<IDialogWindow>();
            else
                return _containerProvider.Resolve<IDialogWindow>(name);
        }

        void ConfigureDialogWindowContent(string dialogName, IDialogWindow window, IDialogParameters parameters)
        {
            var content = _containerProvider.Resolve<object>(dialogName);
            if (content is not FrameworkElement dialogContent)
            {
                throw new NullReferenceException("A dialog's content must be a FrameworkElement");
            }

            MvvmHelpers.AutowireViewModel(content);

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

            DialogService.ConfigureDialogWindowProperties(window, dialogContent, viewModel);

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

        void ConfigureDialogWindowEvents(IDialogWindow contentDialog, DialogCallback callback)
        {
            IDialogResult? result = null;

            void RequestCloseHandler(IDialogResult r)
            {
                result = r ?? new DialogResult();
                contentDialog.Hide();
            }

            RoutedEventHandler loadedHandler = null!;

View on GitHub (pinned to 358118cd64)