简体   繁体   English

如何更改 Xamarin Forms 中控件的视觉 state?

[英]How to change the visual state of a control in Xamarin Forms?

I am trying to make a tabs like view with plain xamarin forms because I don't want to use any third party plugin.我正在尝试使用普通的 xamarin forms 制作一个类似视图的标签,因为我不想使用任何第三方插件。 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.为此,我使用了如下所示的两个框架,并在点击该框架时将其 state 更改为“已选择”和“未选择”,使其看起来像那样。

在此处输入图像描述

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.所以我尝试在页面 OnAppearing Method 中这样做。 But it doesn't work.但它不起作用。 What is the problem here?这里有什么问题?

protected override void OnAppearing()
        {
            VisualStateManager.GoToState(AllNewsTab, "Selected");

            base.OnAppearing();
        }

Try this, In xamarin, VisualElement have 4 states such as Normal, Disabled, Focused, Selected.试试这个,在 xamarin 中,VisualElement 有 4 种状态,例如 Normal、Disabled、Focused、Selected。 And we can define our own VisualElements.我们可以定义自己的 VisualElements。

MainPage.Xml主页.Xml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="Xam_VS_Test.Views.MainPage">
    <ContentPage.Resources>
        <Style TargetType="Frame">
            <Setter Property="VisualStateManager.VisualStateGroups">
                <VisualStateGroupList>
                    <VisualStateGroup Name="SelectionStates">
                        <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>
    </ContentPage.Resources>
    <ContentPage.Content>
        <StackLayout Orientation="Vertical" >
            <Frame x:Name="AllNewsTab" Padding="10,5,10,5" HeightRequest="20" WidthRequest="50" HorizontalOptions="FillAndExpand" CornerRadius="3" HasShadow="False" VerticalOptions="FillAndExpand">
                <Label Text="All" FontSize="Large" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" HorizontalOptions="Center"/>
                <Frame.GestureRecognizers>
                    <TapGestureRecognizer NumberOfTapsRequired="1" Tapped="Tab_Tapped"/>
                </Frame.GestureRecognizers>
            </Frame>
            <Frame x:Name="AllNewsTab2" Padding="10,5,10,5" HeightRequest="20" WidthRequest="50" HorizontalOptions="FillAndExpand" CornerRadius="3" HasShadow="False" VerticalOptions="FillAndExpand">
                <Label Text="1" FontSize="Large" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" HorizontalOptions="Center"/>
                <Frame.GestureRecognizers>
                    <TapGestureRecognizer NumberOfTapsRequired="1" Tapped="Tab_Tapped"/>
                </Frame.GestureRecognizers>
            </Frame>
            <Frame x:Name="AllNewsTab3" Padding="10,5,10,5" HeightRequest="20" WidthRequest="50" HorizontalOptions="FillAndExpand" CornerRadius="3" HasShadow="False" VerticalOptions="FillAndExpand">
                <Label Text="2" FontSize="Large" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" HorizontalOptions="Center"/>
                <Frame.GestureRecognizers>
                    <TapGestureRecognizer NumberOfTapsRequired="1" Tapped="Tab_Tapped"/>
                </Frame.GestureRecognizers>
            </Frame>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

MainPage.cs主页.cs

    public partial class MainPage : ContentPage
    {
        Frame frameSelected;
       public MainPage()
       {
           InitializeComponent();
       }
       protected override void OnAppearing()
       {
         if (frameSelected == null)
         {
            VisualStateManager.GoToState(AllNewsTab, "Selected");
            frameSelected = AllNewsTab;
         }
          base.OnAppearing();
        }
        private void Tab_Tapped(object sender, EventArgs e)
        {
            if (frameSelected != null)
                VisualStateManager.GoToState(frameSelected, "UnSelected");

            VisualStateManager.GoToState((Frame)sender, "Selected");

            frameSelected = (Frame)sender;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 命令驱动的可视化 state in Xamarin.Forms 自定义控件 - command-driven visual state in Xamarin.Forms custom control 如何在Visual Studio for Xamarin表单中更改AppId - How to change AppId in Visual Studio for Xamarin Forms 如何在Xamarin表单中的段控件中更改选项卡的样式 - How to change the style of tab in segment control in xamarin forms 如何更改“Xamarin.Forms Material Visual”禁用按钮的透明度? - How to change transparency of "Xamarin.Forms Material Visual" disabled button? 如何在 xamarin forms 中更改视觉材料 DatePickerDialog 和 TimePickerDialog 背景颜色 - How to change visual material DatePickerDialog and TimePickerDialog background color in xamarin forms 如何增加Xamarin表格控件的大小? - How to increase size of switch control Xamarin forms? 如何在Xamarin Forms中制作圆角编辑器控件 - How to make Rounded Editor control in Xamarin Forms 如何创建自定义WebView控件Xamarin表单 - How to create a custom WebView Control Xamarin forms 如何更改Xamarin.Forms中编辑器控件的OnMouseHover行为 - How do I change the OnMouseHover behavior of an Editor Control in Xamarin.Forms 如何使用Visual Material在iOS上的整个Xamarin Forms Prism应用程序中更改默认按钮配色方案? - How to change the default button color scheme throughout Xamarin Forms Prism application on iOS using Visual Material?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM