简体   繁体   中英

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; 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.

in the first page

public MainPage()
    {
        InitializeComponent();

        MessagingCenter.Subscribe<Object, Color>(this, "changeColor", (arg,color) => {

            firstPage.BackgroundColor = color;


        });

    }

in the second page

private void ChangeColourForFirst(object sender, EventArgs e)
{
    MessagingCenter.Send<Object, Color>(this, "changeColor", Color.Red); // send the bgcolor that you want to change
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM