dotnet/wpf · error

SR.SharedAttributeInLooseXaml

Error message

SR.SharedAttributeInLooseXaml

What it means

WpfXamlLoader.WriteValue encounters the x:Shared attribute (PresentationOptions/x:Shared) whose value parses as a bool. The x:Shared='False' directive is only valid in compiled BAML/resource dictionaries; in loose (runtime-parsed) XAML it cannot be honored, so a XamlParseException is thrown.

Solutions

  1. Remove x:Shared="False" from loose XAML loaded via XamlReader.Load
  2. Compile the dictionary into the assembly (Build Action: Page / merged in App.xaml) so BAML supports x:Shared
  3. Split: keep shared resources loose, move only x:Shared="False" entries into a compiled dictionary

Example fix

// before (loose XAML via XamlReader.Load)
<Button x:Shared="False"/>
// after (place in compiled ResourceDictionary or drop attribute)
<Button/>
Defensive patterns

Strategy: try-catch

Validate before calling

bool UsesSharedInLooseXaml(string xaml) =>
    !isCompiledBaml && xaml.Contains("x:Shared");

Try / catch

try { var obj = XamlReader.Load(stream); }
catch (XamlParseException ex) when (ex.Message.Contains("Shared")) { /* remove x:Shared or compile the dictionary */ }

Prevention

When it happens

Trigger: XamlReader.Load (loose XAML) on a ResourceDictionary entry declared with x:Shared="False"; the same XAML compiles fine when placed in a compiled resource dictionary (BAML).

Common situations: Moving a ResourceDictionary from App.xaml (compiled) to a runtime-loaded loose .xaml file (XamlReader.Load) while keeping x:Shared="False"; loading dictionaries dynamically that rely on instance-per-lookup semantics.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/WpfXamlLoader.cs:364

                if (onlyLoadOneNode)
                {
                    return;
                }
            }
        }

        private static void WriteValue(Xaml.XamlReader xamlReader, XamlObjectWriter xamlWriter, XamlContextStack<WpfXamlFrame> stack, IStyleConnector styleConnector)
        {
            if (stack.CurrentFrame.Property.IsDirective && stack.CurrentFrame.Property == XamlLanguage.Shared)
            {
                bool isShared;
                if (bool.TryParse(xamlReader.Value as string, out isShared))
                {
                    if (!isShared)
                    {
                        if (!(xamlReader is Baml2006Reader))
                        {
                            throw new XamlParseException(SR.SharedAttributeInLooseXaml);
                        }
                    }
                }
            }
            // ObjectWriter should not process PresentationOptions:Freeze directive nodes since it is unknown
            if (stack.CurrentFrame.Property.IsDirective && stack.CurrentFrame.Property == XamlReaderHelper.Freeze)
            {
                bool freeze = Convert.ToBoolean(xamlReader.Value, TypeConverterHelper.InvariantEnglishUS);
                stack.CurrentFrame.FreezeFreezable = freeze;
                var bamlReader = xamlReader as System.Windows.Baml2006.Baml2006Reader;
                bamlReader?.FreezeFreezables = freeze;
            }
            // The space directive node stream should not be written because it induces object instantiation,
            // and the Baml2006Reader can produce space directives prematurely.
            else if (stack.CurrentFrame.Property == XmlSpace.Value || stack.CurrentFrame.Property == XamlLanguage.Space)
            {
                if (typeof(DependencyObject).IsAssignableFrom(stack.CurrentFrame.Type.UnderlyingType))
                {

View on GitHub (pinned to 81131a70a4)