dotnet/wpf · error · XamlObjectWriterException

exceptionMessage.ToString()

Error message

exceptionMessage.ToString()

What it means

NameFixupGraph.ThrowProvideValueCycle throws XamlObjectWriterException when the XAML object graph contains a cycle in ProvideValue/deferred-content dependencies that prevents name fixup from completing. The exception message is built by appending each remaining object's dependency name until the cycle is described.

Solutions

  1. Break the cycle in the XAML: restructure resources/templates so no object references (directly or via ProvideValue) back to itself or its ancestors.
  2. Replace one side of the cycle with a code-behind assignment done after load completes.
  3. Read the exception message's remaining-dependency list to identify the exact objects forming the cycle and remove one link.
  4. Convert one deferred reference (e.g. DynamicResource cycle) into an eagerly-resolved value.

Example fix

<!-- before: A references B, B references A via markup extension cycles -->
<Style x:Key="A" BasedOn="{StaticResource B}" ... />
<Style x:Key="B" BasedOn="{StaticResource A}" ... />
<!-- after: break the cycle with a common base -->
<Style x:Key="Base" TargetType="Button" />
<Style x:Key="A" BasedOn="{StaticResource Base}" ... />
<Style x:Key="B" BasedOn="{StaticResource Base}" ... />
Defensive patterns

Strategy: validation

Validate before calling

// Static sanity check: ensure no resource key is referenced (directly or transitively) by a definition with the same key
foreach (var key in resourceKeys)
    if (DependsOn(resourceDict[key], key)) throw new InvalidOperationException($"Cycle detected at resource '{key}'");

Try / catch

try { LoadXaml(xamlReader); }
catch (XamlObjectWriterException ex) when (ex.Message.Contains("cycle") || ex.Message.Contains("ProvideValue"))
{ /* surface the listed dependencies, ask user to break the cycle */ }

Prevention

When it happens

Trigger: Loading XAML (XamlObjectWriter / Baml) whose object graph has circular references through markup extensions or deferred ProvideValue sites, e.g. resource dictionaries or templates referencing each other so name fixup never terminates.

Common situations: Cyclic StaticResource/Template references between resources; two XAML objects whose markup extensions reference one another; complex deferred content in App.xaml or merged dictionaries.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/NameFixupGraph.cs:515

                string meName = token.ReferencedObject.ToString();
                if (token.LineNumber != 0)
                {
                    if (token.LinePosition != 0)
                    {
                        exceptionMessage.Append(SR.Format(SR.LineNumberAndPosition, meName, token.LineNumber, token.LinePosition));
                    }
                    else
                    {
                        exceptionMessage.Append(SR.Format(SR.LineNumberOnly, meName, token.LineNumber));
                    }
                }
                else
                {
                    exceptionMessage.Append(meName);
                }
            }

            throw new XamlObjectWriterException(exceptionMessage.ToString());
        }
    }
}

View on GitHub (pinned to 81131a70a4)