在同一个XAML中如何让多个对象使用同一个Resource,同样,不在同一个XAML中的对象也可以使用同一个Resource。Resources可以写在Grid下,也可以写在StackPanel中,还可以写在UserControl中,下面就来一一介绍。
一.在Grid中
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="EightBall.MainPage" Width="400" Height="300"> <Grid x:Name="grid1"> <Grid.Resources> <LinearGradientBrush x:Key="ButtonFace"> <GradientStop Offset="0.00" Color="Yellow" /> <GradientStop Offset="0.50" Color="White" /> <GradientStop Offset="1.00" Color="Purple" /> </LinearGradientBrush> </Grid.Resources> <Button Content="Click Me Frist" Width="200" Height="50" Background="{StaticResource ButtonFace}"></Button> </Grid> </UserControl>
二、在UserControl中
<UserControl x:Class="Resources.EightBallResource" xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300"> <UserControl.Resources> <LinearGradientBrush x:Key="BackgroundBrush"> <LinearGradientBrush.GradientStops> <GradientStop Offset="0.00" Color="Yellow" /> <GradientStop Offset="0.50" Color="White" /> <GradientStop Offset="1.00" Color="Purple" /> </LinearGradientBrush.GradientStops> </LinearGradientBrush> </UserControl.Resources> <Grid Name="grid1" Background="{StaticResource BackgroundBrush}"> </Grid> </UserControl>
三、在StackPanel中
<UserControl x:Class="Resources.ResourceHierarchy" xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300"> <StackPanel x:Name="LayoutRoot" Background="White"> <StackPanel> <StackPanel.Resources> <LinearGradientBrush x:Key="ButtonFace"> <GradientStop Offset="0.00" Color="Yellow" /> <GradientStop Offset="0.50" Color="White" /> <GradientStop Offset="1.00" Color="Purple" /> </LinearGradientBrush> </StackPanel.Resources> <Button Content="Panel Brush" Margin="5" Background="{StaticResource ButtonFace}"></Button> <Button Content="Panel Brush" Margin="5" Background="{StaticResource ButtonFace}"></Button> </StackPanel> </StackPanel> </UserControl>
不仅仅在XAML中可以设置Resources,也可以在代码中做设置,下面我们来看一个例子,
<UserControl x:Class="Resources.ResourcesInCode" xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300"> <UserControl.Resources> <LinearGradientBrush x:Key="ButtonFace"> <GradientStop Offset="0.00" Color="Purple" /> <GradientStop Offset="0.50" Color="White" /> <GradientStop Offset="1.00" Color="Yellow" /> </LinearGradientBrush> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White"> <StackPanel> <Button Content="Change the Resource" Margin="5" Click="cmdChange_Click" Background="{StaticResource ButtonFace}"></Button> <Button Content="Replace the Resource (not allowed)" Margin="5" Click="cmdReplace_Click" Background="{StaticResource ButtonFace}"></Button> </StackPanel> </Grid> </UserControl>
完成后的界面如下:
现在我们来做Change the Resource的效果,使达到这样的效果:
不难发现,我们是要使这个按钮的背景色从“红-黄”变成“黄-红”
这个用代码怎么写呢?
Private Sub cmdChange_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Dim brush as LinearGradientBrush =CType(me.Resources ("ButtonFace"),LinearGradientBrush) Dim color As Color = brush.GradientStops(0).Color brush.GradientStops(0).Color = brush.GradientStops(2).Color brush.GradientStops(2).Color = color End Sub
先定义一个LinearGradientBrush对象获取名为“ButtonFace”的LinearGradientBrush,然后定义一个Color型的变量获得LinearGradientBrush中第一个GradientStop的颜色,然后把第一个GradientStop的颜色变为第三个GradientStop的颜色,再把第三个GradientStop的颜色设为color的颜色。
color作为一个中转设备,成功地将第一个GradientStop和第三个GradientStop的进行了转换。