Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better exception for using viewCell with collection View #25681

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading