首页 文章

如何在UWP中更改Button的背景颜色

提问于
浏览
0

在SO中有一些相同的问题要求改变UWP(通用Windows平台)中按钮的背景颜色:

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

how to change background color of button in UWP Apps in c# ?

但是,似乎微软一直在改变一切 . 无法再找到 SolidColorBrush . 我尝试过使用 Windows.UI.Xaml.Media.BrushWindows.UI.Colors . 这些都不起作用 .

鼠标悬停在button.background上,提示显示背景预期类型: Windows.UI.Xaml.Media.Brush .

enter image description here

My Question :如何使用c#代码更改按钮的背景颜色?如果我在其他相同的帖子中使用建议的解决方案,则找不到名称空间 SolidColorBrush .

1 回答

  • 2

    您是否导入了以下命名空间?

    Windows.UI.Xaml.Media
    

    因为如果没有,您将不会直接访问SolidColorBrush类,并且必须通过以下方式访问:

    Windows.UI.Xaml.Media.SolidColorBrush mycolor = new SolidColorBrush(Windows.UI.Colors.Blue);
    

    在这个例子中,我创建了一个带有Color of Blue的SolidColorBrush,我直接setit,没有任何类型的转换,如下所示:

    myButton.Background = mycolor;
    

    如果您想创建自己的颜色,可以使用 Windows.UI.Color.FromArgb 方法,您甚至可以在其中指定颜色的alpha .

    编辑:

    回顾你的答案,我意识到你正试图用 Brush 类创建你的 Brush ,它将设置一个按钮控件的 Background . Brush类是几个派生的Brush类的父类,它们都有不同的用途 . 其中一个是 SolidColorBrush 类 .

相关问题