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

Commit 109bc1a

Browse files
authored
[Core] Add Selected VisualState for IndicatorView (#13534)
* [Controls] Add repo for issue #13515 * [Core] Add selected state to IndicatorView Fixes part of #13515
1 parent 020fc33 commit 109bc1a

File tree

8 files changed

+91
-7
lines changed

8 files changed

+91
-7
lines changed

Xamarin.Forms.Controls/GalleryPages/IndicatorViewGalleries/IndicatorGalleries.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public IndicatorGalleries()
1919
GalleryBuilder.NavButton("IndicatorView Gallery", () =>
2020
new IndicatorsSample(), Navigation),
2121
GalleryBuilder.NavButton("Indicator MaxVisible Gallery", () =>
22-
new IndicatorsSampleMaximumVisible(), Navigation)
22+
new IndicatorsSampleMaximumVisible(), Navigation),
23+
GalleryBuilder.NavButton("Indicator VisualState", () =>
24+
new IndicatorsTemplateSample(), Navigation)
2325
}
2426
}
2527
};

Xamarin.Forms.Controls/GalleryPages/IndicatorViewGalleries/IndicatorsSample.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:local="clr-namespace:Xamarin.Forms.Controls"
77
mc:Ignorable="d"
8-
x:Class="Xamarin.Forms.Controls.IndicatorsSample">
8+
x:Class="Xamarin.Forms.Controls.GalleryPages.IndicatorsSample">
99
<ContentPage.Resources>
1010
<OnPlatform x:Key="IonicsFontFamily" x:TypeArguments="x:String">
1111
<On Platform="iOS" Value="Ionicons" />
@@ -25,7 +25,7 @@
2525
<DataTemplate>
2626
<Frame BackgroundColor="{Binding Color}">
2727
<Grid HorizontalOptions="Center" VerticalOptions="Center">
28-
<Label Text="{Binding Name}" FontSize="25"></Label>
28+
<Label Text="{Binding Name}" FontSize="25"/>
2929
</Grid>
3030
</Frame>
3131
</DataTemplate>

Xamarin.Forms.Controls/GalleryPages/IndicatorViewGalleries/IndicatorsSample.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Xamarin.Forms.Xaml;
22

3-
namespace Xamarin.Forms.Controls
3+
namespace Xamarin.Forms.Controls.GalleryPages
44
{
55
[XamlCompilation(XamlCompilationOptions.Compile)]
66
public partial class IndicatorsSample : ContentPage

Xamarin.Forms.Controls/GalleryPages/IndicatorViewGalleries/IndicatorsSampleMaximumVisible.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
33
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4-
x:Class="Xamarin.Forms.Controls.IndicatorsSampleMaximumVisible">
4+
x:Class="Xamarin.Forms.Controls.GalleryPages.IndicatorsSampleMaximumVisible">
55
<ContentPage.Resources>
66
<OnPlatform x:Key="IonicsFontFamily" x:TypeArguments="x:String">
77
<On Platform="iOS" Value="Ionicons" />

Xamarin.Forms.Controls/GalleryPages/IndicatorViewGalleries/IndicatorsSampleMaximumVisible.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using Xamarin.Forms.Controls.GalleryPages.CollectionViewGalleries.CarouselViewGalleries;
33

4-
namespace Xamarin.Forms.Controls
4+
namespace Xamarin.Forms.Controls.GalleryPages
55
{
66
public partial class IndicatorsSampleMaximumVisible : ContentPage
77
{
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
3+
x:Class="Xamarin.Forms.Controls.GalleryPages.IndicatorsTemplateSample">
4+
<ContentPage.Resources>
5+
<OnPlatform x:Key="IonicsFontFamily" x:TypeArguments="x:String">
6+
<On Platform="iOS" Value="Ionicons" />
7+
<On Platform="Android" Value="fonts/ionicons.ttf#" />
8+
<On Platform="UWP" Value="Assets/Fonts/ionicons.ttf#ionicons" />
9+
</OnPlatform>
10+
<x:String x:Key="Indicator">&#xf30c;</x:String>
11+
<Style x:Key="IndicatorLabelStyle" TargetType="Label">
12+
<Setter Property="VisualStateManager.VisualStateGroups">
13+
<VisualStateGroupList>
14+
<VisualStateGroup x:Name="CommonStates">
15+
<VisualState x:Name="Normal" >
16+
<VisualState.Setters>
17+
<Setter Property="TextColor"
18+
Value="Red" />
19+
</VisualState.Setters>
20+
</VisualState>
21+
<VisualState x:Name="Selected">
22+
<VisualState.Setters>
23+
<Setter Property="TextColor"
24+
Value="LightSkyBlue" />
25+
</VisualState.Setters>
26+
</VisualState>
27+
</VisualStateGroup>
28+
</VisualStateGroupList>
29+
</Setter>
30+
</Style>
31+
</ContentPage.Resources>
32+
<Grid Margin="20,20,20,20">
33+
<Grid.RowDefinitions>
34+
<RowDefinition></RowDefinition>
35+
<RowDefinition Height="Auto"></RowDefinition>
36+
<RowDefinition Height="Auto"></RowDefinition>
37+
</Grid.RowDefinitions>
38+
<CarouselView x:Name="carousel" ItemsSource="{Binding Items}" Grid.Row="0" Grid.RowSpan="3" IndicatorView="indicators">
39+
<CarouselView.ItemTemplate>
40+
<DataTemplate>
41+
<Frame BackgroundColor="{Binding Color}">
42+
<Grid HorizontalOptions="Center" VerticalOptions="Center">
43+
<Label Text="{Binding Name}" FontSize="25"></Label>
44+
</Grid>
45+
</Frame>
46+
</DataTemplate>
47+
</CarouselView.ItemTemplate>
48+
</CarouselView>
49+
<IndicatorView x:Name="indicators" Grid.Row="1" IndicatorColor="Transparent" SelectedIndicatorColor="Transparent" HorizontalOptions="Center" >
50+
<IndicatorView.IndicatorTemplate>
51+
<DataTemplate>
52+
<Label Style="{StaticResource IndicatorLabelStyle}"
53+
Text="{StaticResource Indicator}"
54+
FontFamily="{StaticResource IonicsFontFamily}"/>
55+
</DataTemplate>
56+
</IndicatorView.IndicatorTemplate>
57+
</IndicatorView>
58+
</Grid>
59+
</ContentPage>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
using Xamarin.Forms;
5+
6+
namespace Xamarin.Forms.Controls.GalleryPages
7+
{
8+
public partial class IndicatorsTemplateSample : ContentPage
9+
{
10+
public IndicatorsTemplateSample()
11+
{
12+
InitializeComponent();
13+
BindingContext = new GalleryPages.CollectionViewGalleries.CarouselViewGalleries.CarouselItemsGalleryViewModel(false, false);
14+
15+
}
16+
}
17+
}

Xamarin.Forms.Core/IndicatorStackLayout.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,15 @@ void ResetIndicatorStylesNonBatch()
9595
var maxVisible = _indicatorView.MaximumVisible;
9696
var position = _indicatorView.Position;
9797
var selectedIndex = position >= maxVisible ? maxVisible - 1 : position;
98-
Children[index].BackgroundColor = index == selectedIndex
98+
bool isSelected = index == selectedIndex;
99+
Children[index].BackgroundColor = isSelected
99100
? GetColorOrDefault(_indicatorView.SelectedIndicatorColor, Color.Gray)
100101
: GetColorOrDefault(_indicatorView.IndicatorColor, Color.Silver);
102+
103+
VisualStateManager.GoToState(Children[index], isSelected
104+
? VisualStateManager.CommonStates.Selected
105+
: VisualStateManager.CommonStates.Normal);
106+
101107
}
102108

103109
IsVisible = indicatorCount > 1 || !_indicatorView.HideSingle;

0 commit comments

Comments
 (0)