我在 xamarin 中很新,我正在嘗試向我的按鈕添加點擊事件。所以這是我的 4 個按鈕:
<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
<Button Text="Page 1" TextColor="#EEF2FF" FontSize="20" HorizontalOptions="Center" BackgroundColor="#FF5959" />
</StackLayout>
<StackLayout Grid.Column="1" HorizontalOptions="Center" VerticalOptions="Center">
<Button Text="Page 2" Clicked="Handle_Page" TextColor="#676FA3" FontSize="20" HorizontalOptions="Center" BackgroundColor="#EEF2FF"/>
</StackLayout>
<StackLayout Grid.Column="2" HorizontalOptions="Center" VerticalOptions="Center">
<Button Text="Page 3" TextColor="#676FA3" FontSize="20" HorizontalOptions="Center" BackgroundColor="#EEF2FF"/>
</StackLayout>
<StackLayout Grid.Column="3" HorizontalOptions="Center" VerticalOptions="Center">
<Button Text="Page 4" TextColor="#676FA3" FontSize="20" HorizontalOptions="Center" BackgroundColor="#EEF2FF"/>
</StackLayout>
所以按鈕的顏色是#EEF2FF,但是當我單擊按鈕時,我希望將其更改為#FF5959。但是如果已經點擊了另一個按鈕,那么它也應該變為主顏色,#EEF2FF。所以我們可以同步點擊事件。
我很新,所以任何幫助將不勝感激。
uj5u.com熱心網友回復:
添加單擊的處理程式
<Button Clicked="ButtonClicked" ... />
然后在處理程式中
protected void ButtonClicked(object sender, EventArgs args)
{
// set the other buttons back to the base color
// you will need to assign each button an x:Name in xaml
btn1.BackgroundColor = Color.FromHex("EEF2FF");
...
btn10.BackgroundColor = Color.FromHex("EEF2FF");
// then change the color of only the selected button
var btn = (Button)sender;
btn.BackgroundColor = Color.FromHex("FF5959");
}
uj5u.com熱心網友回復:
protected override void OnCreate(Bundle savedInstanceState)
{
ScreenClickButton.Click = ChangeColorToRed;
}
private void ChangeColorToRed(object sender, EventArgs e)
{
ScreenClickButton.SetBackgroundColor(color: Color.Red);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/446860.html
標籤:xamarin xamarin.forms xamarin.android xamarin.ios xamarin 工作室
