[英]Change button background color by clicking a different button on a different page
I am trying to achieve a task that bugs me a bit.我正在尝试完成一项让我有点烦恼的任务。 I have 2 buttons on 2 different pages, I want to achieve this;我在 2 个不同的页面上有 2 个按钮,我想实现这个; when pressing on the button situated on the second page, to change the background colour of the button situated on the first page.当按下位于第二页的按钮时,更改位于第一页的按钮的背景颜色。 Example: First Page:示例:首页:
<Stacklayout>
<Button Text="Task 1"
x:Name = "firstPage"
BackgroundColor = "Red" />
</Stacklayout>
SecondPage:第二页:
<Stacklayout>
<Button Text="Completed"
x:Name = "secondPage"
Clicked = "ChangeColourForFirst" />
</Stacklayout>
The simplest solution is to use MessagingCenter to send notification when clicking the button.最简单的解决方案是在单击按钮时使用MessagingCenter发送通知。
public MainPage()
{
InitializeComponent();
MessagingCenter.Subscribe<Object, Color>(this, "changeColor", (arg,color) => {
firstPage.BackgroundColor = color;
});
}
private void ChangeColourForFirst(object sender, EventArgs e)
{
MessagingCenter.Send<Object, Color>(this, "changeColor", Color.Red); // send the bgcolor that you want to change
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.