{"record":{"id":"2e18ae3d97d5ef9b","repo":"dotnet/maui","slug":"visible-item-not-found","errorCode":null,"errorMessage":"Visible item not found","messagePattern":"Visible item not found","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/Windows/CollectionView/CarouselViewRenderer.cs","lineNumber":414,"sourceCode":"\t\t\tif (!IsValidPosition(position))\n\t\t\t\treturn;\n\n\t\t\tvar currentPosition = CarouselView.Position;\n\n\t\t\tif (currentPosition != position)\n\t\t\t{\n\t\t\t\tCarouselView.SetValueFromRenderer(CarouselView.PositionProperty, position);\n\t\t\t}\n\t\t}\n\n\t\t[PortHandler]\n\t\tvoid SetCarouselViewCurrentItem(int carouselPosition)\n\t\t{\n\t\t\tif (!IsValidPosition(carouselPosition))\n\t\t\t\treturn;\n\n\t\t\tif (!(GetItem(carouselPosition) is ItemTemplateContext itemTemplateContext))\n\t\t\t\tthrow new InvalidOperationException(\"Visible item not found\");\n\n\t\t\tvar item = itemTemplateContext.Item;\n\t\t\tCarouselView.SetValueFromRenderer(CarouselView.CurrentItemProperty, item);\n\t\t}\n\n\t\t[PortHandler]\n\t\tbool IsValidPosition(int position)\n\t\t{\n\t\t\tif (ItemCount == 0)\n\t\t\t\treturn false;\n\n\t\t\tif (position < 0 || position >= ItemCount)\n\t\t\t\treturn false;\n\n\t\t\treturn true;\n\t\t}\n\n\t\t[PortHandler]","sourceCodeStart":396,"sourceCodeEnd":432,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/Windows/CollectionView/CarouselViewRenderer.cs#L396-L432","documentation":"Thrown as InvalidOperationException by CarouselViewRenderer.SetCarouselViewCurrentItem when GetItem(carouselPosition) does not return an ItemTemplateContext. This means the position passed validation (0 <= pos < ItemCount) but the item at that position has not yet been materialized/templated by the underlying list virtualization.","triggerScenarios":"The position is valid but the underlying ListViewBase has not yet created the container/item template context for that index — e.g., immediately after ItemsSource changes, during fast scrolling, or when virtualization defers container generation.","commonSituations":"Setting CarouselView.Position programmatically right after binding a new ItemsSource, before the UI has had a render pass. Also occurs during rapid Position changes or when the list is still loading.","solutions":["Delay setting Position until after the ItemsSource has been rendered (e.g., on the next dispatcher tick or after the ListView Loaded event).","Use CarouselView.ScrollTo with ScrollToPosition instead of setting Position directly during initialization.","Ensure the DataTemplate for items is registered before items are loaded."],"exampleFix":"// before\ncarouselView.ItemsSource = items;\ncarouselView.Position = 3; // throws: item not yet materialized\n\n// after — defer until loaded\ncarouselView.ItemsSource = items;\ncarouselView.Loaded += (s, e) =>\n{\n    carouselView.Position = 3;\n};","handlingStrategy":"validation","validationCode":"if (carouselView.ItemCount > 0 && position < carouselView.ItemCount)\n{\n    carouselView.Position = position;\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    carouselView.Position = desiredPosition;\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Visible item not found\"))\n{\n    // item not yet materialized; defer to next layout cycle","preventionTips":["Set Position after the CarouselView has rendered its items (e.g., after Loaded).","Avoid setting Position synchronously during ItemsSource assignment."],"tags":["wpf","maui-compatibility","carouselview","collectionview","virtualization"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}