C# Xamarin按钮样式
嗨,我在Xamarin表单中有两个按钮,当一个按钮被点击时,另一个按钮没有点击时,它必须改变它的样式 我必须像图中所示的那样。 选择的一个应视为ITEM2,另一个应为ITEM1 如果我现在点击ITEM1,它应该采用ITEM2的样式,ITEM2应该采用ITEM1的样式。 我有使用XAMARIN表单编写的基本按钮代码,如下所示C# Xamarin按钮样式,c#,xamarin.ios,xamarin,C#,Xamarin.ios,Xamarin,嗨,我在Xamarin表单中有两个按钮,当一个按钮被点击时,另一个按钮没有点击时,它必须改变它的样式 我必须像图中所示的那样。 选择的一个应视为ITEM2,另一个应为ITEM1 如果我现在点击ITEM1,它应该采用ITEM2的样式,ITEM2应该采用ITEM1的样式。 我有使用XAMARIN表单编写的基本按钮代码,如下所示 var Button1 = new Button { Text = "ITEM1", Font = Font.SystemFontOfSize(14, Fo
var Button1 = new Button
{
Text = "ITEM1",
Font = Font.SystemFontOfSize(14, FontAttributes.Bold),
BorderWidth = 1,
HorizontalOptions = LayoutOptions.CenterAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand
};
Button1.Clicked += OnButton1Clicked;
var Button2 = new Button
{
Text = "ITEM2",
Font = Font.SystemFontOfSize(14, FontAttributes.Bold),
BorderWidth = 1,
HorizontalOptions = LayoutOptions.CenterAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand
};
Button2.Clicked += OnButton2Clicked;
var tabHeader = new StackLayout
{
HeightRequest = 48,
//BackgroundColor = BrandingManager.LightGrey(),
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand,
Children = { Button1, Button2},
Padding = new Thickness(10, 10, 10, 10)
};
您可以实现一个名为
SelectButton(Button-Button,bool-selected)
的方法,然后在您的OnButton1Clicked函数中
SelectButton(Button1, true);
SelectButton(Button2, false);
SelectButton(Button1, false);
SelectButton(Button2, true);
在你的OnButton2Clicked函数中
SelectButton(Button1, true);
SelectButton(Button2, false);
SelectButton(Button1, false);
SelectButton(Button2, true);
SelectButton方法将相应地设置背景颜色:
public void SelectButton(Button button, bool selected) {
if(selected) button.BackgroundColor = BrandingManager.Green();
else button.BackgroundColor = BrandingManager.LightGrey();
}
您可以在两个单击事件中使用不同的输入参数调用一个函数来为您处理该问题。谢谢您的建议。