无法更改 UWP 中 TeachingTip 的背景颜色

Unable to change the background colour of TeachingTip in UWP

我在UWP中使用WinUi 2.4的教学提示控件,想改变背景颜色。但是我在 xaml 中给出的背景值并没有反映在控件中。

 <winui:TeachingTip x:Name="ToggleThemeTeachingTip2"
  Title="Change themes without hassle"
  Subtitle="It's easier than ever to see control samples in both light and dark theme!"
  PreferredPlacement="Auto"
PlacementMargin="20"                    
IsLightDismissEnabled="True"
  Background="Red"
   >

这不是很好笑吗?因为当您将 light dismiss 设置为 false 时它会起作用。

原因在这里:https://github.com/microsoft/microsoft-ui-xaml/blob/master/dev/TeachingTip/TeachingTip.xaml#L32

看到在 LightDismiss 状态下它强制主题值。

因此您需要复制并编辑该样式以遵守 TemplateBinding(您在控件上设置的红色)

<VisualState x:Name="LightDismiss">
    <VisualState.Setters>
        <Setter Target="TailEdgeBorder.Background" Value="{Binding Background,RelativeSource={RelativeSource TemplatedParent}}"/>
        <Setter Target="TailPolygon.Fill" Value="{Binding Background,RelativeSource={RelativeSource TemplatedParent}}"/>
        <Setter Target="TopTailPolygonHighlight.Fill" Value="{Binding Background,RelativeSource={RelativeSource TemplatedParent}}"/>
        <Setter Target="ContentRootGrid.Background" Value="{Binding Background,RelativeSource={RelativeSource TemplatedParent}}"/>
    </VisualState.Setters>
</VisualState>

我让你更容易分享完整的 App.xaml 文件和一些命名空间编辑等等:

https://pastebin.com/QgPpqMKS

换个x:class就行了,我的是App9。另请注意,我用 CornerRadius 注释了这一行,因为它会抛出一些错误 (??)。

也为您的教学提示添加风格:

 <winui:TeachingTip x:Name="ToggleThemeTeachingTip2"
  Title="Change themes without hassle"
  Subtitle="It's easier than ever to see control samples in both light and dark theme!"
  PreferredPlacement="Auto"
PlacementMargin="20"                    
IsLightDismissEnabled="True"
Style="{StaticResource MyTeachingTipStyle}"
  Background="Red"
   >