dotnet/wpf · error · InvalidOperationException

SR.ParserBamlEvent

Error message

SR.ParserBamlEvent: {0}

What it means

BamlReader.ReadRoutedEventRecord unconditionally throws InvalidOperationException with SR.ParserBamlEvent. In the WPF runtime BAML reader, routed-event records are expected to be compiled away by the markup compiler (hooked up in generated code), so a RoutedEvent record in a BAML stream is an error condition at parse time.

Solutions

  1. Regenerate the BAML with the standard WPF markup compiler (build the project normally) so event records are handled by generated code.
  2. Do not rely on the runtime BamlReader for streams containing event hookups; use XamlReader/load-path APIs instead.
  3. Remove or replace event attribute hookups with code-behind wiring if generating BAML with custom tooling.

Example fix

// before: XAML with event attribute compiled by non-standard tool
<Button Click="OnClick" />

// after: wire in code-behind
<Button x:Name="btn" />
// in constructor:
btn.Click += OnClick;
Defensive patterns

Strategy: validation

Validate before calling

// reject streams containing routed-event records before reading
// (walk records with the map table, or simply rebuild with the standard compiler)
if (bamlRecords.Any(r => r.RecordType == BamlRecordType.RoutedEvent))
    throw new NotSupportedException("BAML contains routed-event records; rebuild with the WPF markup compiler.");

Type guard

bool HasEventRecords(IEnumerable<BamlRecord> recs) => recs.Any(r => r.RecordType == BamlRecordType.RoutedEvent || r.RecordType == BamlRecordType.ClrEvent);

Try / catch

try { reader.Read(); }
catch (InvalidOperationException) {
    log.LogError("BAML stream contains event records; use the standard markup compiler or XamlReader.Load instead.");
}

Prevention

When it happens

Trigger: Parsing a BAML stream that contains a BamlRoutedEventRecord — reached via ReadRoutedEventRecord during node iteration of a markup-extension-style read.

Common situations: Manually produced or tool-generated BAML that still carries event hookup records; using internal BAML reading APIs on streams not produced by the standard WPF markup compiler.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlReader.cs:1793

            }
            else
            {
                return builder.ToString();
            }
        }

        /***************************************************************************\
        *
        * BamlReader.ReadRoutedEventRecord
        *
        * Read a routed event record.  These are currently not stored in the
        * BAML stream, but are handled by code generated by the compiler.
        *
        \***************************************************************************/

        private void ReadRoutedEventRecord()
        {
            throw new InvalidOperationException(SR.Format(SR.ParserBamlEvent, string.Empty));
        }

        /***************************************************************************\
        *
        * BamlReader.ReadClrEventRecord
        *
        * Read a clr event record.  These are currently not stored in the
        * BAML stream, but are handled by code generated by the compiler.
        *
        \***************************************************************************/

        private void ReadClrEventRecord()
        {
            throw new InvalidOperationException(SR.Format(SR.ParserBamlEvent, string.Empty));
        }

        /***************************************************************************\
        *

View on GitHub (pinned to 81131a70a4)