4

我正在尝试使用某些特定样式设置我的 UWP 应用程序的样式,而在其他平台上它应该保持默认设置。

这是我的项目布局:

在此处输入图像描述

我尝试了以下事情:在Clients.Shared创建如下样式时:

<Style x:Key="SomeStyle" TargetType="Button" />

并在其中添加相同的 Style 键,Clients.Themes.UWP因此它有望覆盖它,但没有运气。

然后我尝试了使用虚拟风格并使用,onPlatform但这也不起作用,但我仍然认为这是要走的路。我有以下代码:

<Style x:Key="DummyStyle" TargetType="Button">
    <Setter Property="Style">
        <Setter.Value>
            <OnPlatform x:Key="ButtonStyle" x:TypeArguments="Style">
                <On Platform="UWP" Value="{StaticResource SomeStyle}"></On>
            </OnPlatform>
        </Setter.Value>
    </Setter>
</Style>

我尝试搞乱合并ResourceDictionary.MergedDictionaries,但我无法包含 xaml

有人有任何线索吗?

4

3 回答 3

2

尝试这个:

<OnPlatform x:TypeArguments="Color" Android="Green" iOS="White" WinPhone="White"
        x:Key="PrimaryColor" />


<Style x:Key="ButtonColor" TargetType="Button">
    <Setter Property="BackgroundColor" Value="{StaticResource PrimaryColor}" />
</Style>

<Button Text="Hello" HorizontalOptions="StartAndExpand"
   Grid.Row="2" Grid.ColumnSpan="2" Style="{StaticResource ButtonColor}" />

在这个例子中,我定义了一个名为 Button 的样式ButtonColor。此按钮将BackgroundColor在所有平台上使用白色,除了 Android,它将为绿色。

我发现这是该标签在样式方面最常见的用途;如果您正在使用字体,请确保在模拟器和实际设备上运行以获得您想要的字体。

于 2018-01-19T01:13:12.383 回答
0

这种语法对我有用:

<Style x:Key="zoneLabelStyle" TargetType="Label">
  <Setter Property="HeightRequest" Value="18" />
  <Setter Property="Margin">
    <OnPlatform x:TypeArguments="Thickness">
      <On Platform="Android" Value="0, -3, 0, 0" />
    </OnPlatform>
  </Setter>
</Style>
于 2022-03-03T14:04:25.197 回答
0

为什么不使用

<Button.Style>
    <OnPlatform x:TypeArguments="x:Style">
        <OnPlatform.iOS>...</OnPlatform.iOS>
        <OnPlatform.Android>...</OnPlatform.Android>
        <OnPlatform.WinPhone>...</OnPlatform.WinPhone>
    </OnPlatform>
</Button.Style>

希望它会有所帮助。

于 2018-01-18T10:50:49.470 回答