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

C# 如何更改通用Windows平台应用程序中单击按钮事件的背景色?,c#,xaml,windows-10,win-universal-app,C#,Xaml,Windows 10,Win Universal App,我正在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.Backgro

我正在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);

使用以下列表中的预定义颜色对象:


您也可以提供不同的颜色

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

这非常简单和合适,因为您可以提供任何颜色,而不是默认颜色,如黑色、橙色等。

您可能更幸运地使用XAML而不是代码隐藏。可能会有帮助。是的,这很好,但我需要更改C代码的颜色,因为我正在处理不同的场景。“BackgroundProperty”和“UI”。只是提一下,这样人们就可以复制/粘贴而不会出错。看起来SolidColorBrush在UWP中已经不存在了?
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)
                )
            );