Skip to content

Commit

Permalink
1207 UniformItemsLayout NoItems crash (#1220)
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavAntonyuk authored Jun 4, 2023
1 parent 0e35019 commit e89e7da
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@
</StackLayout>
</StackLayout>
</mct:UniformItemsLayout>

<Button Text="Add Random item" Command="{Binding AddItemCommand}" HorizontalOptions="Center" VerticalOptions="Center" Padding="8"/>
<mct:UniformItemsLayout BindableLayout.ItemsSource="{Binding Items}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Label Text="{Binding}" />
</DataTemplate>
</BindableLayout.ItemTemplate>
</mct:UniformItemsLayout>
</VerticalStackLayout>
</ScrollView>
</pages:BasePage>
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
namespace CommunityToolkit.Maui.Sample.ViewModels.Layouts;
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

public class UniformItemsLayoutViewModel : BaseViewModel
namespace CommunityToolkit.Maui.Sample.ViewModels.Layouts;

public partial class UniformItemsLayoutViewModel : BaseViewModel
{
[RelayCommand]
void AddItem()
{
Items.Add(Path.GetRandomFileName());
}

public ObservableCollection<string> Items { get; } = new();
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public override Size Measure(double widthConstraint, double heightConstraint)

if (childWidth == 0)
{
var sizeRequest = visibleChildren[0].Measure(double.PositiveInfinity, double.PositiveInfinity);
var sizeRequest = visibleChildren.Length == 0 ? Size.Zero : visibleChildren[0].Measure(double.PositiveInfinity, double.PositiveInfinity);

childWidth = sizeRequest.Width;
childHeight = sizeRequest.Height;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ public void MeasureUniformItemsLayout_NoWrap()
Assert.Equal(expectedSize, actualSize);
}

[Fact]
public void MeasureUniformItemsLayout_NoItems_ZeroSize()
{
var expectedSize = Size.Zero;
var emptyUniformItemsLayout = new UniformItemsLayout();

var actualSize = emptyUniformItemsLayout.CrossPlatformMeasure(double.PositiveInfinity, double.PositiveInfinity);

Assert.Equal(expectedSize, actualSize);
}

[Fact]
public void MeasureUniformItemsLayout_WrapOnNextRow()
{
Expand Down

0 comments on commit e89e7da

Please sign in to comment.