unoplatform/uno · error · ArgumentException

Binding to XamlSetMarkupExtensionHandler failed

Error message

Binding to XamlSetMarkupExtensionHandler failed

What it means

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.

Source

Thrown at src/SourceGenerators/System.Xaml/System.Xaml/XamlType.cs:738

			var methods = (from m in UnderlyingType.GetConstructors (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) where m.GetParameters ().Length == parameterCount select m).ToArray ();
			if (methods.Length == 1)
				return (from p in methods [0].GetParameters () select SchemaContext.GetXamlType (p.ParameterType)).ToArray ();

			if (SchemaContext.SupportMarkupExtensionsWithDuplicateArity)
				throw new NotSupportedException ("The default LookupPositionalParameters implementation does not allow duplicate arity of markup extensions");
			return null;
		}

		BindingFlags flags_get_static = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;

		protected virtual EventHandler<XamlSetMarkupExtensionEventArgs> LookupSetMarkupExtensionHandler ()
		{
			var a = this.GetCustomAttribute<XamlSetMarkupExtensionAttribute> ();
			if (a == null)
				return null;
			var mi = type.GetMethod (a.XamlSetMarkupExtensionHandler, flags_get_static);
			if (mi == null)
				throw new ArgumentException ("Binding to XamlSetMarkupExtensionHandler failed");
			return (EventHandler<XamlSetMarkupExtensionEventArgs>) Delegate.CreateDelegate (typeof (EventHandler<XamlSetMarkupExtensionEventArgs>), mi);
		}

		protected virtual EventHandler<XamlSetTypeConverterEventArgs> LookupSetTypeConverterHandler ()
		{
			var a = this.GetCustomAttribute<XamlSetTypeConverterAttribute> ();
			if (a == null)
				return null;
			var mi = type.GetMethod (a.XamlSetTypeConverterHandler, flags_get_static);
			if (mi == null)
				throw new ArgumentException ("Binding to XamlSetTypeConverterHandler failed");
			return (EventHandler<XamlSetTypeConverterEventArgs>) Delegate.CreateDelegate (typeof (EventHandler<XamlSetTypeConverterEventArgs>), mi);
		}

		protected virtual bool LookupTrimSurroundingWhitespace ()
		{
			// Uno specific: We are not adding TrimSurroundingWhitespaceAttribute to LineBreak, so we special-case it here.
			if (PreferredXamlNamespace.Equals("http://schemas.microsoft.com/winfx/2006/xaml/presentation", StringComparison.Ordinal) && Name == "LineBreak")

View on GitHub (pinned to 0418340488)

Solutions

  1. Verify the method named in [XamlSetMarkupExtension] exists on the type with the exact spelling and is declared 'static'.
  2. Ensure the handler signature is static void M(object sender, XamlSetMarkupExtensionEventArgs e).
  3. If you intentionally removed the handler, remove the attribute as well.
  4. Keep the method public or internal (NonPublic is allowed by the flags) but it MUST be static.

Example fix

// before
[XamlSetMarkupExtension("HandleMarkup")]
public class MyType {
    // wrong: instance method, GetMethod with Static flag returns null
    void HandleMarkup(object s, XamlSetMarkupExtensionEventArgs e) {}
}

// after
[XamlSetMarkupExtension(nameof(OnSetMarkupExtension))]
public class MyType {
    public static void OnSetMarkupExtension(object s, XamlSetMarkupExtensionEventArgs e) {}
}
Defensive patterns

Strategy: validation

Validate before calling

var attr = typeof(T).GetCustomAttribute<XamlSetMarkupExtensionAttribute>();
if (attr != null) {
    var mi = typeof(T).GetMethod(attr.XamlSetMarkupExtensionHandler,
        BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
    if (mi == null) throw new InvalidOperationException(
        $"Handler '{attr.XamlSetMarkupExtensionHandler}' not found/static on {typeof(T)}");
}

Prevention

When it happens

Trigger: 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.

Common situations: 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>.

Related errors


AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13). Data as JSON: /api/errors/ee67fc8c0319a453. Report an issue: GitHub.