我正在尝试使用普通的 xamarin 表单制作类似视图的选项卡,因为我不想使用任何第三方插件。为此,我使用了如下所示的两个框架,并在点击该框架时将其状态更改为“已选择”和“未选择”,使其看起来像那样。
框架样式:
<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>
我的框架:
<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>
抽头事件:
private void Tab_Tapped(object sender, EventArgs e)
{
if (frameSelected != null)
VisualStateManager.GoToState(frameSelected, "UnSelected");
VisualStateManager.GoToState((Frame)sender, "Selected");
frameSelected = (Frame)sender;
}
但是我希望在页面第一次出现时看起来选择了一个框架。所以我尝试在页面 OnAppearing Method 中这样做。但它不起作用。这里有什么问题?
protected override void OnAppearing()
{
VisualStateManager.GoToState(AllNewsTab, "Selected");
base.OnAppearing();
}