PrismLibrary/Prism · error · NullReferenceException

A dialog's content must be a FrameworkElement

Error message

A dialog's content must be a FrameworkElement

What it means

The WPF DialogService resolves the view registered for the dialog name and requires the resolved object to be a FrameworkElement to host it in the dialog window. If the container resolves a non-FrameworkElement (or null), it throws NullReferenceException. Dialogs must be registered as views, not arbitrary types.

Solutions

  1. Register the dialog view: containerRegistry.RegisterDialog<MyDialogView, MyDialogViewModel>();
  2. Ensure the name passed to ShowDialog matches the registered view name (or type name).
  3. Make the dialog a UserControl or other FrameworkElement-derived type.
  4. Confirm registration happens before ShowDialog (module initialized, App.RegisterTypes executed).

Example fix

// before
containerRegistry.RegisterForNavigation<MyDialogViewModel>(); // wrong type registered for dialog

// after
containerRegistry.RegisterDialog<MyDialogView, MyDialogViewModel>();
Defensive patterns

Strategy: type-guard

Validate before calling

// before calling ShowDialog (WPF)
var content = containerExtension.Resolve<object>(dialogName);
if (!(content is FrameworkElement))
    throw new InvalidOperationException($"Dialog '{dialogName}' must resolve to a FrameworkElement.");

Type guard

static bool IsValidDialogRegistration(IContainerExtension ext, string name) =>
    ext.Resolve<object>(name) is FrameworkElement;

Try / catch

try
{
    dialogService.ShowDialog(dialogName, parameters, callback);
}
catch (NullReferenceException ex) when (ex.Message.Contains("must be a FrameworkElement"))
{
    logger.LogError(ex, "Dialog {Dialog} registration invalid (not a view)", dialogName);
}

Prevention

When it happens

Trigger: Calling IDialogService.ShowDialog(name, ...) where the registration for `name` (via RegisterDialog or a manual container registration) resolves to a type that is not a FrameworkElement, or where the name is registered to a ViewModel-only type.

Common situations: Registering the ViewModel instead of the view; typos in the dialog name falling back to a wrong registration; dialogs defined as plain classes or Windows that were refactored to UserControls incorrectly; registering dialogs after ShowDialog is invoked (module not yet initialized).

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

        protected virtual IDialogWindow CreateDialogWindow(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
                return _containerExtension.Resolve<IDialogWindow>();
            else
                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)
        {

View on GitHub (pinned to 358118cd64)