dotnet/maui · error · InvalidOperationException

Call to SetBinding<{typeof(TSource)}, {typeof(TProperty)}> w

Error message

Call to SetBinding<{typeof(TSource)}, {typeof(TProperty)}> was not intercepted.

What it means

Thrown by the same typed SetBinding stub, but AFTER the feature switch check passed: AreBindingInterceptorsSupported is true, yet the call was still not rewritten by the source generator. It means the interceptor was enabled in config but never actually executed against this call site, so the stub ran unmodified. This is a build-pipeline defect, not a runtime config mistake.

Source

Thrown at src/Controls/src/Core/BindableObjectExtensions.cs:153

		/// <exception cref="ArgumentNullException"></exception>
		public static void SetBinding<TSource, TProperty>(
			this BindableObject self,
			BindableProperty targetProperty,
			Func<TSource, TProperty> getter,
			BindingMode mode = BindingMode.Default,
			IValueConverter? converter = null,
			object? converterParameter = null,
			string? stringFormat = null,
			object? source = null,
			object? fallbackValue = null,
			object? targetNullValue = null)
		{
			if (!RuntimeFeature.AreBindingInterceptorsSupported)
			{
				throw new InvalidOperationException($"Call to SetBinding<{typeof(TSource)}, {typeof(TProperty)}> could not be intercepted because the feature has been disabled. Consider removing the DisableMauiAnalyzers property from your project file or set the _MauiBindingInterceptorsSupport property to true instead.");
			}

			throw new InvalidOperationException($"Call to SetBinding<{typeof(TSource)}, {typeof(TProperty)}> was not intercepted.");
		}
#nullable disable

		public static T GetPropertyIfSet<T>(this BindableObject bindableObject, BindableProperty bindableProperty, T returnIfNotSet)
		{
			if (bindableObject == null)
				return returnIfNotSet;

			if (bindableObject.IsSet(bindableProperty))
				return (T)bindableObject.GetValue(bindableProperty);

			return returnIfNotSet;
		}

		internal static bool TrySetDynamicThemeColor(
			this BindableObject bindableObject,
			string resourceKey,
			BindableProperty bindableProperty,

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Rebuild cleanly (delete obj/bin) so the source generator runs against the current call site.
  2. Ensure the calling project is a MAUI app/library (uses Microsoft.Maui.Controls.Sdk) and not a generic library that lacks the generator reference.
  3. Simplify the getter to a single expression-bodied lambda with no captured locals, e.g. static vm => vm.Title.
  4. Verify Microsoft.Maui.Controls and the source-generator package are the same version.
  5. If the call site can't be intercepted, switch to the string-path Binding overload.

Example fix

// before (block-bodied / capturing lambda - not interceptable)
myLabel.SetBinding(Label.TextProperty, vm => { return vm.Title ?? "-"; });

// after (expression-bodied, interceptable)
myLabel.SetBinding(Label.TextProperty, static vm => vm.Title);
Defensive patterns

Strategy: validation

Validate before calling

// Before relying on the typed overload, confirm the generator ran by checking the
// compiled assembly for the generated interceptor method via reflection at test time.
// At the call site, keep lambdas expression-bodied and static:
label.SetBinding(Label.TextProperty, static vm => vm.Title);

Prevention

When it happens

Trigger: Using the lambda SetBinding/Create overload inside a project type the MAUI source generator does not target (e.g. a plain netstandard library, a unit test assembly, or a multi-targeting config where the generator assembly isn't loaded); the lambda expression shape is one the interceptor can't pattern-match (e.g. a block-bodied lambda or one capturing locals); an SDK mismatch where the generator package version differs from the runtime.

Common situations: Calling SetBinding with a lambda from a shared library that is not a MAUI app project; upgrading only part of the MAUI packages so the generator and runtime are out of sync; using block-bodied lambdas or method-group conversions instead of expression-bodied getters; incremental-build cache holding a stale generation.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/3c64bb7677970b52. Report an issue: GitHub.