WPF中的资源

开发工具与关键技术:Visual Studio 2015、WPF
作者:黄元进
撰写时间: 2019年6月4日

先来解释一下什么是资源和资源的优点:1.WPF中的资源与表传统WEB应用中CSS样式类似
2.目的是为了实现对象的重复调用 3.有助于XAML代码重用,有助于应

用维护的一致性
而定义资源的语法格式为:<根元素对象.Resources><资源定义/></根元素对象.Resources>
下面来用资源语法来做一个例子,实现效果如下:在这里插入图片描述
鼠标点击效果
在这里插入图片描述
用XAML代码实现:

<Windowx:Class="XAML高级教程Demo.资源"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="资源"Height="300" Width="300">
<Grid>
<Grid.Resources> 
<!--线性渐变画刷-->
<LinearGradientBrushx:Key="bgBrush" StartPoint="0,0" EndPoint="1,1"Opacity="0.8">
<GradientStopColor="Yellow" Offset="0.0"/>
<GradientStop Color="Red"Offset="0.25"/>
<GradientStopColor="Blue" Offset="0.75"/>
<GradientStopColor="LimeGreen" Offset="1.0"/>
</LinearGradientBrush>
</Grid.Resources>
<!--依赖属性绑定值-->
<Button x:Name="btnSubmit"Background="{StaticResource bgBrush}" Height="60"Width="120" Margin="87,43,86.6,167.4" > 
</Button>
<Button Height="60"Width="120" Margin="87,150,86.6,60.4" >
<Button.Background>
<!--线性渐变画刷-->
<LinearGradientBrushStartPoint="0.5,0" EndPoint="0.5,1"Opacity="0.5">
<GradientStopColor="#FFCD5174" Offset="0"/>
<GradientStopColor="Yellow" Offset="1"/> 
</LinearGradientBrush>
</Button.Background>
</Button>
</Grid>
</Window>

猜你喜欢

转载自blog.csdn.net/weixin_44547949/article/details/91376713