Skip to content

Commit

Permalink
Better exception for using viewCell with collection View (#25681)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaflo authored Nov 13, 2024
1 parent c1bb5fd commit b9562d0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/Controls/src/Core/Handlers/Items/iOS/TemplatedCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit b9562d0

Please sign in to comment.