dotnet/maui · error · ArgumentException

Unhandled expression type: '{0}'

Error message

Unhandled expression type: '{0}'

What it means

WindowsExpressionSearch.Visit throws ArgumentException for expression tree node types not handled in its switch statement. The visitor handles unary, binary, member access, lambda, call, invoke, member init, list init, and constant types, but throws for any other ExpressionType (e.g. Conditional, Coalesce, Switch, Try, Block, NewArray, Index, etc.).

Source

Thrown at src/Compatibility/Core/src/Windows/WindowsExpressionSearch.cs:119

				case ExpressionType.Invoke:
					var invocation = (InvocationExpression)expression;
					VisitList(invocation.Arguments, Visit);
					Visit(invocation.Expression);
					break;
				case ExpressionType.MemberInit:
					var init = (MemberInitExpression)expression;
					VisitList(init.NewExpression.Arguments, Visit);
					VisitList(init.Bindings, VisitBinding);
					break;
				case ExpressionType.ListInit:
					var init1 = (ListInitExpression)expression;
					VisitList(init1.NewExpression.Arguments, Visit);
					VisitList(init1.Initializers, initializer => VisitList(initializer.Arguments, Visit));
					break;
				case ExpressionType.Constant:
					break;
				default:
					throw new ArgumentException(string.Format("Unhandled expression type: '{0}'", expression.NodeType));
			}
		}

		void VisitBinding(MemberBinding binding)
		{
			switch (binding.BindingType)
			{
				case MemberBindingType.Assignment:
					Visit(((MemberAssignment)binding).Expression);
					break;
				case MemberBindingType.MemberBinding:
					VisitList(((MemberMemberBinding)binding).Bindings, VisitBinding);
					break;
				case MemberBindingType.ListBinding:
					VisitList(((MemberListBinding)binding).Initializers, initializer => VisitList(initializer.Arguments, Visit));
					break;
				default:
					throw new ArgumentException(string.Format("Unhandled binding type '{0}'", binding.BindingType));

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Simplify binding expressions to use only property access, method calls, and arithmetic operations
  2. Replace conditional expressions with value converters
  3. Pre-compute values in the ViewModel instead of using complex expressions in bindings

Example fix

// before — conditional expression triggers unhandled ExpressionType.Conditional
SetBinding(MyProperty, new Binding(() => flag ? value1 : value2));

// after — use a value converter or pre-computed property
SetBinding(MyProperty, new Binding(nameof(ViewModel.ComputedValue)));
Defensive patterns

Strategy: try-catch

Validate before calling

// Before using complex expressions in bindings, check the node type
static bool IsSupportedExpression(Expression expr)
{
    var supported = new[]
    {
        ExpressionType.MemberAccess, ExpressionType.Constant, ExpressionType.Call,
        ExpressionType.Invoke, ExpressionType.Lambda, ExpressionType.Convert,
        ExpressionType.Add, ExpressionType.Subtract, ExpressionType.Multiply,
        ExpressionType.Divide, ExpressionType.Parameter
    };
    return supported.Contains(expr.NodeType);
}

Try / catch

try
{
    SetBinding(MyViewProperty, new Binding(() => complexExpression));
}
catch (ArgumentException ex) when (ex.Message.Contains("Unhandled expression type"))
{
    // Fall back to a simpler binding or pre-computed value
    SetBinding(MyViewProperty, new Binding(nameof(ViewModel.SimpleValue)));
}

Prevention

When it happens

Trigger: A binding expression or lambda passed through the expression search contains a node type the visitor does not handle — e.g. a ternary conditional expression (a ? b : c), null-coalescing (a ?? b), array creation, or dynamic expressions in a data binding path.

Common situations: Complex multi-binding with conditional logic in XAML; advanced lambda expressions in binding definitions; newer C# expression patterns not anticipated by the visitor.

Related errors


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