{"record":{"id":"ee67fc8c0319a453","repo":"unoplatform/uno","slug":"binding-to-xamlsetmarkupextensionhandler-failed","errorCode":null,"errorMessage":"Binding to XamlSetMarkupExtensionHandler failed","messagePattern":"Binding to XamlSetMarkupExtensionHandler failed","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/SourceGenerators/System.Xaml/System.Xaml/XamlType.cs","lineNumber":738,"sourceCode":"\t\t\tvar methods = (from m in UnderlyingType.GetConstructors (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) where m.GetParameters ().Length == parameterCount select m).ToArray ();\n\t\t\tif (methods.Length == 1)\n\t\t\t\treturn (from p in methods [0].GetParameters () select SchemaContext.GetXamlType (p.ParameterType)).ToArray ();\n\n\t\t\tif (SchemaContext.SupportMarkupExtensionsWithDuplicateArity)\n\t\t\t\tthrow new NotSupportedException (\"The default LookupPositionalParameters implementation does not allow duplicate arity of markup extensions\");\n\t\t\treturn null;\n\t\t}\n\n\t\tBindingFlags flags_get_static = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;\n\n\t\tprotected virtual EventHandler<XamlSetMarkupExtensionEventArgs> LookupSetMarkupExtensionHandler ()\n\t\t{\n\t\t\tvar a = this.GetCustomAttribute<XamlSetMarkupExtensionAttribute> ();\n\t\t\tif (a == null)\n\t\t\t\treturn null;\n\t\t\tvar mi = type.GetMethod (a.XamlSetMarkupExtensionHandler, flags_get_static);\n\t\t\tif (mi == null)\n\t\t\t\tthrow new ArgumentException (\"Binding to XamlSetMarkupExtensionHandler failed\");\n\t\t\treturn (EventHandler<XamlSetMarkupExtensionEventArgs>) Delegate.CreateDelegate (typeof (EventHandler<XamlSetMarkupExtensionEventArgs>), mi);\n\t\t}\n\n\t\tprotected virtual EventHandler<XamlSetTypeConverterEventArgs> LookupSetTypeConverterHandler ()\n\t\t{\n\t\t\tvar a = this.GetCustomAttribute<XamlSetTypeConverterAttribute> ();\n\t\t\tif (a == null)\n\t\t\t\treturn null;\n\t\t\tvar mi = type.GetMethod (a.XamlSetTypeConverterHandler, flags_get_static);\n\t\t\tif (mi == null)\n\t\t\t\tthrow new ArgumentException (\"Binding to XamlSetTypeConverterHandler failed\");\n\t\t\treturn (EventHandler<XamlSetTypeConverterEventArgs>) Delegate.CreateDelegate (typeof (EventHandler<XamlSetTypeConverterEventArgs>), mi);\n\t\t}\n\n\t\tprotected virtual bool LookupTrimSurroundingWhitespace ()\n\t\t{\n\t\t\t// Uno specific: We are not adding TrimSurroundingWhitespaceAttribute to LineBreak, so we special-case it here.\n\t\t\tif (PreferredXamlNamespace.Equals(\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\", StringComparison.Ordinal) && Name == \"LineBreak\")","sourceCodeStart":720,"sourceCodeEnd":756,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SourceGenerators/System.Xaml/System.Xaml/XamlType.cs#L720-L756","documentation":"LookupSetMarkupExtensionHandler throws ArgumentException when a type is decorated with [XamlSetMarkupExtensionAttribute(Name)] but the named handler method cannot be found via reflection (type.GetMethod returns null). The attribute promises a static method to receive markup-extension value assignment; if the method name is wrong, was removed, or is not static/public-matching the binding flags, binding fails.","triggerScenarios":"Applying [XamlSetMarkupExtension(\"OnSetMarkupExtension\")] to a type where OnSetMarkupExtension does not exist, is misspelled, is an instance method, or is private with the wrong signature. The flags used are Public|NonPublic|Static, so non-static methods are the usual culprit.","commonSituations":"Renaming the handler method without updating the attribute; refactoring that turned a static handler into an instance method; copy-paste of an attribute from another type whose handler name differs; signature not matching EventHandler<XamlSetMarkupExtensionEventArgs>.","solutions":["Verify the method named in [XamlSetMarkupExtension] exists on the type with the exact spelling and is declared 'static'.","Ensure the handler signature is static void M(object sender, XamlSetMarkupExtensionEventArgs e).","If you intentionally removed the handler, remove the attribute as well.","Keep the method public or internal (NonPublic is allowed by the flags) but it MUST be static."],"exampleFix":"// before\n[XamlSetMarkupExtension(\"HandleMarkup\")]\npublic class MyType {\n    // wrong: instance method, GetMethod with Static flag returns null\n    void HandleMarkup(object s, XamlSetMarkupExtensionEventArgs e) {}\n}\n\n// after\n[XamlSetMarkupExtension(nameof(OnSetMarkupExtension))]\npublic class MyType {\n    public static void OnSetMarkupExtension(object s, XamlSetMarkupExtensionEventArgs e) {}\n}","handlingStrategy":"validation","validationCode":"var attr = typeof(T).GetCustomAttribute<XamlSetMarkupExtensionAttribute>();\nif (attr != null) {\n    var mi = typeof(T).GetMethod(attr.XamlSetMarkupExtensionHandler,\n        BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);\n    if (mi == null) throw new InvalidOperationException(\n        $\"Handler '{attr.XamlSetMarkupExtensionHandler}' not found/static on {typeof(T)}\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use nameof(HandlerMethod) in the attribute so renames stay in sync.","Keep handlers static with the canonical EventHandler<XamlSetMarkupExtensionEventArgs> signature.","Add a reflection-based unit test that verifies every [XamlSetMarkupExtension] attribute resolves to a static method."],"tags":["xaml","markup-extension","reflection","attribute-binding"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}