danH
danH

Reputation: 105

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>

Upvotes: 0

Views: 65

Answers (1)

Lucas Zhang
Lucas Zhang

Reputation: 18861

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
}

Upvotes: 1

Related Questions