I am trying to make a tabs like view with plain xamarin forms because I don't want to use any third party plugin. For that I used two frames like below and changed its state as "Selected" & "Unselected" when tapped on that frame to make it look like that.
Style for frame:
<Style TargetType="Frame">
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup>
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Orange" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="UnSelected">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="White" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
My Frame:
<Frame x:Name="AllNewsTab" Padding="10,5,10,5" CornerRadius="3" HasShadow="False" VerticalOptions="FillAndExpand">
<Label Text="All" FontFamily="{StaticResource BoldFont}" TextColor="{StaticResource BodyTextColor}" FontSize="Medium" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" HorizontalOptions="Center"/>
<Frame.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Tapped="Tab_Tapped"/>
</Frame.GestureRecognizers>
</Frame>
Tapped Event:
private void Tab_Tapped(object sender, EventArgs e)
{
if (frameSelected != null)
VisualStateManager.GoToState(frameSelected, "UnSelected");
VisualStateManager.GoToState((Frame)sender, "Selected");
frameSelected = (Frame)sender;
}
But I want one frame to look selected when the page appears for the first time. So I tried to do like this in the pages OnAppearing Method. But it doesn't work. What is the problem here?
protected override void OnAppearing()
{
VisualStateManager.GoToState(AllNewsTab, "Selected");
base.OnAppearing();
}