dotnet/maui · error · NotSupportedException

RecycleElementAndDataTemplate requires DataTemplate activate

Error message

RecycleElementAndDataTemplate requires DataTemplate activated with ctor taking a type.

What it means

When a ListView uses CachingStrategy RecycleElementAndDataTemplate, SelectTemplate caches the chosen DataTemplate per item Type and requires it to be recyclable. CanRecycle is only true for DataTemplates constructed via the constructor that takes a Type (ElementTemplate sets _canRecycle=true only there). A DataTemplate built from a Func or the parameterless ctor cannot be recycled, so the framework throws NotSupportedException.

Source

Thrown at src/Controls/src/Core/DataTemplateSelector.cs:40

#pragma warning restore CS0618 // Type or member is obsolete

			var recycle = listView == null ? false :
				(listView.CachingStrategy & ListViewCachingStrategy.RecycleElementAndDataTemplate) ==
					ListViewCachingStrategy.RecycleElementAndDataTemplate;

			DataTemplate dataTemplate = null;
			if (recycle && _dataTemplates.TryGetValue(item.GetType(), out dataTemplate))
				return dataTemplate;

			dataTemplate = OnSelectTemplate(item, container);
			if (dataTemplate is DataTemplateSelector)
				throw new NotSupportedException(
					"DataTemplateSelector.OnSelectTemplate must not return another DataTemplateSelector");

			if (recycle)
			{
				if (!dataTemplate.CanRecycle)
					throw new NotSupportedException(
						"RecycleElementAndDataTemplate requires DataTemplate activated with ctor taking a type.");

				_dataTemplates[item.GetType()] = dataTemplate;
			}

			return dataTemplate;
		}

		protected abstract DataTemplate OnSelectTemplate(object item, BindableObject container);
	}
}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Construct each selectable DataTemplate with the Type overload: new DataTemplate(typeof(MyViewCell)).
  2. If you need a Func-based template, change the ListView CachingStrategy to RecycleElement (not the AndDataTemplate variant).
  3. Ensure the type passed has a public parameterless constructor so Activator.CreateInstance works.

Example fix

// before
new DataTemplate(() => new MyViewCell()) // Func ctor, CanRecycle == false -> throws

// after
new DataTemplate(typeof(MyViewCell)) // Type ctor, CanRecycle == true
Defensive patterns

Strategy: validation

Validate before calling

// Verify recyclability up front when using RecycleElementAndDataTemplate.
foreach (var kv in selectorFieldPerType)
    if (!kv.Value.CanRecycle)
        throw new InvalidOperationException($"DataTemplate for {kv.Key} is not Type-constructed; cannot use RecycleElementAndDataTemplate.");

Type guard

static bool IsRecyclable(DataTemplate t) => t.CanRecycle; // true only for the Type ctor

Prevention

When it happens

Trigger: ListView with CachingStrategy=RecycleElementAndDataTemplate whose DataTemplateSelector.OnSelectTemplate returns a DataTemplate created via new DataTemplate(() => new MyCell()) (Func ctor) or new DataTemplate() rather than new DataTemplate(typeof(MyCell)).

Common situations: Migrating a ListView to the RecycleElementAndDataTemplate strategy; using a factory/Func-based DataTemplate that worked under RecycleElement; XAML DataTemplate that does not resolve to a Type-backed template.

Related errors


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