• silverlight 之 – Blend 之 LinearGradientBrush (二)


    ----------------------------------------------------------------

    关于再画一个的问题

    1.肯定想到的就是拷贝;

    》不能每次用都拷贝,很麻烦

    》现在我们画的是矩形(Rectangle),下次可能是 Border,Grid,StackPanel

    所以拷贝不行;

    2.把这些颜色都记下来

    》太麻烦,这也不行

    ----------------------------------------------------------------

    正题:

    1. 下面我们来介绍下画布界面:

    截图04

    看到右上角有三个标志,分别是: 设计模式,代码模式,拆分模式

    2.进入代码模式看看我们之前的代码:

    <UserControl
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    	mc:Ignorable="d"
    	x:Class="StyleTest.ColorCard"
    	d:DesignWidth="640" d:DesignHeight="480">
    
    	<Grid x:Name="LayoutRoot" Background="#FF6A6A6A">
    		<Rectangle Stroke="Black" Height="43" VerticalAlignment="Top">
    			<Rectangle.Fill>
    				<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
    					<GradientStop Color="#FF606060" Offset="0"/>
    					<GradientStop Color="Black" Offset="1"/>
    					<GradientStop Color="#FE333333" Offset="0.487"/>
    					<GradientStop Color="#FE161515" Offset="0.574"/>
    				</LinearGradientBrush>
    			</Rectangle.Fill>
    		</Rectangle>	
    	</Grid>	
    </UserControl>

    下面我们要复用这些代码中关于颜色填充的部分;

    3. 在项目上点击右键》添加新项》资源字典; 我把他命名为: Style.xaml

    4. 打开Style.xaml,显示无法再设计视图中编辑,点击切换到代码模式;默认代码如下:

    <ResourceDictionary
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    	<!-- 应该在此定义资源字典条目。-->
    </ResourceDictionary>

    5. 把第二步中的:

    	<LinearGradientBrush   EndPoint="0.5,1" StartPoint="0.5,0">
    					<GradientStop Color="#FF606060" Offset="0"/>
    					<GradientStop Color="Black" Offset="1"/>
    					<GradientStop Color="#FE333333" Offset="0.487"/>
    					<GradientStop Color="#FE161515" Offset="0.574"/>
    	</LinearGradientBrush>

    》 拷贝到 Style.xaml 的 “<!-- 应该在此定义资源字典条目。—> ” 下面;

    》 程序会提示错误

    》 给他取个名字例如:  <LinearGradientBrush x:Key="AppleStyle1"  就好了;

    这样我们就把这个填充样式加到字典里面了:

    <ResourceDictionary
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    	<!-- 应该在此定义资源字典条目。-->
    	
    	<LinearGradientBrush x:Key="AppleStyle1" EndPoint="0.5,1" StartPoint="0.5,0">
    					<GradientStop Color="#FF606060" Offset="0"/>
    					<GradientStop Color="Black" Offset="1"/>
    					<GradientStop Color="#FE333333" Offset="0.487"/>
    					<GradientStop Color="#FE161515" Offset="0.574"/>
    	</LinearGradientBrush>
    
    	
    </ResourceDictionary>

    6. 回到ColorCard.xaml 页面;打开“库” 看到我们刚才的字典了!!!

    截图05

    我们用他来填充我们刚才定义的矩形, 并且再复制一个效果如下:

    截图06

    代码如下:

    	<Grid x:Name="LayoutRoot" Background="#FF6A6A6A">
    		<Rectangle Stroke="Black" Height="43" VerticalAlignment="Top" Fill="{StaticResource AppleStyle1}"/>
    		<Rectangle Stroke="Black" Height="43" VerticalAlignment="Bottom" Fill="{StaticResource AppleStyle1}"/>	
    	</Grid>	

    看看这代码是不是简洁多了!

    说到这里你肯定知道:

    》 以后画别的东西,就直接到”库“去选择就可以填充这个样式了;(实现了复用)

    》 修改的时候只有修改 Style.xaml 里面的代码,这边就也更新了;(方便更新调整)

    那么今天的主角 LinearGradientBrush 就和大家见面了!

    silverlight 之 – Blend 一切源于Brush(一)

    silverlight 之 – Blend 之 LinearGradientBrush (二)

    silverlight 之 – Blend 之 Style for Button (三)

    silverlight 之 – Blend 之图形按钮(四)

  • 相关阅读:
    VB.net和c#利用IOleCommandTarget接口实现Html编辑器的Glyphs(原创)
    vb.net中用com通讯实现对HTMLDocument的事件捕捉(原创)
    自己写的用JS脚本定时执行Web地址并且得到它的源代码的页面程序
    Alice:曾经的CS反恐精英顶尖高手
    .net利用IOleCommandTarget接口实现Html编辑器表格边框的显示和隐藏(原创)
    ASP用语法及函数
    最近在研究质量统计与分析方面的项目开发
    propertyGrid实现选项(原创)
    .Net平台AOP技术研究
    c# propertyGrid属性控件跟Htmlelement元素绑定(原创精华)
  • 原文地址:https://www.cnblogs.com/zhanxp/p/1681789.html
Copyright © 2020-2023  润新知