我的github:codetoys,所有代码都将会位于ctfc库中。已经放入库中我会指出在库中的位置。
这些代码大部分以Linux为目标但部分代码是纯C++的,可以在任何平台上使用。
源码指引:github源码指引_初级代码游戏的博客-CSDN博客
C#是我多年以来的业余爱好,新搞的东西能用C#的就用C#了。
我们肯定希望使用统一的控件风格,避免每个控件重复设置相同的属性。
由于微软的界面技术变化太快,各种技术名称又很容易混淆,文档太杂乱,经常很难找到合适的东西。
一、官方文档
比如这个:XAML 样式 - Windows apps | Microsoft Learn
看起来没什么问题:
里面给出的例子是:
代码是:
<Page.Resources>
<Style TargetType="Button">
<Setter Property="BorderThickness" Value="5" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="BorderBrush" >
<Setter.Value>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="Yellow" Offset="0.0" />
<GradientStop Color="Red" Offset="0.25" />
<GradientStop Color="Blue" Offset="0.75" />
<GradientStop Color="LimeGreen" Offset="1.0" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<StackPanel Orientation="Horizontal">
<Button Content="Button"/>
<Button Content="Button"/>
<Button Content="Button"/>
</StackPanel>
直接贴到MainWindow.xaml里面:
错的。Page.Resources不对,StackPanel也不对。
二、正确做法
正确的做法是,把Style部分放置在App.xaml的ResourceDictionary里面就可以了:
这个风格将直接应用到所有按钮上:
(这里是文档结束)