dotnet/wpf · error

SR.Format(SR.ParserBamlEvent, eventIdName)

Error message

SR.Format(SR.ParserBamlEvent, eventIdName)

What it means

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.

Solutions

  1. Use a build of PresentationFramework where routed-event BAML records are enabled (the #if-compiled path).
  2. Strip routed event attributes from the XAML before serialization, wiring handlers in code instead.
  3. Capture the NotSupportedException and fall back to saving as XAML text rather than BAML.

Example fix

// before
writer.WriteRoutedEvent("Click", "System.Windows.Controls.Button", "OnClick");
// after
// remove the event attribute; attach handler in code:
button.Click += OnClick;
Defensive patterns

Strategy: fallback

Validate before calling

// Detect routed-event attributes in source XAML before serialization:
bool hasRoutedEvents = xamlText.Contains("Click=") || Regex.IsMatch(xamlText, "\w+\.\w+\s*="); // heuristic; better: scan XAML event attributes via XamlReader

Try / catch

try { writer.WriteRoutedEvent(eventName, ownerType, handler); }
catch (NotSupportedException ex) when (ex.Message.Contains("event"))
{
    // fall back to XAML text output for this document
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/011b3f48b450b9c9. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlWriter.cs:901

            string     ownerTypeFullName,
            string     eventIdName,
            string     handlerName)
        {
#if EVENTSUPPORT            
            VerifyWriteState();

            XamlRoutedEventNode eventNode = new XamlRoutedEventNode(
                                                      0,
                                                      0,
                                                      _depth,
                                                      null,
                                                      assemblyName,
                                                      ownerTypeFullName,
                                                      eventIdName,
                                                      handlerName);
            _bamlRecordWriter.WriteRoutedEvent(eventNode);
#else
            throw new NotSupportedException(SR.Format(SR.ParserBamlEvent, eventIdName));
#endif

        }

        /// <summary>
        /// Write an event record to BAML.  
        /// </summary>
        /// <remarks>
        /// The Avalon parser does not process event records itself and will
        /// throw an exception if it encounters  an event record in BAML.  It
        /// is included here for completeness and future expandability.
        /// </remarks>
        public void WriteEvent(
            string    eventName,
            string    handlerName)
        {
#if EVENTSUPPORT            
            VerifyWriteState();

View on GitHub (pinned to 81131a70a4)