Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check if the background color of a button is [color]? (.NET Maui – VS)

Hello this is my code (.NET Maui – VS)

Mainpage.xaml.cs:

using System.ComponentModel;
using System.Windows.Input;
using Microsoft.Maui.Controls;

namespace App;

public partial class MainPage : ContentPage
{
    
    public MainPage()
    {
        InitializeComponent();
    }

    ......

    private void LockerClicked(object sender, EventArgs e)
    {
        Button clickedButton = (Button)sender;

        if (clickedButton.BackgroundColor == Color.Blue)
        {
            // Action
        }
    }
}

Mainpage.cs:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

......
<Button x:Name="LockerButton" Clicked="LockerClicked" Text="⌨" Grid.Row="3" Grid.Column="2" FontSize="40" TextColor="#007070" FontAttributes="Bold" CornerRadius="100" BackgroundColor="White"/>
......

However, I get a Compiler Error CS0120: An object reference is required for the nonstatic field, method, or property ‘Color.Blue’.

What is wrong with my code? Thank you in advance!

>Solution :

you need to use Colors, not Color

if (clickedButton.BackgroundColor == Colors.Blue)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading