Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Add gallery pages for some carouselview bugs (#15250)
Browse files Browse the repository at this point in the history
* Add gallery pages for changing the itemssource of a carousel view, and hosting a carouselview inside a collecitonview

* Expand tests.

Co-authored-by: Oliver Brown <oliver_brown2@next.co.uk>
  • Loading branch information
GalaxiaGuy and OliverBrown-Next committed Apr 8, 2022
1 parent faaadf8 commit e967c7a
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public CarouselViewGallery()
new CarouselXamlGallery(false, 3), Navigation),
GalleryBuilder.NavButton("CarouselView Set CurrentItem Loop", () =>
new CarouselXamlGallery(true, 3), Navigation),
GalleryBuilder.NavButton("CarouselView Replace Source", () =>
new ReplaceSourceGallery(), Navigation)
}
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Xamarin.Forms.Controls.GalleryPages.CollectionViewGalleries.CarouselViewGalleries.ReplaceSourceGallery">
<StackLayout>
<CarouselView Loop="False" ItemsSource="{Binding Images}" HeightRequest="200" x:Name="ImageCarousel">
<CarouselView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding .}" />
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
<Label Text="{Binding Position, Source={x:Reference ImageCarousel}}"/>

<CarouselView Loop="False" ItemsSource="{Binding Colors}" HeightRequest="200" x:Name="ColorCarousel">
<CarouselView.ItemTemplate>
<DataTemplate>
<BoxView Color="{Binding .}" />
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
<Label Text="{Binding Position, Source={x:Reference ColorCarousel}}"/>

<Button Text="Update BindingContext" Clicked="BindingContextButton_Clicked" />
<Button Text="Update ItemsSource" Clicked="ItemsSourceButton_Clicked" />
</StackLayout>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;

namespace Xamarin.Forms.Controls.GalleryPages.CollectionViewGalleries.CarouselViewGalleries
{
public partial class ReplaceSourceGallery : ContentPage
{
public ReplaceSourceGallery()
{
InitializeComponent();
BindingContext = new ViewModel();
}

public void BindingContextButton_Clicked(object sender, EventArgs eventArgs)
{
BindingContext = new ViewModel();
}

public void ItemsSourceButton_Clicked(object sender, EventArgs eventArgs)
{
((ViewModel)BindingContext).Images = CreateImages();
}

private static IList<string> CreateImages() => new[]
{
"cover1.jpg",
"oasis.jpg",
"photo.jpg",
"Vegetables.jpg",
"Fruits.jpg",
"FlowerBuds.jpg",
"Legumes.jpg"
};

private static IList<Color> CreateColors() => new[]
{
Color.Red, Color.Blue, Color.Green
};

public class ViewModel
{
public IList<string> Images { get; set; } = CreateImages();
public IList<Color> Colors { get; set; } = CreateColors();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public CollectionViewGallery()
GalleryBuilder.NavButton("Alternate Layout Galleries", () => new AlternateLayoutGallery(), Navigation),
GalleryBuilder.NavButton("Header/Footer Galleries", () => new HeaderFooterGallery(), Navigation),
GalleryBuilder.NavButton("Nested CollectionViews", () => new NestedGalleries.NestedCollectionViewGallery(), Navigation),
GalleryBuilder.NavButton("CarouselView in CollectionView", () => new NestedGalleries.CarouselInCollectionViewGallery(), Navigation),
}
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Xamarin.Forms.Controls.GalleryPages.CollectionViewGalleries.NestedGalleries.CarouselInCollectionViewGallery">
<CollectionView ItemsSource="{Binding Items}">
<CollectionView.ItemsLayout>
<GridItemsLayout Span="2" Orientation="Vertical" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<CarouselView Loop="False" ItemsSource="{Binding Images}" HeightRequest="200" x:Name="Carousel">
<CarouselView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding .}" />
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
<Label Text="{Binding Position, Source={x:Reference Carousel}}"/>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using System.Linq;

namespace Xamarin.Forms.Controls.GalleryPages.CollectionViewGalleries.NestedGalleries
{
public partial class CarouselInCollectionViewGallery : ContentPage
{
public CarouselInCollectionViewGallery()
{
InitializeComponent();
BindingContext = new ViewModel();
}

public class ViewModel
{
public List<Item> Items { get; }

public ViewModel()
{
Items = Enumerable.Range(0, 30).Select(x => new Item()).ToList();
}
}

public class Item
{
public IList<string> Images { get; set; } = new[]
{
"cover1.jpg",
"oasis.jpg",
"photo.jpg",
"Vegetables.jpg",
"Fruits.jpg",
"FlowerBuds.jpg",
"Legumes.jpg"
};
}
}
}

0 comments on commit e967c7a

Please sign in to comment.