'How to make a dynamic (add/remove) carousel in Xamarin?
I am making a CarouselView in Xamarin (Maui Blazor). It is intended that each item represents a day (starting from the present), and the user can swipe between days chronologically. I wish to be able to dynamically remove and add items to my carousel as the user navigates through the days, so that the collection behind does not become too large, ideally staying at either three or five entries or something similiar.
My XAML is as follows:
<CarouselView x:Name="carousel"
Loop="False"
ItemsSource="{Binding ItemCollection}">
<CarouselView.ItemTemplate>
<DataTemplate>
<ContentView Content="{Binding .}">
</ContentView>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
I have a property for the collection...
public ObservableCollection<BlazorWebView> ItemCollection { get; set; }
...and then methods to set up the carousel:
public MainPage()
{
InitializeComponent();
ObservableCollection<BlazorWebView> itemCollection = new ObservableCollection<BlazorWebView>();
for (int i = 0; i < 100; i++)
{
itemCollection.Add(SetUpBlazorWebView(i));
}
ItemCollection = itemCollection;
BindingContext = this;
}
public BlazorWebView SetUpBlazorWebView(int counterParameter)
{
BlazorWebView blazor = new BlazorWebView()
{
HostPage = "wwwroot/index.html"
};
blazor.RootComponents.Add(new RootComponent()
{
ComponentType = typeof(Pages.Counter),
Selector = "#app",
Parameters = new Dictionary<string, object>() { { "CurrentCount", counterParameter} }
});
return blazor;
}
(Pages.Counter is just the default Blazor counter component modified so that the currentCount is a parameter) This all works fine, however it has 100 items. So far I have tried binding the position to a property that modifies the collection through the setter depending on whether the new position is higher or lower. This does not work, specifically modifying the collection from within the setter.
Does anyone have any advice on how to solve this problem? Or possibly a different approach as I am aware that I may be making some fundamental mistakes with regards to my approach to the problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
