如何将CommandBar的AppBarButton图标改为微软应用样式?
嘿,我完全懂你想要的那种质感——微软日历、邮件这类应用的AppBar图标确实是更大尺寸、纤细流畅的线条,还有那种纯净的亮白色,和默认的AppBarButton风格差别挺明显的。你试过的资源文件、字号调整没起效,大概率是没触碰到Icon元素的核心配置,咱们来一步步搞定它:
核心问题分析
默认的AppBarButton图标存在几个和原生风格不符的点,咱们需要针对性调整:
- 默认图标尺寸偏小(通常20px左右),显得紧凑
- 前景色是偏灰的白色,不够鲜明
- 若未指定正确字体,线条质感会偏离微软原生风格
自定义AppBarButton样式
把这个样式加到你的Page.Resources里,替换原来的AppBarButtonStyle1:
<Page.Resources> <Style x:Key="MicrosoftStyleAppBarButton" TargetType="AppBarButton"> <Setter Property="Foreground" Value="#FFFFFF"/> <Setter Property="FontFamily" Value="Segoe MDL2 Assets"/> <Setter Property="Width" Value="48"/> <Setter Property="Height" Value="48"/> <Setter Property="Padding" Value="0"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="AppBarButton"> <Grid x:Name="Root" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"> <VisualStateManager.VisualStateGroups> <!-- 可从系统默认AppBarButton样式中复制点击、悬停等视觉状态,保证交互体验一致 --> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal"/> <VisualState x:Name="PointerOver"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Root" Storyboard.TargetProperty="Background"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListLowBrush}"/> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Icon" Storyboard.TargetProperty="Foreground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="Pressed"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Root" Storyboard.TargetProperty="Background"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListMediumBrush}"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> <FontIcon x:Name="Icon" Glyph="{TemplateBinding Icon}" Width="28" Height="28" FontSize="28" Foreground="{TemplateBinding Foreground}"/> <TextBlock x:Name="Label" Text="{TemplateBinding Label}" FontSize="12" Margin="0,4,0,0" HorizontalAlignment="Center" Foreground="{TemplateBinding Foreground}"/> </StackPanel> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </Page.Resources>
修改你的CommandBar代码
现在把所有AppBarButton的样式换成自定义的,同时纠正你代码里的Label笔误:
<CommandBar Height="40" OverflowButtonVisibility="Collapsed" Background="Transparent" IsOpen="True" DefaultLabelPosition="Right"> <CommandBar.Content> <TextBlock Text="X" FontFamily="Segoe UI" Padding="10,0,0,0"/> </CommandBar.Content> <AppBarButton Style="{StaticResource MicrosoftStyleAppBarButton}" Icon="Save" Label="Save"/> <AppBarButton Style="{StaticResource MicrosoftStyleAppBarButton}" Icon="Delete" Label="Delete"/> <AppBarButton Style="{StaticResource MicrosoftStyleAppBarButton}" Icon="Save" Label="Save"/> <AppBarButton Style="{StaticResource MicrosoftStyleAppBarButton}" Icon="Delete" Label="Delete"/> </CommandBar>
关键细节说明
- 图标尺寸:我这里设了
Width="28" Height="28"和FontSize="28",你可以根据需求调整到24或30,只要三者保持一致,越大线条越显得纤细流畅 - 字体选择:
Segoe MDL2 Assets是微软原生应用图标统一使用的字体,能保证线条质感完全匹配 - 视觉状态:模板里保留了基础的悬停、点击效果,你可以根据需求补充更多状态,比如禁用态
- 文本字体:把原来的
SEGOE改成Segoe UI,和微软应用的文本风格统一
这样调整后,你的AppBarButton图标应该就能和日历、邮件这类应用的风格完全匹配了!
内容的提问来源于stack exchange,提问作者rubStackOverflow
相关产品推荐
相关产品推荐

