{"record":{"id":"46d4dc6abbbea83d","repo":"PrismLibrary/Prism","slug":"unabletosetthedialogcloselistener","errorCode":"UnableToSetTheDialogCloseListener","errorMessage":"DialogException.UnableToSetTheDialogCloseListener","messagePattern":"DialogException\\.UnableToSetTheDialogCloseListener","errorType":"error_code","errorClass":"DialogException","httpStatus":null,"severity":"error","filePath":"src/Prism.Core/Dialogs/DialogUtilities.cs","lineNumber":68,"sourceCode":"        {\n            return x => propInfo.SetValue(dialogAware, x);\n        }\n\n        var fields = type.GetRuntimeFields().Where(x => x.FieldType == typeof(DialogCloseListener));\n        var field = fields.FirstOrDefault(x => x.Name == $\"<{nameof(IDialogAware.RequestClose)}>k__BackingField\");\n        if (field is not null)\n        {\n            return x => field.SetValue(dialogAware, x);\n        }\n        else if (fields.Any())\n        {\n            field = fields.First();\n            return x => field.SetValue(dialogAware, x);\n        }\n\n        var baseType = type.BaseType;\n        if (baseType is null || baseType == typeof(object))\n            throw new DialogException(DialogException.UnableToSetTheDialogCloseListener);\n\n        return GetListenerSetter(dialogAware, baseType);\n    }\n}\n","sourceCodeStart":50,"sourceCodeEnd":73,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Core/Dialogs/DialogUtilities.cs#L50-L73","documentation":"DialogUtilities.GetListenerSetter walks the dialog's type hierarchy to find the private DialogCloseListener field; if it reaches a base type of null or object without finding the field, it throws DialogException with code UnableToSetTheDialogCloseListener. This means the class is not a Prism dialog (DialogViewModelBase-derived) and cannot receive the close callback.","triggerScenarios":"Passing an object to DialogService that does not derive from DialogViewModelBase/DialogBase (no _closeListener field anywhere in its hierarchy), e.g. a plain view or wrong view-model registered as a dialog.","commonSituations":"Registering a dialog whose view-model does not inherit Prism's DialogViewModelBase; renaming or removing the listener field via custom base classes; upgrading Prism where the field location changed.","solutions":["Make the dialog's binding context derive from DialogViewModelBase (or DialogBase)","Register the dialog with the correct view/view-model pair so DialogService receives the right instance","Verify the class hierarchy retains Prism's dialog base class","Catch DialogException in dialog registration/show code and validate the type beforehand"],"exampleFix":"// before\npublic class MyDialogViewModel { } // not a Prism dialog VM\n// after\npublic class MyDialogViewModel : DialogViewModelBase { }","handlingStrategy":"type-guard","validationCode":"if (dialogAware is not DialogViewModelBase && dialogAware is not DialogBase)\n    throw new ArgumentException(\"Object is not a Prism dialog\", nameof(dialogAware));","typeGuard":"bool isPrismDialog(IDialogAware d) =>\n    d.GetType().BaseType != null && typeof(DialogViewModelBase).IsAssignableFrom(d.GetType());","tryCatchPattern":"try { await dialogService.ShowDialogAsync(name, parameters, cb); }\ncatch (DialogException ex) when (ex.Code == DialogExceptionCode.UnableToSetTheDialogCloseListener)\n{ logger.LogError(ex, \"'{Name}' is not a Prism dialog\", name); }","preventionTips":["Derive all dialog view-models from DialogViewModelBase","Verify dialog registration pairs view and view-model correctly","Do not remove Prism's private close-listener field via custom base classes","Add a startup assertion that each registered dialog inherits the Prism base class"],"tags":["dialog","reflection","prism","type-mismatch"],"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"}