{"record":{"id":"eb013a44444e0c0e","repo":"dotnet/wpf","slug":"sr-sharedattributeinloosexaml","errorCode":null,"errorMessage":"SR.SharedAttributeInLooseXaml","messagePattern":"SR\\.SharedAttributeInLooseXaml","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/WpfXamlLoader.cs","lineNumber":364,"sourceCode":"                if (onlyLoadOneNode)\n                {\n                    return;\n                }\n            }\n        }\n\n        private static void WriteValue(Xaml.XamlReader xamlReader, XamlObjectWriter xamlWriter, XamlContextStack<WpfXamlFrame> stack, IStyleConnector styleConnector)\n        {\n            if (stack.CurrentFrame.Property.IsDirective && stack.CurrentFrame.Property == XamlLanguage.Shared)\n            {\n                bool isShared;\n                if (bool.TryParse(xamlReader.Value as string, out isShared))\n                {\n                    if (!isShared)\n                    {\n                        if (!(xamlReader is Baml2006Reader))\n                        {\n                            throw new XamlParseException(SR.SharedAttributeInLooseXaml);\n                        }\n                    }\n                }\n            }\n            // ObjectWriter should not process PresentationOptions:Freeze directive nodes since it is unknown\n            if (stack.CurrentFrame.Property.IsDirective && stack.CurrentFrame.Property == XamlReaderHelper.Freeze)\n            {\n                bool freeze = Convert.ToBoolean(xamlReader.Value, TypeConverterHelper.InvariantEnglishUS);\n                stack.CurrentFrame.FreezeFreezable = freeze;\n                var bamlReader = xamlReader as System.Windows.Baml2006.Baml2006Reader;\n                bamlReader?.FreezeFreezables = freeze;\n            }\n            // The space directive node stream should not be written because it induces object instantiation,\n            // and the Baml2006Reader can produce space directives prematurely.\n            else if (stack.CurrentFrame.Property == XmlSpace.Value || stack.CurrentFrame.Property == XamlLanguage.Space)\n            {\n                if (typeof(DependencyObject).IsAssignableFrom(stack.CurrentFrame.Type.UnderlyingType))\n                {","sourceCodeStart":346,"sourceCodeEnd":382,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/WpfXamlLoader.cs#L346-L382","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Remove x:Shared=\"False\" from loose XAML loaded via XamlReader.Load","Compile the dictionary into the assembly (Build Action: Page / merged in App.xaml) so BAML supports x:Shared","Split: keep shared resources loose, move only x:Shared=\"False\" entries into a compiled dictionary"],"exampleFix":"// before (loose XAML via XamlReader.Load)\n<Button x:Shared=\"False\"/>\n// after (place in compiled ResourceDictionary or drop attribute)\n<Button/>","handlingStrategy":"try-catch","validationCode":"bool UsesSharedInLooseXaml(string xaml) =>\n    !isCompiledBaml && xaml.Contains(\"x:Shared\");","typeGuard":null,"tryCatchPattern":"try { var obj = XamlReader.Load(stream); }\ncatch (XamlParseException ex) when (ex.Message.Contains(\"Shared\")) { /* remove x:Shared or compile the dictionary */ }","preventionTips":["Never use x:Shared=\"False\" in XAML loaded at runtime via XamlReader.Load","Keep x:Shared=\"False\" entries only in compiled (Page build action) resource dictionaries","Split runtime-loaded dictionaries from compiled ones"],"tags":["wpf","xaml","resourcedictionary","loose-xaml"],"backgroundTag":"unsupported-operation","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"}