{"record":{"id":"7b8a22b2a45595f7","repo":"dotnet/maui","slug":"itemtemplate-count-has-exceeded-the-limit-of-view-7b8a22","errorCode":null,"errorMessage":"ItemTemplate count has exceeded the limit of {ViewTypeCount}\nPlease make sure to reuse DataTemplate objects","messagePattern":"ItemTemplate count has exceeded the limit of (.+?)\nPlease make sure to reuse DataTemplate objects","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/Compatibility/Handlers/ListView/Android/ListViewAdapter.cs","lineNumber":219,"sourceCode":"\t\t\t\t}\n\n\t\t\t\titemTemplate = selector.SelectTemplate(item, _listView);\n\t\t\t}\n\n\t\t\t// check again to guard against DataTemplateSelectors that return null\n\t\t\tif (itemTemplate == null)\n\t\t\t\treturn DefaultItemTemplateId;\n\n\t\t\tif (!_templateToId.TryGetValue(itemTemplate, out int key))\n\t\t\t{\n\t\t\t\t_dataTemplateIncrementer++;\n\t\t\t\tkey = _dataTemplateIncrementer;\n\t\t\t\t_templateToId[itemTemplate] = key;\n\t\t\t}\n\n\t\t\tif (key >= ViewTypeCount)\n\t\t\t{\n\t\t\t\tthrow new Exception($\"ItemTemplate count has exceeded the limit of {ViewTypeCount}\" + Environment.NewLine +\n\t\t\t\t\t\t\t\t\t \"Please make sure to reuse DataTemplate objects\");\n\t\t\t}\n\n\t\t\treturn key;\n\t\t}\n\n\t\t// This is used by the `ListViewRenderer.GetDesiredSize` call to retrieve an already created ViewCellContainer\n\t\t// This helps us fake cell reuse so we aren't creating extra views during the measure pass.\n\t\tinternal ConditionalFocusLayout GetConvertViewForMeasuringInfiniteHeight(int position)\n\t\t{\n\t\t\tif (_layoutsCreated.TryGetValue(position, out ConditionalFocusLayout foundValue))\n\t\t\t\treturn foundValue;\n\n\t\t\treturn null;\n\t\t}\n\n\t\tpublic override AView GetView(int position, AView convertView, ViewGroup parent)\n\t\t{","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/Compatibility/Handlers/ListView/Android/ListViewAdapter.cs#L201-L237","documentation":"Thrown by the Android ListViewAdapter when the number of unique DataTemplate objects registered via GetItemTemplateId reaches ViewTypeCount. Android's adapter model allocates a fixed number of view types per ListView; creating a new DataTemplate per item instead of reusing a small set exhausts this budget.","triggerScenarios":"Returning a new DataTemplate(() => new View()) for every item in HasListView.ItemTemplate (a factory that allocates per call). Using ItemTemplate = new DataTemplate(() => BuildCell(item)) inside a data-selectors that creates templates on the fly. Large lists with per-item distinct templates.","commonSituations":"Building DataTemplate inside GetCell or item-source enumeration rather than once. Using a DataTemplateSelector that returns brand-new DataTemplate instances instead of cached static ones. Misunderstanding that ItemTemplate should be a single shared template/selectors.","solutions":["Create the DataTemplate once and reuse it across all items (assign a single instance to ItemTemplate).","In a DataTemplateSelector, return cached static DataTemplate fields, never allocate in SelectTemplate.","Use a single template with bindings/triggers for per-item variation instead of N distinct templates."],"exampleFix":"// before — allocates per item\nlistView.ItemTemplate = new DataTemplate(() => new ViewCell { View = BuildView(item) });\n\n// after — single reusable template\nvar template = new DataTemplate(() =>\n{\n    var cell = new ViewCell();\n    cell.View = new Label();\n    cell.View.SetBinding(Label.TextProperty, \"Name\");\n    return cell;\n});\nlistView.ItemTemplate = template;","handlingStrategy":"validation","validationCode":"// Allocate the DataTemplate once, reuse for all items\nDataTemplate _shared = new DataTemplate(() =>\n{\n    var cell = new ViewCell { View = new Label() };\n    cell.View.SetBinding(Label.TextProperty, \"Name\");\n    return cell;\n});\nlistView.ItemTemplate = _shared;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never allocate DataTemplate per item; create once and reuse.","Cache static DataTemplate fields in a DataTemplateSelector.","Use a single template with bindings for per-item variation."],"tags":["maui","listview","android","datatemplate","performance","memory"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}