【问题标题】:Why are my AppBarButton(s) inheriting margin from a TextBlock style?为什么我的 AppBarButton(s) 从 TextBlock 样式继承边距?
【发布时间】:2016-04-13 17:41:45
【问题描述】:

我正在开发一个 Windows 8 商店桌面应用程序,我在 BottomAppBar 中的 AppBarButton(s) 遇到了一个奇怪的问题。

这是我的 BottomAppBar 的代码,只是为了表明那里的 xaml 中没有设置任何内容:

<Page.BottomAppBar>
        <CommandBar Background="SlateGray">
            <AppBarButton Label="UseGPS"
                          Icon="Target"/>
            <AppBarButton Label="Reset"
                          Icon="Clear"/>
            <AppBarButton Label="Save to File"
                          Icon="Save" />
            <AppBarSeparator />
            <AppBarButton Label="Copy Longitude"
                          Icon="Copy"/>
            <AppBarButton Label="Copy Latitude"
                          Icon="Copy" />
        </CommandBar>
    </Page.BottomAppBar>

真正的问题在于我的 Generic.Xaml 资源字典中的这个 xaml 代码:

<Style TargetType="TextBlock">
        <Setter Property="VerticalAlignment"
                Value="Center" />
        <Setter Property="FontFamily" 
                Value="Segoe UI" />
        <Setter Property="FontSize" 
                Value="26" />
        <Setter Property="Foreground" 
                Value="{StaticResource ApplicationForegroundThemeBrush}" />
        <Setter Property="Margin"
                Value="10,0" />
    </Style>

特别是这一行:&lt;Setter Property="Margin" Value="10,0" /&gt;

使用此代码,按钮的图标会被剪掉:

当该行被注释掉时,我会看到像这样的正常图标:

我尝试为 CommandBar 和 AppBarButton 设置Style={x:Null},我也尝试为两者设置Margin=0,但都没有解决问题。我不明白为什么以 TextBlock 为目标的样式会导致图标在 AppBarButton 中剪辑。我不能只注释样式的 setter,因为 TextBlocks 实际上需要它,所以如果有人有不涉及删除该 setter 的解决方案,我将非常感激。

【问题讨论】:

  • 你的图标是字体吗?
  • @ChrisW。我的图标是 xaml 提供的内置图标

标签: xaml winrt-xaml


【解决方案1】:

所以原因是,因为图标实际上只是来自 SymbolIcon classglyph's,它使用 Segoe MDL2 资产 font 并在运行时呈现为 TextBlock。因此继承自 TargetType。

您可以轻松地在实例中关闭该继承;

<CommandBar Background="SlateGray">
   <CommandBar.Resources>
      <Style TargetType="TextBlock">
         <Setter Property="Margin" Value="0"/>
      </Style>
   </CommandBar.Resources>
            <AppBarButton Label="UseGPS"
                          Icon="Target"/>
            <AppBarButton Label="Reset"
                          Icon="Clear"/>
            <AppBarButton Label="Save to File"
                          Icon="Save" />
            <AppBarSeparator />
            <AppBarButton Label="Copy Longitude"
                          Icon="Copy"/>
            <AppBarButton Label="Copy Latitude"
                          Icon="Copy" />
</CommandBar>

希望这会有所帮助,干杯。

【讨论】:

  • 您的修复工作完美,您还解释了为什么会发生,谢谢!
  • @GordonAllocman 嘿,前几天我从你的一个答案中学到了一些东西,所以它只是互惠的。 :)
猜你喜欢
  • 2014-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-27
  • 2014-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多