unoplatform/uno · error · Exception

x:Bind indexing expects a single literal argument

Error message

x:Bind indexing expects a single literal argument

What it means

Thrown by the Roslyn-based x:Bind expression visitor (VisitElementAccessExpression) when an indexer '[]' in an x:Bind expression does not have exactly one argument, or that argument is not a literal expression. x:Bind only supports indexing by a single string or integer literal.

Source

Thrown at src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/Utils/XBindExpressionParser.cs:322

				_builder.Append(')');
			}

			public override void VisitLiteralExpression(LiteralExpressionSyntax node)
			{
				_builder.Append(node.ToString());
			}

			public override void VisitElementAccessExpression(ElementAccessExpressionSyntax node)
			{
				if (Failed)
				{
					return;
				}

				if (node.ArgumentList.Arguments.Count != 1 ||
					node.ArgumentList.Arguments[0].Expression is not LiteralExpressionSyntax index)
				{
					throw new Exception("x:Bind indexing expects a single literal argument");
				}

				Visit(node.Expression);

				var lastType = _lastAccessed.Symbol switch
				{
					IPropertySymbol property => property.Type,
					IFieldSymbol field => field.Type,
					_ => throw new Exception("Only properties and fields can be indexed.")
				};

				var expectedIndexerParameterType = index is LiteralExpressionSyntax indexLiteral && indexLiteral.IsKind(SyntaxKind.StringLiteralExpression)
					? SpecialType.System_String
					: SpecialType.System_Int32;
				var indexer = lastType.GetMemberIncludingBaseTypes(expectedIndexerParameterType, (m, arg) => m is IPropertySymbol { IsIndexer: true } p && p.Parameters[0].Type.SpecialType == arg);
				if (indexer is not null)
				{
					_lastAccessed = (indexer, IsTopLevelContext: false);

View on GitHub (pinned to 0418340488)

Solutions

  1. Ensure the indexer has exactly one argument that is a literal string or integer.
  2. If you need a dynamic index, expose a computed property on your ViewModel/data context and bind to that instead.
  3. Remove any extra arguments from the indexer.

Example fix

<!-- before -->
<Text Text="{x:Bind Items[indexVariable]}" />
<Text Text="{x:Bind Items[1, 2]}" />
<!-- after -->
<Text Text="{x:Bind Items[0]}" />
<Text Text="{x:Bind CurrentItem}" />
Defensive patterns

Strategy: validation

Validate before calling

// Validate x:Bind indexer expressions before build:
//   - Must have exactly 1 argument
//   - The argument must be a literal (string or int)
// Linting regex for simple cases: \{x:Bind\s+\w+\[(\d+|"[^"]*")\]\}

Prevention

When it happens

Trigger: An x:Bind binding uses an indexer with zero arguments, more than one argument, or a non-literal argument (e.g. a variable, a method call, or a binding path as the index).

Common situations: Trying to index a collection with a dynamic/computed value in x:Bind; attempting multi-dimensional indexing; using a variable as the index instead of a hardcoded literal.

Related errors


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