{"record":{"id":"d71c2d72f503fd33","repo":"PrismLibrary/Prism","slug":"a-dialog-s-viewmodel-must-implement-the-idialogaware","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/Avalonia/Prism.Avalonia/Dialogs/DialogService.cs","lineNumber":94,"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 Avalonia.Controls.Control dialogContent))\n                throw new NullReferenceException(\"A dialog's content must be an Avalonia.Controls.Control\");\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 = (result) =>\n            {\n                dialogWindow.Result = result;\n                dialogWindow.Close();\n            };","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Avalonia/Prism.Avalonia/Dialogs/DialogService.cs#L76-L112","documentation":"After resolving the dialog's view, Prism autowires its ViewModel and requires the view's DataContext to implement IDialogAware so the service can drive dialog lifecycle (OnDialogOpened, RequestClose, etc.). If the resolved DataContext does not implement IDialogAware, DialogService throws this NullReferenceException.","triggerScenarios":"ShowDialog(\"MyDialog\") where MyDialogView's ViewModel (autowired or explicitly set as DataContext) does not implement IDialogAware, or the view sets its own DataContext to a non-IDialogAware class overriding the autowired one, or AutowireViewModel fails to wire a ViewModel leaving DataContext null or wrong type.","commonSituations":"ViewModel was written before migrating to Prism dialogs and lacks IDialogAware; view explicitly sets DataContext in XAML/code-behind to a plain VM; ViewModelLocator binds a VM that doesn't implement the interface; refactoring renamed IDialogAware implementation methods so the interface is no longer implemented.","solutions":["Make the dialog's ViewModel implement IDialogAware (OnDialogOpened, CanCloseDialog, OnDialogClosed, RequestClose).","Remove explicit DataContext assignments so Prism's MvvmHelpers.AutowireViewModel wires the correct ViewModel.","Register the dialog as a pair RegisterDialog<MyDialogView, MyDialogViewModel>() so Prism knows the ViewModel type.","Verify ViewModelLocator.AutowireViewModel setting on the view doesn't bind an unintended ViewModel."],"exampleFix":"// before\npublic class MyDialogViewModel { }\n// after\npublic class MyDialogViewModel : IDialogAware\n{\n    public bool CanCloseDialog() => true;\n    public void OnDialogClosed() { }\n    public void OnDialogOpened(IDialogParameters parameters) { }\n    public string Title { get; set; }\n    public event EventHandler<IDialogResult> RequestClose;\n}","handlingStrategy":"type-guard","validationCode":"if (myDialogViewModel is not IDialogAware)\n    throw new InvalidOperationException(\"Dialog ViewModel must implement IDialogAware\");","typeGuard":"bool IsDialogViewModel(object vm) => vm is IDialogAware;","tryCatchPattern":null,"preventionTips":["Make every dialog ViewModel implement IDialogAware at creation time","Do not set DataContext explicitly on dialog views; let Prism autowire","Use a shared base class implementing IDialogAware for all dialog VMs"],"tags":["avalonia","dialogs","idialogaware","viewmodel"],"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"}