我有一個標簽串列,我使用StackLayout來劃分它。我沒有使用TabbedPage,因為這些標簽共享同一個頁面。
Page1.xaml
<StackLayout Padding="10,10,10,0" > <Frame x:Name="tab1" Padding="0" HasShadow="False" > <StackLayout> <標簽 TextColor="#848484" FontSize="13" HorizontalTextAlignment="中心" Text="Tab 1" /> <Label HeightRequest="2" Margin="0,3,0,0" HorizontalOptions="FillAndExpand" BackgroundColor="#00AA13" /> ---> 點擊時背景顏色會改變 </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" /> ---> 默認背景色標簽 </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";
}
uj5u.com熱心網友回復:
private void tab1_Tapped(object sender, EventArgs e)
{
tab1.BackgroundColor = Color.Blue;
}
CodePudding
檢查下面的代碼:
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.創建一個cusotm標簽來添加istapped屬性。
public class TapLabel:Label
{
public static readonly BindableProperty IsTappedProperty = BindableProperty.Create("IsTapped", typeof(Boolean), typeof(TapLabel), null);
公共的布爾型IsTapped
{
設定 { SetValue(IsTappedProperty, value); }
get { return (Boolean)GetValue(IsTappedProperty); }
}
}
2.創建viewmodel來系結。
public class TapViewModel:INotifyPropertyChanged
{
private Color color color=Color.Green;
private Boolean IsTapped;
public event PropertyChangedEventHandler PropertyChanged;
公共顏色 _Color
{ set { }
獲取
{
如果(IsTapped == false)
{ return Color.Gray; }
回傳顏色。
}
}
公共布林值 _IsTapped {
設定
{
IsTapped = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("_Color"))。
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("_IsTapped"))。
_Color = color;
}
獲取 { 回傳IsTapped; }
}}
3.消耗自定義標簽并使用databinding:
xmal:
<StackLayout Padding="10,10,10,0" >
<Frame x:Name="tab1" Padding="0" HasShadow="False" >
<StackLayout Padding="10,10,10,0" x:Name="stk1" >
<標簽 TextColor="#848484" FontSize="13" HorizontalTextAlignment="中心"
Text="Tab 1" />
<local:TapLabel x:Name="intab1" HeightRequest="2" Margin="0,3,0,0" HorizontalOptions="FillAndExpand"
IsTapped="{Binding _IsTapped,Mode=TwoWay}"
背景顏色="{Binding _Color}" />
</StackLayout>
<Frame.GestureRecognizers>
<TapGestureRecognizer Tapped="tab1_Tapped" />
</Frame.GestureRecognizers>
</Frame>
</StackLayout>
codebehind:
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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/317272.html
標籤:

