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
Original file line number Diff line number Diff line change
Expand Up @@ -571,46 +571,42 @@ void ClearLayoutListener()

protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
if (Carousel.Loop)
// If the height or width are unbounded and the user is set to
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only removed the if (Carousel.Loop) condition but indentation changes make it harder to spot

// Loop then we can't just do an infinite measure.
// Looping works by setting item count to 16384 so if the
// CarV has infinite room it'll generate all 16384 items.
// This code forces the adapter to just measure the first item
// And then that measure is used for the WxH of the CarouselView

// I found that "AtMost" also causes this behavior so
// that's why I'm turning "AtMost" into "Exactly"
if (MeasureSpec.GetMode(widthMeasureSpec) == MeasureSpecMode.AtMost)
{
// If the height or width are unbounded and the user is set to
// Loop then we can't just do an infinite measure.
// Looping works by setting item count to 16384 so if the
// CarV has infinite room it'll generate all 16384 items.
// This code forces the adapter to just measure the first item
// And then that measure is used for the WxH of the CarouselView

// I found that "AtMost" also causes this behavior so
// that's why I'm turning "AtMost" into "Exactly"
if (MeasureSpec.GetMode(widthMeasureSpec) == MeasureSpecMode.AtMost)
{
widthMeasureSpec = MeasureSpecMode.Exactly.MakeMeasureSpec(widthMeasureSpec.GetSize());
}
widthMeasureSpec = MeasureSpecMode.Exactly.MakeMeasureSpec(widthMeasureSpec.GetSize());
}

if (MeasureSpec.GetMode(heightMeasureSpec) == MeasureSpecMode.AtMost)
{
heightMeasureSpec = MeasureSpecMode.Exactly.MakeMeasureSpec(heightMeasureSpec.GetSize());
}
if (MeasureSpec.GetMode(heightMeasureSpec) == MeasureSpecMode.AtMost)
{
heightMeasureSpec = MeasureSpecMode.Exactly.MakeMeasureSpec(heightMeasureSpec.GetSize());
}

if (MeasureSpec.GetMode(widthMeasureSpec) == MeasureSpecMode.Unspecified ||
MeasureSpec.GetMode(heightMeasureSpec) == MeasureSpecMode.Unspecified)
if (MeasureSpec.GetMode(widthMeasureSpec) == MeasureSpecMode.Unspecified ||
MeasureSpec.GetMode(heightMeasureSpec) == MeasureSpecMode.Unspecified)
{
if (ItemsViewAdapter.ItemCount > 0)
{
if (ItemsViewAdapter.ItemCount > 0)
{
// Retrieve the first item of the CarouselView and measure it
// This is what we'll use for the CarV WxH if the requested measure
// is for an infinite amount of space

var viewType = ItemsViewAdapter.GetItemViewType(0);
var viewHolder = (ViewHolder)ItemsViewAdapter.CreateViewHolder(this, viewType);
ItemsViewAdapter.BindViewHolder(viewHolder, 0);
viewHolder.ItemView.Measure(widthMeasureSpec, heightMeasureSpec);
widthMeasureSpec = MeasureSpecMode.Exactly.MakeMeasureSpec(viewHolder.ItemView.MeasuredWidth);
heightMeasureSpec = MeasureSpecMode.Exactly.MakeMeasureSpec(viewHolder.ItemView.MeasuredHeight);
}
// Retrieve the first item of the CarouselView and measure it
// This is what we'll use for the CarV WxH if the requested measure
// is for an infinite amount of space

var viewType = ItemsViewAdapter.GetItemViewType(0);
var viewHolder = (ViewHolder)ItemsViewAdapter.CreateViewHolder(this, viewType);
ItemsViewAdapter.BindViewHolder(viewHolder, 0);
viewHolder.ItemView.Measure(widthMeasureSpec, heightMeasureSpec);
widthMeasureSpec = MeasureSpecMode.Exactly.MakeMeasureSpec(viewHolder.ItemView.MeasuredWidth);
heightMeasureSpec = MeasureSpecMode.Exactly.MakeMeasureSpec(viewHolder.ItemView.MeasuredHeight);
}
}

base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
105 changes: 105 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue25192.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue25192">
<Grid>
<CarouselView Margin="2,9,2,20"
HeightRequest="180"
Loop="False"
PeekAreaInsets="20"
VerticalOptions="End">
<CarouselView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Item1</x:String>
<x:String>Item2</x:String>
<x:String>Item3</x:String>
</x:Array>
</CarouselView.ItemsSource>
<CarouselView.ItemsLayout>
<LinearItemsLayout ItemSpacing="5"
Orientation="Horizontal"
SnapPointsAlignment="Center"
SnapPointsType="MandatorySingle"/>
</CarouselView.ItemsLayout>
<CarouselView.ItemTemplate>
<DataTemplate
x:DataType="{x:Null}">
<Border BackgroundColor="Red"
Stroke="Blue"
StrokeShape="RoundRectangle 12"
StrokeThickness="0">
<Grid Margin="16"
ColumnDefinitions="*,*"
ColumnSpacing="15"
RowDefinitions="*,*,36"
RowSpacing="15">
<VerticalStackLayout
Grid.Row="0"
Grid.ColumnSpan="2"
VerticalOptions="Center">
<Label FontSize="16"
LineBreakMode="WordWrap"
Text="DummyLabel"
TextColor="#333333"
VerticalOptions="Center"/>
<Label FontFamily="Medium"
FontSize="14"
LineBreakMode="WordWrap"
Text="DummyDesc"
TextColor="#5F5E6A"/>
</VerticalStackLayout>

<Border Grid.Row="1"
Grid.Column="0"
BackgroundColor="LightGray"
HorizontalOptions="Start"
StrokeShape="RoundRectangle 12"
StrokeThickness="0"
VerticalOptions="Center">
<Label Grid.Column="1"
FontFamily="Regular"
FontSize="14"
Margin="6"
AutomationId="{Binding .}"
Text="{Binding .}"
TextColor="Black"
VerticalOptions="Center"/>
</Border>

<Border Grid.Row="1"
Grid.Column="1"
BackgroundColor="#e8fccf"
HorizontalOptions="Center"
StrokeShape="RoundRectangle 12"
StrokeThickness="0"
VerticalOptions="Center">
<Label Grid.Column="1"
FontFamily="Regular"
FontSize="14"
Margin="6"
Text="{Binding .}"
TextColor="#155d27"
VerticalOptions="Center"/>
</Border>

<Label Grid.Row="2"
FontSize="24"
HorizontalOptions="Start"
Text="I am here"
TextColor="Black"
VerticalOptions="Center"/>

<Button Grid.Row="2"
Grid.Column="1"
BackgroundColor="White"
BorderWidth="2"
TextColor="Black"
Text="Click me"
VerticalOptions="Center"/>
</Grid>
</Border>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</Grid>
</ContentPage>
11 changes: 11 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue25192.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Maui.Controls.Sample.Issues
{
[Issue(IssueTracker.Github, 25192, "CarouselView Loop='False' renders incorrectly items", PlatformAffected.All)]
public partial class Issue25192 : ContentPage
{
public Issue25192()
{
InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue25192 : _IssuesUITest
{
public Issue25192(TestDevice testDevice) : base(testDevice)
{
}

public override string Issue => "CarouselView Loop='False' renders incorrectly items";

[Test]
[Category(UITestCategories.CarouselView)]
public void CarouselViewShouldRenderCorrectly()
{
App.WaitForElement("Item1");
VerifyScreenshot();
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.