Better exception for using viewCell with collection View (#25681)

This commit is contained in:
Jakub Florkowski 2024-11-13 01:30:06 +01:00 коммит произвёл GitHub
Родитель c1bb5fd280
Коммит b9562d0d22
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 21 добавлений и 3 удалений

Просмотреть файл

@ -57,7 +57,13 @@ namespace Microsoft.Maui.Controls.Handlers.Items
_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

Просмотреть файл

@ -168,7 +168,12 @@ namespace Microsoft.Maui.Controls.Handlers.Items
}
// 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;

Просмотреть файл

@ -172,7 +172,14 @@ namespace Microsoft.Maui.Controls.Platform
// 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);