This commit is contained in:
Rui Marinho 2024-09-13 11:26:50 +01:00
Родитель f419ad7ede
Коммит ede510ef57
5 изменённых файлов: 30 добавлений и 26 удалений

Просмотреть файл

@ -49,10 +49,10 @@ namespace Microsoft.Maui.Controls.Handlers.Items2
var layout = new UICollectionViewCompositionalLayout((sectionIndex, environment) =>
{
if(VirtualView is null)
if (VirtualView is null)
{
return null;
}
}
double sectionMargin = 0.0;
if (!IsHorizontal)
{
@ -87,19 +87,25 @@ namespace Microsoft.Maui.Controls.Handlers.Items2
section.OrthogonalScrollingBehavior = UICollectionLayoutSectionOrthogonalScrollingBehavior.GroupPagingCentered;
section.VisibleItemsInvalidationHandler = (items, offset, env) =>
{
//This will allow us to SetPosition when we are scrolling the items
//based on the current page
var page = (offset.X + sectionMargin) / env.Container.ContentSize.Width;
// Check if we not are at the beginning or end of the page
// Check if we not are at the beginning or end of the page and if we have items
if (Math.Abs(page % 1) > (double.Epsilon * 100) || Controller.ItemsSource.ItemCount <= 0)
{
return;
}
var pageIndex = (int)page;
var carouselPosition = pageIndex;
var cv2Controller = (CarouselViewController2)Controller;
//If we are looping, we need to get the correct position
if (ItemsView.Loop)
{
var maxIndex = (Controller.ItemsSource as Items.ILoopItemsViewSource).LoopCount - 1;
var maxIndex = (Controller.ItemsSource as ILoopItemsViewSource).LoopCount - 1;
//To mimic looping, we needed to modify the ItemSource and inserted a new item at the beginning and at the end
if (pageIndex == maxIndex)
@ -113,28 +119,29 @@ namespace Microsoft.Maui.Controls.Handlers.Items2
pageIndex = maxIndex - 1;
}
//since we added one item at the beginning, we need to subtract one
var realPage = pageIndex - 1;
//since we added one item at the beginning of our ItemSource, we need to subtract one
carouselPosition = pageIndex - 1;
if (ItemsView.Position != realPage)
if (ItemsView.Position != carouselPosition)
{
if (((CarouselViewController2)Controller).IsUpdating())
//If we are updating the ItemsSource, we don't want to scroll the CollectionView
if (cv2Controller.IsUpdating())
{
return;
}
var pageNumberIndex = NSIndexPath.FromItemSection(pageIndex, 0);
System.Diagnostics.Debug.WriteLine($"VisibleItemsInvalidationHandler Scrool: {realPage} {maxIndex} {pageNumberIndex}");
Controller.CollectionView.ScrollToItem(pageNumberIndex, UICollectionViewScrollPosition.Left, false);
var goToIndexPath = cv2Controller.GetScrollToIndexPath(carouselPosition);
//This will move the carousel to fake the loop
Controller.CollectionView.ScrollToItem(NSIndexPath.FromItemSection(pageIndex, 0), UICollectionViewScrollPosition.Left, false);
(Controller as CarouselViewController2)?.SetPosition(realPage);
//Update the CarouselView position
}
}
else
{
(Controller as CarouselViewController2)?.SetPosition((int)page);
}
};
//Update the CarouselView position
cv2Controller?.SetPosition(carouselPosition);
};
return section;
});

Просмотреть файл

@ -526,7 +526,6 @@ namespace Microsoft.Maui.Controls.Handlers.Items2
return;
}
System.Diagnostics.Debug.WriteLine($"UpdateInitialPosition ScrollToItem: {projectedPosition}");
CollectionView.ScrollToItem(projectedPosition, uICollectionViewScrollPosition, false);
//Set the position on VirtualView to update the CurrentItem also

Просмотреть файл

@ -1,4 +1,4 @@
#if IOS
#if IOS || MACCATALYST
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;
@ -15,6 +15,7 @@ namespace Microsoft.Maui.TestCases.Tests.Issues
[Test]
[Category(UITestCategories.CarouselView)]
[FailsOnMac("ScrollRight is not implemented on Mac")]
public void ScrollCarouselViewAfterDispose()
{
App.WaitForElement("goToShow");

Просмотреть файл

@ -1,5 +1,4 @@
#if !MACCATALYST
using NUnit.Framework;
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;
@ -25,5 +24,4 @@ namespace Microsoft.Maui.TestCases.Tests.Issues
VerifyScreenshot();
}
}
}
#endif
}

Просмотреть файл

@ -14,8 +14,7 @@ namespace Microsoft.Maui.TestCases.Tests.Issues
public override string Issue => "[Android] CarouselView: VirtualView cannot be null here, when clearing and adding items on second navigation";
[Test]
//[FailsOnIOS("This test is failing, likely due to product issue. More information: https://github.com/dotnet/maui/issues/22287")]
[FailsOnMac("This test is failing, likely due to product issue. More information: https://github.com/dotnet/maui/issues/22287")]
[FailsOnMac("This test is failing, App.Back() is not working as expected.")]
[FailsOnWindows("This test is failing, likely due to product issue. More information: https://github.com/dotnet/maui/issues/22287")]
public void CarouselViewVirtualViewNotNull()
{