##概述 Button是各类手机平台中最重要的控件之一,咱们与系统的大部分交互都会经过按钮进行,在Android和iOS中都提供了很方便的按钮自定义方式,Android能够采用selector背景选择器进行按钮状态控制,iOS中则更简单,提供了Default,Highlight,Selected,Disable四种状态进行设置,而且能够进行背景、图片、标题,整个内容框的自定义,在WindowsPhone中则相对要复杂一些,由于Button在默认的属性中并无直接提供与状态相关的属性进行设置,可是提供了样式的方式进行修改。字体
###分析Button默认Style 下面是Windows Phone 8 按钮的默认样式 前面几个属性都是常见的属性,Background(按钮背景),BorderBrush(按钮边框),Foreground(前景),BorderThickness(边框粗细), FontFamily(字体样式),FontSize(字体大小),Padding(间距).动画
在Template的设置里首先设置了按钮控件的可视状态,在Windows Phone里按钮状态分为四种,Normal 默认状态,MouseOver 鼠标指针悬停在控件上,Pressed 控件已按下,Disabled 控件被禁用,都是隶属于CommonStates的VisualStateGroup。spa
在VisualState 包含 Storyboard,用于更改 ControlTemplate 中的元素的外观,在控件进入VisualState指定的状态时,Storyboard开始,控件退出状态时Storyboard结束,这个特性能够给按钮的状态加入一些动画等特殊效果。 在VisualStateGroups后设置的是内容模板,这里主要控制按钮的外观样式,好比将按钮修改为圆形,或者其余形状。指针
<!-- lang: xml --> <Style x:Key="ButtonStyle1" TargetType="Button"> <Setter Property="Background" Value="Transparent" /> <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}" /> <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}" /> <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}" /> <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}" /> <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}" /> <Setter Property="Padding" Value="10,5,10,6" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Grid Background="Transparent"> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal" /> <VisualState x:Name="MouseOver" /> <VisualState x:Name="Pressed"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneButtonBasePressedForegroundBrush}" /> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}" /> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="Disabled"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}" /> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}" /> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" /> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}"> <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" /> </Border> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
###圆形按钮设置code