From 3824730059ad43b6cbd10121098854daaf2247d7 Mon Sep 17 00:00:00 2001 From: Jakub Florkowski Date: Tue, 5 Nov 2024 02:56:05 +0100 Subject: [PATCH] Better exception for using viewCell with collection View --- .../Handlers/Items/Android/TemplatedItemViewHolder.cs | 8 +++++++- .../src/Core/Handlers/Items/iOS/TemplatedCell.cs | 7 ++++++- .../Windows/CollectionView/ItemContentControl.cs | 9 ++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Controls/src/Core/Handlers/Items/Android/TemplatedItemViewHolder.cs b/src/Controls/src/Core/Handlers/Items/Android/TemplatedItemViewHolder.cs index 2d6d3e035b9b..c0581ead5c01 100644 --- a/src/Controls/src/Core/Handlers/Items/Android/TemplatedItemViewHolder.cs +++ b/src/Controls/src/Core/Handlers/Items/Android/TemplatedItemViewHolder.cs @@ -57,7 +57,13 @@ public void Bind(object itemBindingContext, ItemsView itemsView, _itemContentView.Recycle(); // Create the new content - View = (View)template.CreateContent(); + var content = template.CreateContent(); + View = content as View; + + if(View is null) + { + throw new InvalidOperationException($"{template} could not be created from {content}"); + } // Set the binding context _before_ we create the renderer; that way, the bound data will be // available during OnElementChanged diff --git a/src/Controls/src/Core/Handlers/Items/iOS/TemplatedCell.cs b/src/Controls/src/Core/Handlers/Items/iOS/TemplatedCell.cs index 480538a4c128..20a51d6d67cb 100644 --- a/src/Controls/src/Core/Handlers/Items/iOS/TemplatedCell.cs +++ b/src/Controls/src/Core/Handlers/Items/iOS/TemplatedCell.cs @@ -168,7 +168,12 @@ public void Bind(DataTemplate template, object bindingContext, ItemsView itemsVi } // Create the content and renderer for the view - var view = itemTemplate.CreateContent() as View; + var content = itemTemplate.CreateContent(); + + if(content is not View view) + { + throw new InvalidOperationException($"{itemTemplate} could not be created from {content}"); + } // Set the binding context _before_ we create the renderer; that way, it's available during OnElementChanged view.BindingContext = bindingContext; diff --git a/src/Controls/src/Core/Platform/Windows/CollectionView/ItemContentControl.cs b/src/Controls/src/Core/Platform/Windows/CollectionView/ItemContentControl.cs index e099f3f545e7..486a3c85c0f5 100644 --- a/src/Controls/src/Core/Platform/Windows/CollectionView/ItemContentControl.cs +++ b/src/Controls/src/Core/Platform/Windows/CollectionView/ItemContentControl.cs @@ -172,7 +172,14 @@ internal void Realize() // If the content has never been realized (i.e., this is a new instance), // or if we need to switch DataTemplates (because this instance is being recycled) // then we'll need to create the content from the template - _visualElement = dataTemplate.CreateContent() as VisualElement; + var content = dataTemplate.CreateContent(); + _visualElement = content as VisualElement; + + if(_visualElement is null) + { + throw new InvalidOperationException($"{dataTemplate} could not be created from {content}"); + } + _visualElement.BindingContext = dataContext; _handler = _visualElement.ToHandler(mauiContext);