{"record":{"id":"06b0598829b881d1","repo":"dotnet/maui","slug":"recycleelementanddatatemplate-requires-datatemplat","errorCode":null,"errorMessage":"RecycleElementAndDataTemplate requires DataTemplate activated with ctor taking a type.","messagePattern":"RecycleElementAndDataTemplate requires DataTemplate activated with ctor taking a type\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/DataTemplateSelector.cs","lineNumber":40,"sourceCode":"#pragma warning restore CS0618 // Type or member is obsolete\n\n\t\t\tvar recycle = listView == null ? false :\n\t\t\t\t(listView.CachingStrategy & ListViewCachingStrategy.RecycleElementAndDataTemplate) ==\n\t\t\t\t\tListViewCachingStrategy.RecycleElementAndDataTemplate;\n\n\t\t\tDataTemplate dataTemplate = null;\n\t\t\tif (recycle && _dataTemplates.TryGetValue(item.GetType(), out dataTemplate))\n\t\t\t\treturn dataTemplate;\n\n\t\t\tdataTemplate = OnSelectTemplate(item, container);\n\t\t\tif (dataTemplate is DataTemplateSelector)\n\t\t\t\tthrow new NotSupportedException(\n\t\t\t\t\t\"DataTemplateSelector.OnSelectTemplate must not return another DataTemplateSelector\");\n\n\t\t\tif (recycle)\n\t\t\t{\n\t\t\t\tif (!dataTemplate.CanRecycle)\n\t\t\t\t\tthrow new NotSupportedException(\n\t\t\t\t\t\t\"RecycleElementAndDataTemplate requires DataTemplate activated with ctor taking a type.\");\n\n\t\t\t\t_dataTemplates[item.GetType()] = dataTemplate;\n\t\t\t}\n\n\t\t\treturn dataTemplate;\n\t\t}\n\n\t\tprotected abstract DataTemplate OnSelectTemplate(object item, BindableObject container);\n\t}\n}","sourceCodeStart":22,"sourceCodeEnd":51,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/DataTemplateSelector.cs#L22-L51","documentation":"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.","triggerScenarios":"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)).","commonSituations":"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.","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."],"exampleFix":"// before\nnew DataTemplate(() => new MyViewCell()) // Func ctor, CanRecycle == false -> throws\n\n// after\nnew DataTemplate(typeof(MyViewCell)) // Type ctor, CanRecycle == true","handlingStrategy":"validation","validationCode":"// Verify recyclability up front when using RecycleElementAndDataTemplate.\nforeach (var kv in selectorFieldPerType)\n    if (!kv.Value.CanRecycle)\n        throw new InvalidOperationException($\"DataTemplate for {kv.Key} is not Type-constructed; cannot use RecycleElementAndDataTemplate.\");","typeGuard":"static bool IsRecyclable(DataTemplate t) => t.CanRecycle; // true only for the Type ctor","tryCatchPattern":null,"preventionTips":["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."],"tags":["listview","datatemplate","caching","recycle","notsupported"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}