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
- Construct each selectable DataTemplate with the Type overload: new DataTemplate(typeof(MyViewCell)).
- If you need a Func-based template, change the ListView CachingStrategy to RecycleElement (not the AndDataTemplate variant).
- 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
- With RecycleElementAndDataTemplate always use new DataTemplate(typeof(T)).
- Prefer the Type overload unless you need a factory.
- If you must use a Func template, switch CachingStrategy to RecycleElement.
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
- DataTemplateSelector.OnSelectTemplate must not return anothe
- ItemTemplate count has exceeded the limit of {ViewTypeCount}
- ItemTemplate count has exceeded the limit of {ViewTypeCount}
- View for cell must implement {nameof(INativeElementView)} to
- ItemTemplate count has exceeded the limit of {ViewTypeCount}
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/06b0598829b881d1.
Report an issue: GitHub.