Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ protected override ItemsViewScrolledEventArgs ComputeVisibleIndexes(ItemsViewScr
{
args = base.ComputeVisibleIndexes(args, orientation, advancing);

if (ItemsView.Loop && ItemsView.ItemsSource is not null)
if (ItemsView.Loop && ItemsView.ItemsSource is not null && ItemCount > 0)
{
args.FirstVisibleItemIndex %= ItemCount;
args.CenterItemIndex %= ItemCount;
Expand All @@ -156,6 +156,21 @@ protected override ItemsViewScrolledEventArgs ComputeVisibleIndexes(ItemsViewScr
return args;
}

protected override void UpdateEmptyViewVisibility()
{
if (ItemsView?.Loop == true)
Comment thread
Shalini-Ashokan marked this conversation as resolved.
{
bool isEmpty = (CollectionViewSource?.View?.Count ?? 0) == 0;
var targetTemplate = isEmpty ? null : CarouselItemsViewTemplate;
if (ListViewBase.ItemTemplate != targetTemplate)
{
ListViewBase.ItemTemplate = targetTemplate;
}
}

base.UpdateEmptyViewVisibility();
}

ListViewBase CreateCarouselListLayout(ItemsLayoutOrientation layoutOrientation)
{
UI.Xaml.Controls.ListView listView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
override Microsoft.Maui.Controls.Shapes.Shape.OnPropertyChanged(string? propertyName = null) -> void
override Microsoft.Maui.Controls.GraphicsView.OnBindingContextChanged() -> void
override Microsoft.Maui.Controls.TitleBar.OnBindingContextChanged() -> void
override Microsoft.Maui.Controls.Handlers.Items.CarouselViewHandler.UpdateEmptyViewVisibility() -> void
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
148 changes: 148 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue7150.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using System;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace Maui.Controls.Sample.Issues;
[Issue(IssueTracker.Github, 7150, "EmptyView using Template displayed at the same time as the content", PlatformAffected.UWP)]
public class Issue7150 : TestContentPage
{
public Issue7150()
{
Title = "Issue 7150";
BindingContext = new Issue7150ViewModel();
}
protected override void Init()
{
var filterButton = new Button
{
Margin = new Thickness(20),
AutomationId = "FilterButton",
Text = "Filter"
};
filterButton.SetBinding(Button.CommandProperty, "FilterCommand");

var emptyViewContent = new StackLayout
{
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Start,
Children =
{
new Label
{
Text = "No results matched your filter.",
Margin = new Thickness(10, 25, 10, 10),
FontAttributes = FontAttributes.Bold,
FontSize = 18,
HorizontalTextAlignment = TextAlignment.Center
},
new Label
{
Text = "Try a broader filter?",
FontAttributes = FontAttributes.Italic,
FontSize = 12,
HorizontalTextAlignment = TextAlignment.Center
}
}
};

var emptyView = new ContentView { Content = emptyViewContent };
var carouselView = new CarouselView
{
ItemTemplate = GetCarouselTemplate(),
EmptyView = emptyView,
};

carouselView.SetBinding(ItemsView.ItemsSourceProperty, "Items");
var grid = new Grid
{
RowDefinitions = new RowDefinitionCollection
{
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Star }
}
};

grid.Add(filterButton, 0, 0);
grid.Add(carouselView, 0, 1);
Content = grid;
}

internal DataTemplate GetCarouselTemplate()
{
return new DataTemplate(() =>
{
var grid = new Grid();
var info = new Label
{
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
Margin = new Thickness(6)
};

info.SetBinding(Label.TextProperty, new Binding("Name"));
grid.Children.Add(info);
return grid;
});
}

public class Issue7150Model
{
public string Name { get; set; }
}

public class Issue7150ViewModel : BindableObject
{
ObservableCollection<Issue7150Model> _items;
public ICommand FilterCommand => new Command(FilterItems);
readonly IList<Issue7150Model> source;

public Issue7150ViewModel()
{
source = new List<Issue7150Model>();
source.Add(new Issue7150Model
{
Name = "Baboon"
});
source.Add(new Issue7150Model
{
Name = "Capuchin Monkey"
});
source.Add(new Issue7150Model
{
Name = "Blue Monkey"
});

Items = new ObservableCollection<Issue7150Model>(source);
}

public ObservableCollection<Issue7150Model> Items
{
get { return _items; }
set
{
_items = value;
OnPropertyChanged();
}
}

public void FilterItems()
{
var filter = "Mandrill";
var filteredItems = source.Where(monkey => monkey.Name?.Contains(filter, StringComparison.OrdinalIgnoreCase) ?? false).ToList();
foreach (var monkey in source)
{
if (!filteredItems.Contains(monkey))
{
Items?.Remove(monkey);
}
else
{
if (Items != null && !Items.Contains(monkey))
{
Items.Add(monkey);
}
}
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#if TEST_FAILS_ON_WINDOWS // Related issue for windows: https://github.com/dotnet/maui/issues/29245
Comment thread
kubaflo marked this conversation as resolved.
Comment thread
Shalini-Ashokan marked this conversation as resolved.
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;
public class Issue7150 : _IssuesUITest
{
public Issue7150(TestDevice device) : base(device) { }

public override string Issue => "EmptyView using Template displayed at the same time as the content";

[Test]
[Category(UITestCategories.CarouselView)]
public void VerifyCarouselViewEmptyView()
{
App.WaitForElement("FilterButton");
App.Tap("FilterButton");
VerifyScreenshot();
Comment thread
jsuarezruiz marked this conversation as resolved.
}
}
#endif
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.
Loading