{"record":{"id":"d0e3d63be9292ef8","repo":"PrismLibrary/Prism","slug":"the-dialogclosecallback-has-not-been-properly-initialized","errorCode":null,"errorMessage":"The DialogCloseCallback has not been properly initialized. This must be initialized by the DialogService, and should not be set by user code.","messagePattern":"The DialogCloseCallback has not been properly initialized\\. This must be initialized by the DialogService, and should not be set by user code\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Prism.Core/Dialogs/DialogCloseListener.cs","lineNumber":72,"sourceCode":"            Result = result\n        });\n\n    /// <summary>\n    /// Invokes the initialized delegate with the specified <see cref=\"IDialogResult\"/>\n    /// </summary>\n    /// <param name=\"result\"></param>\n    public async void Invoke(IDialogResult result)\n    {\n        switch(_callback)\n        {\n            case Action<IDialogResult> actionCallback:\n                actionCallback(result);\n                break;\n            case Func<IDialogResult, Task> taskCallback:\n                await taskCallback(result);\n                break;\n            default:\n                throw new InvalidOperationException(\"The DialogCloseCallback has not been properly initialized. This must be initialized by the DialogService, and should not be set by user code.\");\n        }\n    }\n}\n","sourceCodeStart":54,"sourceCodeEnd":76,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Core/Dialogs/DialogCloseListener.cs#L54-L76","documentation":"DialogCloseListener wraps the callback the DialogService injects into a dialog. Invoking it before/without DialogService initialization leaves the internal callback null (default case), so it throws InvalidOperationException explaining the listener must be set by DialogService and not by user code.","triggerScenarios":"Calling RaiseRequestClose / DialogCloseListener.Invoke from a dialog view-model whose listener was never injected, e.g. constructing the dialog manually with new instead of through IDialogService, or closing before OnDialogOpened runs.","commonSituations":"Manually instantiated dialogs in unit tests; calling RaiseRequestClose inside constructors or before the dialog is shown; copying dialog code without the DialogService pipeline.","solutions":["Show the dialog via IDialogService.ShowDialogAsync so the listener is injected","Only call RaiseRequestClose after OnDialogOpened has executed","Never assign/construct DialogCloseListener yourself","In tests, set the private callback field via reflection or use the DialogService"],"exampleFix":"// before\nvar dialog = new MyDialog();\ndialog.RaiseRequestClose(new DialogResult()); // listener not set\n// after\nawait dialogService.ShowDialogAsync(nameof(MyDialog), parameters, callback);","handlingStrategy":"try-catch","validationCode":"// Only close dialogs that went through IDialogService\nbool canClose = _dialogOpened; // set true in OnDialogOpened\nif (!canClose) return;","typeGuard":"bool listenerInitialized(IDialogAware dialog) =>\n    (bool)(dialog.GetType()\n        .GetField(\"_listener\", BindingFlags.NonPublic | BindingFlags.Instance)\n        ?.GetValue(dialog) is DialogCloseListener l && l.IsInitialized);","tryCatchPattern":"try { RaiseRequestClose(new DialogResult()); }\ncatch (InvalidOperationException ex) { logger.LogError(ex, \"Dialog close attempted before initialization\"); }","preventionTips":["Always show dialogs via IDialogService.ShowDialogAsync","Never call RaiseRequestClose before OnDialogOpened","Never construct or assign DialogCloseListener manually","Guard RaiseRequestClose calls with an OnDialogOpened-completed flag"],"tags":["dialog","invalid-state","prism"],"backgroundTag":"invalid-state-transition","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}