你好
for (int i = 1; i < terasmasasayisi; i++)
{
var buttonteras = new Button
{
Text = i.ToString(),
HeightRequest = 45,
WidthRequest = 45,
Margin = 5,
CornerRadius = 100,
};
buttonteras.Clicked += butonteras;
teras.Children.Add(buttonteras);
async void butonteras(object o, EventArgs args)
{
secilenmasa = buttonteras.Text;
secilenkonum = "Teras";
await Navigation.PushModalAsync(new menu());
}
}
我以这种方式创建多个按钮,即terasmasasayisi用来计算数据库中有多少行
theese按钮将我定向到该页面上的另一页面,单击某个按钮后,我必须更改第一个按钮的背景色,我该怎么做?
答案 0 :(得分:0)
您可以检查以下代码
使用MessagingCenter
for (int i = 1; i < 10; i++)
{
var buttonteras = new Button
{
Text = i.ToString(),
HeightRequest = 45,
WidthRequest = 45,
Margin = 5,
CornerRadius = 100,
};
buttonteras.Clicked += butonteras;
stack.Children.Add(buttonteras);
}
MessagingCenter.Subscribe<Object,View>(this,"click",(obj,view)=>
{
var childrens = stack.Children;
for (int i = 0; i < childrens.Count; i++)
{
var element = childrens[i];
var type = element.GetType();
if (element == view)
{
element.BackgroundColor = Color.Red;
}
}
});
async void butonteras(object o, EventArgs args)
{
//...
var button = o as Button;
await Navigation.PushModalAsync(new menu(button));
}
然后在菜单中,单击按钮。
View Element;
public menu(View element)
{
Element = element;
InitializeComponent();
}
MessagingCenter.Send<Object,View>(this, "click",Element);
答案 1 :(得分:-1)
MessagingCenter.Subscribe<siparis>(this, "click", (obj) =>
{
var childrens = bahce.Children;
for (int i = 0; i < childrens.Count; i++)
{
var element = childrens[i];
var type = element.GetType();
if (element.GetType().ToString() == "Xamarin.Forms.Button")
{
element.BackgroundColor = Color.Red;
break;
}
}
});
async void bahcebuton(object o, EventArgs args)
{
secilenmasa = buttonbahce.Text;
secilenkonum = "Bahçe";
await Navigation.PushModalAsync(new menu());
}
和其他形式
if (masterpage.secilenkonum == "Bahçe")
{
MessagingCenter.Send<siparis>(this, "click");
}
现在它不会使所有按钮都变成红色,但现在仅使第一个按钮变成红色