unoplatform/uno · error · Exception

Only properties and fields can be indexed.

Error message

Only properties and fields can be indexed.

What it means

Thrown by the Roslyn-based x:Bind expression visitor when the expression immediately before an indexer '[]' resolves to a symbol that is neither an IPropertySymbol nor an IFieldSymbol. The switch expression has a default case that throws, covering method calls, events, types, and other symbol kinds.

Source

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

			{
				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);
				}
				else
				{
					throw new Exception("Type is unsupported for indexing.");
				}

				_builder.Append('[');

				VisitLiteralExpression(index);

View on GitHub (pinned to 0418340488)

Solutions

  1. Bind to a property or field instead of a method — expose a property that holds the collection.
  2. If you need the method result, create a computed property wrapping the method call and bind to that.
  3. Move the indexing logic into a ViewModel property.

Example fix

<!-- before -->
<Text Text="{x:Bind GetItems()[0]}" />
<!-- after -->
<!-- Add a property: -->
<!-- public string FirstItem => GetItems()[0]; -->
<Text Text="{x:Bind FirstItem}" />
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the expression before '[]' in x:Bind is a property or field, not a method.
// Review the binding: {x:Bind MyProperty[0]} is valid; {x:Bind MyMethod()[0]} is not.

Prevention

When it happens

Trigger: Indexing the result of a method call in x:Bind, e.g. {x:Bind GetItems()[0]}, where GetItems() is a method symbol rather than a property or field.

Common situations: Attempting to call a method and immediately index its return value in x:Bind; indexing a type or event member; the parser visited a member that does not have a property/field backing.

Related errors


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