如何更改通用 Windows 平台应用程序中点击事件按钮的背景颜色?

How to change the background color of button on Click Event in Universal Windows Platform Apps?

我正在 windows 10 中开发 UWP 应用程序,我正在尝试更改单击事件中按钮的背景颜色。这是我的代码:

private void button1_1_Click(object sender, RoutedEventArgs e)
{
    if (_Sign)
    {
        button_1_1.Content = "Cross";
        _Sign = false;
    }
    else
    {
        // button_1_1.Background = new SolidColorBrush(new Windows.UI.Color )    

        // indows.UI.Colors clr = new Windows.UI.Colors(new SolidColorBrush red);

        // SolidColorBrush color = new SolidColorBrush();
        // color = new SolidColorBrush.
        // button_1_1.Background = clr;

        button_1_1.Content = "Tick";
        _Sign = true;
    }
}

你完全可以做到

button1.SetValue(BackgroundProperty,new SolidColorBrush(Windows.UI.Colors.Black));

你可以玩那个!我现在不在我的电脑上检查它,但类似的东西有效。

或者你可以试试

button1.Background = new SolidColorBrush(Windows.Ui.Colors.Black);

使用 Colors properties:

中的预定义颜色对象
button_1_1.Background = new SolidColorBrush(Windows.UI.Colors.White);

您也可以提供不同的颜色

 SolidColorBrush mySolidColorBrush = new SolidColorBrush();
   mySolidColorBrush.Color = Color.FromArgb(0, 255, 244, 244);
 button1.Background = mySolidColorBrush;

你必须像这样将颜色代码转换为 Argb

return new SolidColorBrush(
                Color.FromArgb(
                    255,
                    Convert.ToByte(hexaColor.Substring(1, 2), 16),
                    Convert.ToByte(hexaColor.Substring(3, 2), 16),
                    Convert.ToByte(hexaColor.Substring(5, 2), 16)
                )
            );

它非常简单和适当,因为你可以给任何颜色而不是默认颜色,如黑色、橙色等。