Silverlight 苹果风格按钮Mac OS Style 实现的更新版

Silverlight 苹果风格按钮Mac OS Style 实现的更新版,第1张

概述在 Silverlight 2 beta 2 时,我曾经写了一篇帖子实现了苹果风格按钮样式( Silverlight 下苹果 (Mac OS)风格按钮的实现)。 而现在 Silverlight 2 终于发布了,也给之前的代码带来了很多问题,导致无法继续使用。 经过一阵探索,我终于成功修改了这个代码,现在在正式版下也能工作正常了。其关键点如下: 1. 在 beta 2 时,需要采用 <vsm:Se

在 Silverlight 2 beta 2 时,我曾经写了一篇帖子实现了苹果风格的按钮样式( Silverlight 下苹果 (Mac OS)风格按钮的实现)。

而现在 Silverlight 2 终于发布了,也给之前的代码带来了很多问题,导致无法继续使用。

经过一阵探索,我终于成功修改了这个代码,现在在正式版下也能工作正常了。其关键点如下:
1. 在 beta 2 时,需要采用 <vsm:Setter> 和 <vsm:Style> 的语法编写样式。其中 vsm 是个名称空间引用:
xmlns:vsm="clr-namespace:System.windows;assembly=System.windows"
而正式版下直接用 <Style> 和 <Setter> 表签即可。
这个问题如果不修改,程序可以编译通过,但是运行时会报一个莫名其妙的 “ AG_E_PARSER_PROPERTY_NOT_FOUND ”错误。

2. ContentPresenter 和 ContentControl 控件删除了如下属性:
Textdecorations,
textwrapPing,
TextAlignment.
因此,现在必须在 template 中将 ContentPresenter 的定位改用容器 button 的 HorizontalContentAlignment 和 VerticalContentAlignment 属性来控制。代码如下:

< ContentPresenter margin= "4,5,4,4" Content= "{TemplateBinding Content}" ContentTemplate= "{TemplateBinding ContentTemplate}" HorizontalAlignment= "{TemplateBinding HorizontalContentAlignment}" VerticalAlignment= "{TemplateBinding VerticalContentAlignment}" />

修改成功后的 Style 全部代码如下:

< UserControl x: Class= "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" d: DesignWIDth= "640" d: DesignHeight= "480" > < UserControl.Resources > < Style x: Key= "Mybutton" targettype= "button" > < Setter Property= "IsEnabled" Value= "true" /> < Setter Property= "IsTabStop" Value= "true" /> < Setter Property= "Background" Value= "#FF003255" /> < Setter Property= "Foreground" Value= "#FF313131" /> < Setter Property= "MinWIDth" Value= "5" /> < Setter Property= "MinHeight" Value= "5" /> < Setter Property= "margin" Value= "0" /> < Setter Property= "HorizontalContentAlignment" Value= "Center" /> < Setter Property= "VerticalContentAlignment" Value= "Center" /> < Setter Property= "Cursor" Value= "Arrow" /> < Setter Property= "FontSize" Value= "11" /> < Setter Property= "Template" > < Setter.Value > < ControlTemplate targettype= "button" > < GrID > < GrID.Resources > < color x: Key= "linearBevellightStartcolor" >#FFFFFFFF </ color > < color x: Key= "linearBevellightEndcolor" >#F4E2E0E0 </ color > < color x: Key= "linearBevelDarkStartcolor" >#E0E5E5E5 </ color > < color x: Key= "linearBevelDarkEndcolor" >#B2FFFFFF </ color > < color x: Key= "MouSEOverlinearBevelDarkEndcolor" >#7FFC1717 </ color > < color x: Key= "HoverlinearBevellightStartcolor" >#FCFFFFFF </ color > < color x: Key= "HoverlinearBevellightEndcolor" >#EAFFFFFF </ color > < color x: Key= "HoverlinearBevelDarkStartcolor" >#D8FFFFFF </ color > < color x: Key= "HoverlinearBevelDarkEndcolor" >#4CFFFFFF </ color > < color x: Key= "CurvedBevelFillStartcolor" >#B3FFFFFF </ color > < color x: Key= "CurvedBevelFillEndcolor" >#3CFFFFFF </ color > < SolIDcolorBrush x: Key= "borderBrush" color= "#FF5E5E5E" /> < SolIDcolorBrush x: Key= "AccentBrush" color= "#FF000000" /> < SolIDcolorBrush x: Key= "DisabledBrush" color= "#A5FFFFFF" /> < linearGradIEntBrush x: Key= "FocusedstrokeBrush" EndPoint= "0.5,1" StartPoint= "0.5,0" > < GradIEntStop color= "#B2FFFFFF" Offset= "0" /> < GradIEntStop color= "#51FFFFFF" Offset= "1" /> < GradIEntStop color= "#66FFFFFF" Offset= "0.325" /> < GradIEntStop color= "#1EFFFFFF" Offset= "0.325" /> </ linearGradIEntBrush > </ GrID.Resources > < visualstatemanager.VisualStateGroups > < VisualStateGroup x: name= "CommonStates" > < VisualStateGroup.Transitions > < VisualTransition GeneratedDuration= "00:00:00.2000000" To= "MouSEOver" /> < VisualTransition GeneratedDuration= "0:0:0.1" To= "pressed" /> < VisualTransition GeneratedDuration= "00:00:00.2000000" From= "normal" To= "MouSEOver" /> </ VisualStateGroup.Transitions > < VisualState x: name= "normal" > < Storyboard /> </ VisualState > < VisualState x: name= "MouSEOver" > < Storyboard > < colorAnimationUsingKeyFrames Duration= "0" Storyboard. Targetname= "BackgroundGradIEnt" Storyboard. TargetProperty= "(Shape.Fill).(GradIEntBrush.GradIEntStops)[0].(GradIEntStop.color)" > < SplinecolorKeyFrame KeyTime= "0" Value= "#c8d5ed" /> </ colorAnimationUsingKeyFrames > < colorAnimationUsingKeyFrames Duration= "0" Storyboard. Targetname= "BackgroundGradIEnt" Storyboard. TargetProperty= "(Shape.Fill).(GradIEntBrush.GradIEntStops)[1].(GradIEntStop.color)" > < SplinecolorKeyFrame KeyTime= "0" Value= "#97c2ee" /> </ colorAnimationUsingKeyFrames > < colorAnimationUsingKeyFrames Duration= "0" Storyboard. Targetname= "BackgroundGradIEnt" Storyboard. TargetProperty= "(Shape.Fill).(GradIEntBrush.GradIEntStops)[2].(GradIEntStop.color)" > < SplinecolorKeyFrame KeyTime= "0" Value= "#6eadee" /> </ colorAnimationUsingKeyFrames > < colorAnimationUsingKeyFrames Duration= "0" Storyboard. Targetname= "BackgroundGradIEnt" Storyboard. TargetProperty= "(Shape.Fill).(GradIEntBrush.GradIEntStops)[3].(GradIEntStop.color)" > < SplinecolorKeyFrame KeyTime= "0" Value= "#aff9ff" /> </ colorAnimationUsingKeyFrames > </ Storyboard > </ VisualState > < VisualState x: name= "pressed" > < Storyboard > < DoubleAnimationUsingKeyFrames Duration= "0" Storyboard. Targetname= "BackgroundGradIEnt" Storyboard. TargetProperty= "(Shape.Fill).(GradIEntBrush.GradIEntStops)[1].(GradIEntStop.Offset)" > < SplineDoubleKeyFrame KeyTime= "0" Value= ".2" /> </ DoubleAnimationUsingKeyFrames > < colorAnimationUsingKeyFrames Duration= "0" Storyboard. Targetname= "BackgroundGradIEnt" Storyboard. TargetProperty= "(Shape.Fill).(GradIEntBrush.GradIEntStops)[0].(GradIEntStop.color)" > < SplinecolorKeyFrame KeyTime= "0" Value= "#bac5e8" /> </ colorAnimationUsingKeyFrames > < colorAnimationUsingKeyFrames Duration= "0" Storyboard. Targetname= "BackgroundGradIEnt" Storyboard. TargetProperty= "(Shape.Fill).(GradIEntBrush.GradIEntStops)[1].(GradIEntStop.color)" > < SplinecolorKeyFrame KeyTime= "0" Value= "#7bb2e9" /> </ colorAnimationUsingKeyFrames > < colorAnimationUsingKeyFrames Duration= "0" Storyboard. Targetname= "BackgroundGradIEnt" Storyboard. TargetProperty= "(Shape.Fill).(GradIEntBrush.GradIEntStops)[2].(GradIEntStop.color)" > < SplinecolorKeyFrame KeyTime= "0" Value= "#4d9ae7" /> </ colorAnimationUsingKeyFrames > < colorAnimationUsingKeyFrames Duration= "0" Storyboard. Targetname= "BackgroundGradIEnt" Storyboard. TargetProperty= "(Shape.Fill).(GradIEntBrush.GradIEntStops)[3].(GradIEntStop.color)" > < SplinecolorKeyFrame KeyTime= "0" Value= "#85eaff" /> </ colorAnimationUsingKeyFrames > </ Storyboard > </ VisualState > < VisualState x: name= "Disabled" > < Storyboard > < DoubleAnimationUsingKeyFrames Duration= "0" Storyboard. Targetname= "DisabledVisual" Storyboard. TargetProperty= "Opacity" > < SplineDoubleKeyFrame KeyTime= "0" Value= "1" /> </ DoubleAnimationUsingKeyFrames > </ Storyboard > </ VisualState > </ VisualStateGroup > < VisualStateGroup x: name= "Focusstates" > < VisualState x: name= "Focused" > < Storyboard > < ObjectAnimationUsingKeyFrames Duration= "0" Storyboard. Targetname= "FocusVisual" Storyboard. TargetProperty= "Visibility" > < discreteObjectKeyFrame KeyTime= "0" > < discreteObjectKeyFrame.Value > < Visibility >Visible </ Visibility > </ discreteObjectKeyFrame.Value > </ discreteObjectKeyFrame > </ ObjectAnimationUsingKeyFrames > </ Storyboard > </ VisualState > < VisualState x: name= "Unfocused" > < Storyboard /> </ VisualState > </ VisualStateGroup > </ visualstatemanager.VisualStateGroups > < Rectangle x: name= "Background" Fill= "{TemplateBinding Background}" RadiusX= "11" RadiusY= "11" /> < Rectangle x: name= "BackgroundGradIEnt" stroke= "{StaticResource borderBrush}" strokeThickness= "1" RadiusX= "11" RadiusY= "11" margin= "-1,-1,-1" > < Rectangle.Fill > < linearGradIEntBrush EndPoint= "0.7,1" StartPoint= "0.7,0" > < GradIEntStop color= "{StaticResource linearBevellightStartcolor}" Offset= "0" /> < GradIEntStop color= "{StaticResource linearBevellightEndcolor}" Offset= "0.326" /> < GradIEntStop color= "{StaticResource linearBevelDarkStartcolor}" Offset= "0.344" /> < GradIEntStop color= "#FFFFFFFF" Offset= "0.786" /> </ linearGradIEntBrush > </ Rectangle.Fill > </ Rectangle > < GrID x: name= "FocusVisual" Visibility= "Collapsed" > < Rectangle margin= "-2,1,-2,-2" stroke= "{StaticResource AccentBrush}" strokeThickness= "1" strokeDashArray= "1.5 1.5" RadiusX= "3" RadiusY= "3" /> </ GrID > < ContentPresenter margin= "4,4" Content= "{TemplateBinding Content}" ContentTemplate= "{TemplateBinding ContentTemplate}" HorizontalAlignment= "{TemplateBinding HorizontalContentAlignment}" VerticalAlignment= "{TemplateBinding VerticalContentAlignment}" /> < Rectangle x: name= "DisabledVisual" IsHitTestVisible= "false" Opacity= "0" Fill= "{StaticResource DisabledBrush}" RadiusX= "11" RadiusY= "11" margin= "-1,-1" /> </ GrID > </ ControlTemplate > </ Setter.Value > </ Setter > </ Style > </ UserControl.Resources > < GrID x: name= "LayoutRoot" Background= "White" > < button Height= "35" HorizontalAlignment= "Stretch" margin= "198,132,262,0" Style= "{StaticResource Mybutton}" VerticalAlignment= "top" Content= "button" /> </ GrID > </ UserControl > 引用:http://msdn.microsoft.com/zh-cn/library/dd391775.aspx 总结

以上是内存溢出为你收集整理的Silverlight 苹果风格按钮Mac OS Style 实现的更新版全部内容,希望文章能够帮你解决Silverlight 苹果风格按钮Mac OS Style 实现的更新版所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1072710.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存