首先先來認識一下ApplicationBar中的圖標:
與WP7的ApplicationBar的圖標樣式很像,但是,他們的實現方式卻是不同的。WP7的ApplicationBar的按鈕圖標來源於固定大小的背景透明的圖片,而在Win8中,盡管也可以按照WP7的方式自定義Button,但是考慮到各種設備的屏幕分辨率不同(WP7開發時主要只需要考慮800*480),用圖片無法完美適配,所以使用了Segoe UI Symbol字體來對特定的Unicode字符進行繪制。
在Windows 8應用商店APP開發時,新建一個APP默認會在common/StandardStyles.xaml文件內加入各種通用的樣式,其中ApplicationBar的樣式也被包含在內,如下:

<Style x:Key="AppBarButtonStyle" TargetType="Button"> <Setter Property="Foreground" Value="{StaticResource AppBarItemForegroundThemeBrush}"/> <Setter Property="VerticalAlignment" Value="Stretch"/> <Setter Property="FontFamily" Value="Segoe UI Symbol"/> <Setter Property="FontWeight" Value="Normal"/> <Setter Property="FontSize" Value="20"/> <Setter Property="AutomationProperties.ItemType" Value="App Bar Button"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Grid Width="100" Background="Transparent"> <StackPanel VerticalAlignment="Top" Margin="0,14,0,13"> <Grid Width="40" Height="40" Margin="0,0,0,5" HorizontalAlignment="Center"> <TextBlock x:Name="BackgroundGlyph" Text="" FontFamily="Segoe UI Symbol" FontSize="53.333" Margin="-4,-19,0,0" Foreground="{StaticResource AppBarItemBackgroundThemeBrush}"/> <TextBlock x:Name="OutlineGlyph" Text="" FontFamily="Segoe UI Symbol" FontSize="53.333" Margin="-4,-19,0,0"/> <ContentPresenter x:Name="Content" HorizontalAlignment="Center" Margin="-1,-1,0,0" VerticalAlignment="Center"/> </Grid> <TextBlock x:Name="TextLabel" Text="{TemplateBinding AutomationProperties.Name}" Margin="0,0,2,0" FontSize="12" TextAlignment="Center" Width="88" MaxHeight="32" TextTrimming="WordEllipsis" Style="{StaticResource BasicTextStyle}"/> </StackPanel> <Rectangle x:Name="FocusVisualWhite" IsHitTestVisible="False" Stroke="{StaticResource FocusVisualWhiteStrokeThemeBrush}" StrokeEndLineCap="Square" StrokeDashArray="1,1" Opacity="0" StrokeDashOffset="1.5"/> <Rectangle x:Name="FocusVisualBlack" IsHitTestVisible="False" Stroke="{StaticResource FocusVisualBlackStrokeThemeBrush}" StrokeEndLineCap="Square" StrokeDashArray="1,1" Opacity="0" StrokeDashOffset="0.5"/> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal"/> <VisualState x:Name="PointerOver"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGlyph" Storyboard.TargetProperty="Foreground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource AppBarItemPointerOverBackgroundThemeBrush}"/> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Content" Storyboard.TargetProperty="Foreground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource AppBarItemPointerOverForegroundThemeBrush}"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="Pressed"> <Storyboard> <DoubleAnimation Storyboard.TargetName="OutlineGlyph" Storyboard.TargetProperty="Opacity" To="0" Duration="0"/> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGlyph" Storyboard.TargetProperty="Foreground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource AppBarItemForegroundThemeBrush}"/> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Content" Storyboard.TargetProperty="Foreground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource AppBarItemPressedForegroundThemeBrush}"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="Disabled"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="OutlineGlyph" Storyboard.TargetProperty="Foreground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource AppBarItemDisabledForegroundThemeBrush}"/> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Content" Storyboard.TargetProperty="Foreground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource AppBarItemDisabledForegroundThemeBrush}"/> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextLabel" Storyboard.TargetProperty="Foreground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource AppBarItemDisabledForegroundThemeBrush}"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> <VisualStateGroup x:Name="FocusStates"> <VisualState x:Name="Focused"> <Storyboard> <DoubleAnimation Storyboard.TargetName="FocusVisualWhite" Storyboard.TargetProperty="Opacity" To="1" Duration="0"/> <DoubleAnimation Storyboard.TargetName="FocusVisualBlack" Storyboard.TargetProperty="Opacity" To="1" Duration="0"/> </Storyboard> </VisualState> <VisualState x:Name="Unfocused" /> <VisualState x:Name="PointerFocused" /> </VisualStateGroup> </VisualStateManager.VisualStateGroups> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
可以注意到,在AppBarButtonStyle下面,已經默認創建了一些常用的樣式,每個樣式的區別就是Content的Value的值。
<Style x:Key="DeleteAppBarButtonStyle" TargetType="Button" BasedOn="{StaticResource AppBarButtonStyle}"> <Setter Property="AutomationProperties.AutomationId" Value="DeleteAppBarButton"/> <Setter Property="AutomationProperties.Name" Value="Delete"/> <Setter Property="Content" Value=""/> </Style>
觀察一下不難發現,所有的這些Content Value全是Unicode字符串。這些都很簡單。在之前的開發中,我遇到的一個問題就是默認創建的這些Style太少滿足不了整個App的需求,需要一些其他的圖標如傾斜、下划線等等,可是,從哪兒去找這些圖標對應的Unicode編碼呢?
按快捷鍵Win+R,輸入charmap.exe,打開字符映射表,將字體選擇為Segoe UI Symbol,然后將內容的滾動條滑動到偏下的位置(常用的圖標為E開頭的),如圖,選擇需要的圖標,在窗口左下角就會顯示出該圖標對應的Unicode編碼。
錦燕雲 微博 http://www.weibo.com/jinyanyun http://www.cnblogs.com/vistach/archive/2012/10/24/Windows8_Win8_WinRT_MetroStyleApps_ModernUIApps_ApplicationBar_Icon_SegoeUISymbol.html