{"record":{"id":"011b3f48b450b9c9","repo":"dotnet/wpf","slug":"sr-format-sr-parserbamlevent-eventidname","errorCode":null,"errorMessage":"SR.Format(SR.ParserBamlEvent, eventIdName)","messagePattern":"SR\\.Format\\(SR\\.ParserBamlEvent, eventIdName\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlWriter.cs","lineNumber":901,"sourceCode":"            string     ownerTypeFullName,\n            string     eventIdName,\n            string     handlerName)\n        {\n#if EVENTSUPPORT            \n            VerifyWriteState();\n\n            XamlRoutedEventNode eventNode = new XamlRoutedEventNode(\n                                                      0,\n                                                      0,\n                                                      _depth,\n                                                      null,\n                                                      assemblyName,\n                                                      ownerTypeFullName,\n                                                      eventIdName,\n                                                      handlerName);\n            _bamlRecordWriter.WriteRoutedEvent(eventNode);\n#else\n            throw new NotSupportedException(SR.Format(SR.ParserBamlEvent, eventIdName));\n#endif\n\n        }\n\n        /// <summary>\n        /// Write an event record to BAML.  \n        /// </summary>\n        /// <remarks>\n        /// The Avalon parser does not process event records itself and will\n        /// throw an exception if it encounters  an event record in BAML.  It\n        /// is included here for completeness and future expandability.\n        /// </remarks>\n        public void WriteEvent(\n            string    eventName,\n            string    handlerName)\n        {\n#if EVENTSUPPORT            \n            VerifyWriteState();","sourceCodeStart":883,"sourceCodeEnd":919,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlWriter.cs#L883-L919","documentation":"When compiled without BAML event-record support (the #if branch is excluded), WriteRoutedEvent unconditionally throws NotSupportedException(SR.Format(SR.ParserBamlEvent, eventIdName)), stating that BAML cannot encode routed events. This build configuration simply cannot represent routed-event records in BAML output.","triggerScenarios":"Calling WriteRoutedEvent on a build where the routed-event BAML record code path is compiled out (#else branch active); serializing XAML containing routed event attributes (e.g. Button Click=\"OnClick\") through BamlWriter in that configuration.","commonSituations":"Using a WPF build/branch with event record support disabled; porting BamlWriter-based serialization code to a stripped-down build and hitting events in the input XAML.","solutions":["Use a build of PresentationFramework where routed-event BAML records are enabled (the #if-compiled path).","Strip routed event attributes from the XAML before serialization, wiring handlers in code instead.","Capture the NotSupportedException and fall back to saving as XAML text rather than BAML."],"exampleFix":"// before\nwriter.WriteRoutedEvent(\"Click\", \"System.Windows.Controls.Button\", \"OnClick\");\n// after\n// remove the event attribute; attach handler in code:\nbutton.Click += OnClick;","handlingStrategy":"fallback","validationCode":"// Detect routed-event attributes in source XAML before serialization:\nbool hasRoutedEvents = xamlText.Contains(\"Click=\") || Regex.IsMatch(xamlText, \"\\w+\\.\\w+\\s*=\"); // heuristic; better: scan XAML event attributes via XamlReader","typeGuard":null,"tryCatchPattern":"try { writer.WriteRoutedEvent(eventName, ownerType, handler); }\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"event\"))\n{\n    // fall back to XAML text output for this document\n}","preventionTips":["Confirm event-record support is compiled into your PresentationFramework build.","Strip event attributes and attach handlers in code-behind.","Prefer XAML text serialization when events must be preserved."],"tags":["wpf","baml","not-supported","routed-events"],"backgroundTag":"operation-not-supported","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}