【问题标题】:Style reference works on UWP but not on Android or Wasm样式参考适用于 UWP,但不适用于 Android 或 Wasm
【发布时间】:2026-02-20 20:55:01
【问题描述】:

我已将 Style 定义为 UserControl 的资源。

...
<UserControl.Resources>
  <Style x:Key="BottomBarButton" TargetType="Button">
    <Setter Target="Background" Value="Transparent"/>
      <Setter Target="BorderThickness" Value="1"/>
      <Setter Target="HorizontalAlignment" Value="Stretch"/>
      <Setter Target="VerticalAlignment" Value="Stretch"/>
    </Style>
</UserControl.Resources>
...

它被使用了

...
<Button Grid.Column="0" Style="{StaticResource BottomBarButton}">
  ...
</Button>
<Button Grid.Column="1" Style="{StaticResource BottomBarButton}">
  ...
</Button>
...

这适用于 UWP,但该样式不适用于 Android 或 Wasm。这些是唯一经过测试的平台

【问题讨论】:

  • 您能指出您使用的是哪个版本的 Uno.UI 包吗?
  • @JérômeLaban Uno 2.4.0

标签: xaml uno-platform


【解决方案1】:

必须使用Property 而不是Target

<Style x:Key="BottomBarButton" TargetType="Button">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="HorizontalAlignment" Value="Stretch"/>
    <Setter Property="VerticalAlignment" Value="Stretch"/>
</Style>

【讨论】:

    【解决方案2】:

    您已经定义了名为“ButtonStyle”的样式,但引用了一个名为“BottomBarButton”的样式(我猜它是在别处定义的)。

    试过了吗?

    <Button Grid.Column="0" Style="{StaticResource ButtonStyle}">
      ...
    </Button>
    <Button Grid.Column="1" Style="{StaticResource ButtonStyle}">
      ...
    </Button>
    

    【讨论】:

    • 这是我快速拼凑的示例的一个很好的捕捉,但不幸的是,这是我的问题中的一个错误,而不是我编写的实际代码。
    最近更新 更多