{"record":{"id":"6c893a2296910a61","repo":"PrismLibrary/Prism","slug":"a-dialog-s-viewmodel-must-implement-the-idialogaware-6c893a","errorCode":null,"errorMessage":"A dialog's ViewModel must implement the IDialogAware interface","messagePattern":"A dialog's ViewModel must implement the IDialogAware interface","errorType":"exception","errorClass":"NullReferenceException","httpStatus":null,"severity":"error","filePath":"src/Wpf/Prism.Wpf/Dialogs/DialogService.cs","lineNumber":85,"sourceCode":"                return _containerExtension.Resolve<IDialogWindow>(name);\n        }\n\n        /// <summary>\n        /// Configure <see cref=\"IDialogWindow\"/> content.\n        /// </summary>\n        /// <param name=\"dialogName\">The name of the dialog to show.</param>\n        /// <param name=\"window\">The hosting window.</param>\n        /// <param name=\"parameters\">The parameters to pass to the dialog.</param>\n        protected virtual void ConfigureDialogWindowContent(string dialogName, IDialogWindow window, IDialogParameters parameters)\n        {\n            var content = _containerExtension.Resolve<object>(dialogName);\n            if (!(content is FrameworkElement dialogContent))\n                throw new NullReferenceException(\"A dialog's content must be a FrameworkElement\");\n\n            MvvmHelpers.AutowireViewModel(dialogContent);\n\n            if (!(dialogContent.DataContext is IDialogAware viewModel))\n                throw new NullReferenceException(\"A dialog's ViewModel must implement the IDialogAware interface\");\n\n            ConfigureDialogWindowProperties(window, dialogContent, viewModel);\n\n            MvvmHelpers.ViewAndViewModelAction<IDialogAware>(viewModel, d => d.OnDialogOpened(parameters));\n        }\n\n        /// <summary>\n        /// Configure <see cref=\"IDialogWindow\"/> and <see cref=\"IDialogAware\"/> events.\n        /// </summary>\n        /// <param name=\"dialogWindow\">The hosting window.</param>\n        /// <param name=\"callback\">The action to perform when the dialog is closed.</param>\n        protected virtual void ConfigureDialogWindowEvents(IDialogWindow dialogWindow, DialogCallback callback)\n        {\n            Action<IDialogResult> requestCloseHandler = (r) =>\n            {\n                dialogWindow.Result = r;\n                dialogWindow.Close();\n            };","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Wpf/Prism.Wpf/Dialogs/DialogService.cs#L67-L103","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Implement IDialogAware on the dialog's ViewModel (or code-behind class that serves as DataContext).","Ensure the view has a DataContext: use prism:ViewModelLocator.AutowireViewModel=\"True\" or set it explicitly.","If you set DataContext manually, set it to the IDialogAware-implementing object.","Check that ConfigureDialogWindowProperties/OnDialogOpened flow: the DataContext at ShowDialog time must already be the IDialogAware instance."],"exampleFix":"<!-- before -->\n<UserControl x:Class=\"MyDialogView\" /> <!-- DataContext null or non-IDialogAware -->\n\n<!-- after -->\n<UserControl x:Class=\"MyDialogView\"\n             xmlns:prism=\"http://prismlibrary.com/\"\n             prism:ViewModelLocator.AutowireViewModel=\"True\" /> <!-- with IDialogAware ViewModel -->","handlingStrategy":"type-guard","validationCode":"// verify DataContext contract before showing (WPF)\nvar view = containerExtension.Resolve<object>(dialogName) as FrameworkElement;\nif (!(view?.DataContext is IDialogAware))\n    throw new InvalidOperationException($\"Dialog '{dialogName}' DataContext must implement IDialogAware.\");","typeGuard":"static bool IsDialogAwareBound(FrameworkElement view) =>\n    view?.DataContext is IDialogAware;","tryCatchPattern":"try\n{\n    dialogService.ShowDialog(dialogName, parameters, callback);\n}\ncatch (NullReferenceException ex) when (ex.Message.Contains(\"IDialogAware\"))\n{\n    logger.LogError(ex, \"Dialog {Dialog} lacks IDialogAware DataContext\", dialogName);\n}","preventionTips":["Make every dialog ViewModel implement IDialogAware","Autowire the ViewModel via prism:ViewModelLocator.AutowireViewModel=\"True\"","Add a convention test that every *DialogViewModel implements IDialogAware","Check for IDialogAware compliance when migrating from Prism 7.x to 8+"],"tags":["prism","wpf","dialogs","idialogaware","mvvm"],"backgroundTag":"type-mismatch","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}