在 Xamarin 中点击时如何分配属性

How to assign properties when Tapped in Xamarin

我有一个Tabs列表,我用StackLayout来划分它。我不使用 TabbedPage,因为选项卡共享同一页。

  1. Page1.xaml

    <StackLayout Padding="10,10,10,0">
         <Frame x:Name="tab1" Padding="0" HasShadow="False">
             <StackLayout>
                 <Label TextColor="#848484" FontSize="13" HorizontalTextAlignment="Center"
                        Text="Tab 1" />
                 <Label HeightRequest="2" Margin="0,3,0,0" HorizontalOptions="FillAndExpand"
                        BackgroundColor="#00AA13" /> ---> BackgroundColor changes when Tapped
             </StackLayout>
             <Frame.GestureRecognizers>
                 <TapGestureRecognizer Tapped="tab1_Tapped" />
             </Frame.GestureRecognizers>
         </Frame>
     </StackLayout>
     <StackLayout Padding="10,10,10,0">
         <Frame x:Name="tab2" Padding="0" HasShadow="False">
             <StackLayout>
                 <Label TextColor="#848484" FontSize="13" HorizontalTextAlignment="Center" Text="Tab 2" />
                 <Label HeightRequest="2" Margin="0,3,0,0" HorizontalOptions="FillAndExpand"
                 BackgroundColor="#fff" /> ---> Default BackgroundColor Label
             </StackLayout>
             <Frame.GestureRecognizers>
                 <TapGestureRecognizer Tapped="tab2_Tapped" />
             </Frame.GestureRecognizers>
         </Frame>
     </StackLayout>
     ......
    

当点击选项卡时,如何通过更改背景颜色来激活选项卡。我尝试通过设置首选项。感谢大家的帮助

private void tab1_Tapped(object sender, EventArgs e)
{
   var tabactive = "tab1";
   ActiveColorBg();
}

private void tab2_Tapped(object sender, EventArgs e)
{
   var tabactive = "tab2";
   ActiveColorBg();
}
..........
private void ActiveColorBg()
{
    var bgcolor = "#00AA13";
}
private void tab1_Tapped(object sender, EventArgs e)
{
   tab1.BackgroundColor = Color.Blue;
}

检查下面的代码:

private void tab1_Tapped(object sender, EventArgs e)
    {
        ActiveColorBg(intab1, Color.Yellow);
    }
    private void tab2_Tapped(object sender, EventArgs e)
    {
        ActiveColorBg(intab2, Color.Green);
    }
    public void ActiveColorBg(Label label,Color color)
    {
        label.BackgroundColor = color;
    }

再次编辑:

1.Create 要添加一个 cusotm 标签 属性.

public class TapLabel:Label
{
    public static readonly BindableProperty IsTappedProperty = BindableProperty.Create("IsTapped", typeof(Boolean), typeof(TapLabel), null);
    public Boolean IsTapped
    {
        set { SetValue(IsTappedProperty, value); }
        get { return (Boolean)GetValue(IsTappedProperty); }
    }
}

2.create viewmodel 绑定。

public class TapViewModel:INotifyPropertyChanged
    {
       private Color color=Color.Green;
       private Boolean IsTapped;
        public event PropertyChangedEventHandler PropertyChanged;
        public Color _Color
        {   set { }
            get
            {
                if (IsTapped == false)
                { return Color.Gray; }
                return color;
            }
           
        }
        public Boolean _IsTapped {
            set
            {
                IsTapped = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("_Color"));
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("_IsTapped"));
                _Color = color;
            } 
            get { return IsTapped; }
        }}

3.consume自定义标签并使用数据绑定:

xmal:

      <StackLayout Padding="10,10,10,0" >
         <Frame x:Name="tab1" Padding="0" HasShadow="False">
             <StackLayout Padding="10,10,10,0" x:Name="stk1">
                 <Label TextColor="#848484" FontSize="13" HorizontalTextAlignment="Center"
                        Text="Tab 1" />
                 
                 <local:TapLabel x:Name="intab1" HeightRequest="2" Margin="0,3,0,0" HorizontalOptions="FillAndExpand"
                                 IsTapped="{Binding _IsTapped,Mode=TwoWay}"
                                 BackgroundColor="{Binding _Color}" /> 
             </StackLayout>
             <Frame.GestureRecognizers>
                 <TapGestureRecognizer Tapped="tab1_Tapped" />
             </Frame.GestureRecognizers>
         </Frame>
    </StackLayout>

代码隐藏:

  public MyTestPage2()
        {
            InitializeComponent();
            TapViewModel tvm1 = new TapViewModel();
            TapViewModel tvm2 = new TapViewModel();
            stk1.BindingContext = tvm1;
            stk2.BindingContext = tvm2;
        }
        private void tab1_Tapped(object sender, EventArgs e)
        {
            intab1.IsTapped = true;
            intab2.IsTapped = false;
        }