{"record":{"id":"56793b9ca9db4c0f","repo":"PrismLibrary/Prism","slug":"a-dialog-s-viewmodel-must-implement-the-idialogaware-56793b","errorCode":null,"errorMessage":"A dialog's ViewModel must implement the IDialogAware interface ({dialogContent.DataContext})","messagePattern":"A dialog's ViewModel must implement the IDialogAware interface \\((.+?)\\)","errorType":"exception","errorClass":"NullReferenceException","httpStatus":null,"severity":"error","filePath":"src/Uno/Prism.Uno/Dialogs/DialogService.cs","lineNumber":66,"sourceCode":"            if (string.IsNullOrWhiteSpace(name))\n                return _containerProvider.Resolve<IDialogWindow>();\n            else\n                return _containerProvider.Resolve<IDialogWindow>(name);\n        }\n\n        void ConfigureDialogWindowContent(string dialogName, IDialogWindow window, IDialogParameters parameters)\n        {\n            var content = _containerProvider.Resolve<object>(dialogName);\n            if (content is not FrameworkElement dialogContent)\n            {\n                throw new NullReferenceException(\"A dialog's content must be a FrameworkElement\");\n            }\n\n            MvvmHelpers.AutowireViewModel(content);\n\n            if (dialogContent.DataContext is not IDialogAware viewModel)\n            {\n                throw new NullReferenceException($\"A dialog's ViewModel must implement the IDialogAware interface ({dialogContent.DataContext})\");\n            }\n\n            DialogService.ConfigureDialogWindowProperties(window, dialogContent, viewModel);\n\n            MvvmHelpers.ViewAndViewModelAction<IDialogAware>(viewModel, d => d.OnDialogOpened(parameters));\n        }\n\n        void ConfigureDialogWindowEvents(IDialogWindow contentDialog, DialogCallback callback)\n        {\n            IDialogResult? result = null;\n\n            void RequestCloseHandler(IDialogResult r)\n            {\n                result = r ?? new DialogResult();\n                contentDialog.Hide();\n            }\n\n            RoutedEventHandler loadedHandler = null!;","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Uno/Prism.Uno/Dialogs/DialogService.cs#L48-L84","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Make the dialog's ViewModel implement IDialogAware (OnDialogOpened, CanCloseDialog, OnDialogClosed, RequestClose) and set it as the view's DataContext.","If using ViewModelLocator, ensure the naming convention resolves the correct ViewModel that implements IDialogAware.","Ensure the view's DataContext is not null: use prism:ViewModelLocator.AutowireViewModel=\"True\" or set DataContext in code/constructor.","Call d.OnDialogOpened(parameters) expectations: verify the parameters passed to ShowDialog can be consumed by your IDialogAware implementation."],"exampleFix":"// before\npublic class MyDialogViewModel { /* no IDialogAware */ }\n\n// after\npublic class MyDialogViewModel : IDialogAware\n{\n    public event EventHandler<DialogResult> RequestClose;\n    public bool CanCloseDialog() => true;\n    public void OnDialogClosed() { }\n    public void OnDialogOpened(IDialogParameters parameters) { }\n    public string Title => \"My Dialog\";\n}","handlingStrategy":"type-guard","validationCode":"// verify before showing\nvar view = containerProvider.Resolve<object>(dialogName) as FrameworkElement;\nvar vm = MvvmHelpers.GetImplementer(view?.DataContext) ?? view?.DataContext;\nif (vm is not IDialogAware)\n    throw new InvalidOperationException($\"ViewModel for dialog '{dialogName}' must implement IDialogAware.\");","typeGuard":"static bool HasDialogAwareViewModel(FrameworkElement view) =>\n    view?.DataContext is IDialogAware;","tryCatchPattern":"try\n{\n    dialogService.ShowDialog(dialogName, parameters, callback);\n}\ncatch (NullReferenceException ex) when (ex.Message.StartsWith(\"A dialog's ViewModel must implement\"))\n{\n    logger.LogError(ex, \"Dialog {Dialog} ViewModel lacks IDialogAware\", dialogName);\n}","preventionTips":["Implement IDialogAware on every dialog ViewModel","Enable prism:ViewModelLocator.AutowireViewModel on dialog views","Write a test asserting each dialog's DataContext implements IDialogAware","During Prism upgrades, audit all dialogs for the IDialogAware contract"],"tags":["prism","uno","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"}