Skip to content
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
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
6 changes: 4 additions & 2 deletions src/Controls/src/Core/TemplatedItemsList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,10 @@ 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 not TItem content)
{
content = _itemsView.CreateDefault(item);
}
content = UpdateContent(content, index, item);

return content;
Expand Down
57 changes: 57 additions & 0 deletions src/Controls/tests/DeviceTests/Elements/ListView/ListViewTests.cs
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);
});
}
}
}