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

[listview] fixes for various null/empty DataTemplate #13146

Merged
merged 2 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions src/Compatibility/Core/src/Android/VisualElementRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ protected virtual void OnElementPropertyChanged(object sender, PropertyChangedEv

protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
if (Element == null)
return;

UpdateLayout(((IElementController)Element).LogicalChildren);
if (Element is IElementController controller)
{
UpdateLayout(controller.LogicalChildren);
}
}

public override void Draw(Canvas canvas)
Expand Down
10 changes: 8 additions & 2 deletions src/Controls/src/Core/TemplatedItemsList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,14 @@ public DataTemplate SelectDataTemplate(object item)

public TItem ActivateContent(int index, object item)
{
TItem content = ItemTemplate != null ? (TItem)ItemTemplate.CreateContent(item, _itemsView) : _itemsView.CreateDefault(item);

if (ItemTemplate?.CreateContent(item, _itemsView) is TItem content)
{
// content is valid
}
else
{
content = _itemsView.CreateDefault(item);
}
jonathanpeppers marked this conversation as resolved.
Show resolved Hide resolved
content = UpdateContent(content, index, item);

return content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void SetupBuilder()
builder.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler<ViewCell, ViewCellRenderer>();
handlers.AddHandler<TextCell, TextCellRenderer>();
handlers.AddHandler<ListView, ListViewRenderer>();
handlers.AddHandler<VerticalStackLayout, LayoutHandler>();
handlers.AddHandler<Label, LabelHandler>();
Expand Down Expand Up @@ -152,5 +153,61 @@ await CreateHandlerAndAddToWindow<LayoutHandler>(layout, async (handler) =>
await Task.Delay(100);
});
}

[Fact]
public async Task NullTemplateDoesntCrash()
{
SetupBuilder();
ObservableCollection<string> data = new ObservableCollection<string>()
{
"cat",
"dog",
"catdog"
};

var listView = new ListView(ListViewCachingStrategy.RecycleElement)
{
ItemTemplate = new DataTemplate(() =>
{
return new ViewCell
{
View = new VerticalStackLayout
{
new Label()
}
};
}),
ItemsSource = data
};

var layout = new VerticalStackLayout
{
listView
};

await CreateHandlerAndAddToWindow<LayoutHandler>(layout, async _ =>
{
await Task.Delay(100);
ValidatePlatformCells(listView);
});

// Default ctor
listView.ItemTemplate = new DataTemplate();

await CreateHandlerAndAddToWindow<LayoutHandler>(layout, async _ =>
{
await Task.Delay(100);
ValidatePlatformCells(listView);
});

// Return null
listView.ItemTemplate = new DataTemplate(() => null);

await CreateHandlerAndAddToWindow<LayoutHandler>(layout, async _ =>
{
await Task.Delay(100);
ValidatePlatformCells(listView);
});
}
}
}