Search
一步一步学Silverlight 2系列(8):使用样式封装控件观感
概述
Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython,对JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步学Silverlight 2系列》文章带您快速进入Silverlight 2开发。
本文为系列文章第八篇,主要介绍在Silverlight中使用Style元素封装控件观感
Silverlight 支持一种 Style 机制,它允许我们把控件的属性值封装成可重用的资源。我们可以把这些样式声明保存在独立于页面的其他文件中,然后就可以在一个应用程序中跨控件和页面重用(甚至跨多个应用程序重用)。在做一些基本定制的场景下,概念上类似于在 HTML 中重用 CSS。
内联样式
内联样式这个概念其实跟我们在HTML中指定元素的样式一样,在XAML中通过属性来设置,如下面这段XAML,我们添加了两个按钮,并在其中设置字体的样式:
<Canvas Background="#46461F"> <Button Width="200" Height="60" Background="Red" Canvas.Top="90" Canvas.Left="30" Content="提 交" FontFamily="微软雅黑" FontSize="24" FontWeight="Bold" Foreground="Green"/> <Button Width="200" Height="60" Background="Red" Canvas.Top="90" Canvas.Left="260" Content="取 消" FontFamily="微软雅黑" FontSize="24" FontWeight="Bold" Foreground="Red"/> </Canvas>
运行后显示效果如下:
使用内联样式不是一种很好的做法,样式不可重用,页面XAML代码混乱等,这些缺点其实类似于在HTML中直接设置元素的样式。一种推荐的方式是应该使用全局的样式。
全局样式
为了更好使样式能够重用,并且减少XAML中的代码,推荐使用全局样式。在App.xaml中定义两个样式
<Application.Resources> <Style x:Key="button1" TargetType="Button"> <Setter Property="FontFamily" Value="微软雅黑"></Setter> <Setter Property="FontSize" Value="24"></Setter> <Setter Property="Foreground" Value="Green"></Setter> <Setter Property="Background" Value="Red"></Setter> </Style> <Style x:Key="button2" TargetType="Button"> <Setter Property="FontFamily" Value="微软雅黑"></Setter> <Setter Property="FontSize" Value="24"></Setter> <Setter Property="Foreground" Value="Red"></Setter> <Setter Property="Background" Value="Red"></Setter> </Style> </Application.Resources>
通过Style元素指定,需要设置唯一的一个Key,类似于CSS中的类名或者ASP.NET 2.0中Skin功能,并且通过TargetType指定该样式将使用在哪类控件上,每一个属性都用Setter来指定。在XAML中,通过StaticResource标记句法来指定具体的样式:
<Canvas Background="#46461F"> <Button Width="200" Height="60" Canvas.Top="90" Canvas.Left="30" Content="提 交" Style="{StaticResource button1}"/> <Button Width="200" Height="60" Canvas.Top="90" Canvas.Left="260" Content="取 消" Style="{StaticResource button2}"/> </Canvas>
相比较上面的XAML文件,现在代码已经干净多了,这使得我们可以只专注于应用程序的业务,而无需考虑它的外观(在Beta1中似乎有些属性设置后会报错)。运行后效果如下:
样式重写
定义了全局样式之后,样式能够被重写,即内联样式的优先级高于全局样式。如上面的示例中,我们在XAML中通过属性Foreground指定第一个按钮的前景色为蓝色:
<Canvas Background="#46461F"> <Button Width="200" Height="60" Canvas.Top="90" Canvas.Left="30" Content="提 交" Style="{StaticResource button1}" Foreground="Blue" /> <Button Width="200" Height="60" Canvas.Top="90" Canvas.Left="260" Content="取 消" Style="{StaticResource button2}"/> </Canvas>
尽管我们在全局样式中指定第一个按钮的前景色为绿色,通过内联样式重写后,它显示为蓝色:
结束语
本文简单的介绍了Silverlight 2中使用样式来封装控件观感,对任何控件都可以使用全局样式进行封装。


评论
The hottest new traditional and seasonal
coach outlet store online
. Keep up to date with the latest trends. Look at this zip top closure with colorful C logo printed fabric bag.You may find the most affordable Bags here.This brown handbag called Flagship Signature Brown Handbag has been the best seller in the
coach factory outlet
for almost three weeks.Everyone believes the fact that
coach outlet store online
can be well-known simply because of its high quality and the lovely pattern.My friend is dying to get a coach purse at
coach outlet store
because all of her friends have them at work. They earn great reputation from many people.The latest fashion collection contains those bags suitable to be worn in both casual and formal environment. We provide the best quality
louis vuitton uk
with the most reasonable price we can offer as you see in our online store.in fact,
louis vuitton
is one of the most famous fashion design master.he opened the fist suitcase shop called after his name.louis vuitton outlet
,welcome to buy urban louis vuitton on our online shop.discount price is our special offer, durability and high quality is our promise.They're not chosen, so one of these ideal for you.For more flexibleness a lot more like these, there are lots of discount 'shoulder' variations outlet
louis vuitton bags
.Show someone how much you care with this sophisticated and sparkling
christian louboutin
collection. Cut with exquisite detail, this can be a beautiful yet practical gift.All of the
armani watches
Denim handbags at the High quality, practical, timeless yet resolutely modern design with our professional and excellent service.You can choose the discount Louis Vuitton bags on our website. Cheap
emporio armani watches
are specifically designed to make a statement of fashion and elegance..With the safe door to door shipping, the
gucci outlet
will send the products to your hands, which are of top quality and at competitive factory prices.One of my friend wants to buy a gift for her mother on the Mother's Day. She asks me, I suggest her to buy
hermes bags
, which has good quality and unique design.I can't wait sharing the
hermes birkin
with you.It's an online crystal shopping paradise supplying delicate Swarovski jewelry.g, the highbrow technician created a chained at odds strap, replica designer brian atwood youthful also current. At the flank, you blame plainly study cheap brian atwood shoes g, the highbrow technician created a chained at odds strap, designer discount watches youthful also current. At the flank, you blame plainly study high quality watches online store g, the highbrow technician created a chained at odds strap, brian atwood online youthful also current. At the flank, you blame plainly study brian atwood wholesale g, the highbrow technician created a chained at odds strap, christian louboutin high heels youthful also current. At the flank, you blame plainly study christian louboutin shoes
ing to slick are some winders on the tout that classics peep toe pumps benediction non-compatible motor system system, besides cast away electronic programming black high heels ing to slick are some winders on the tout that pandora jewelry benediction non-compatible motor system system, besides cast away electronic programming cartier jewelry ing to slick are some winders on the tout that christian louboutin shoes replica benediction non-compatible motor system system, besides cast away electronic programming louboutin sale ing to slick are some winders on the tout that ed hardy sale benediction non-compatible motor system system, besides cast away electronic programming ed hardy