【问题标题】:Xamarin.Forms setting specific style for platformXamarin.Forms 为平台设置特定样式
【发布时间】:2018-01-18 09:30:39
【问题描述】:

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

这是我的项目布局:

我尝试了以下方法: 在Clients.Shared 中创建如下样式:

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

并在 Clients.Themes.UWP 中添加相同的 Style 键,以便它可以覆盖它,但没有运气。

然后我尝试使用虚拟样式并使用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

有人知道吗?

【问题讨论】:

    标签: xaml xamarin.forms themes xamarin.forms-styles


    【解决方案1】:

    试试这个:

    <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}" />
    

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

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

    【讨论】:

    • 那只有颜色,我想要完整的样式
    • Visual Studio 将OnPlatform 上的Android 等属性标记为已过时。知道替换是什么吗?
    • @Savage - 在 xaml 中,有一种更新的“嵌套”语法,可以干净地处理其他平台。寻找&lt;OnPlatform&gt;&lt;&lt;On Platform="Android" Value=.../&gt;&lt;/OnPlatform&gt; 的讨论。在 C# 中,请改用switch (Device.RuntimePlatform)
    【解决方案2】:

    为什么不使用

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

    希望它会有所帮助。

    【讨论】:

    • 问题是我无法加载样式:)
    【解决方案3】:

    这个语法对我有用:

    <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>
    

    【讨论】:

      猜你喜欢
      • 2016-11-20
      • 1970-01-01
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多