【问题标题】:Is there an equivalent of iOS's Dynamic System Colors in Xamarin forms?Xamarin 表单中是否有相当于 iOS 的动态系统颜色?
【发布时间】:2021-01-20 10:35:51
【问题描述】:

在开发 Xamarin.iOS 应用程序时,我能够为 backgroundColors 指定动态系统颜色以自动调整明暗模式。

例如:

if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
{
    goalsCard.BackgroundColor = UIColor.SystemBackgroundColor;
}
else
{
    goalsCard.BackgroundColor = UIColor.White;
}

现在,当使用 .xaml 文件并且不使用主题时,我希望能够做同样的事情,例如标签。

我看到我可以用属性说:

TextColor = {AppThemeBinding Dark=Black, Light=White}

但我想让目标平台为我选择合适的颜色,而不是强制它们。

实际上,这个简单的 Label(没有明确指定颜色)在 iOS 上的深色模式下会消失:

<Label Text="{this:Localize SavePassword}"
                 Font="Large"
                 HorizontalOptions="StartAndExpand"
                 VerticalOptions="Center"
                 Grid.Row="0"
                 Grid.Column="0"/>

Xamarin Forms 甚至可以做到这一点吗?

【问题讨论】:

    标签: ios xamarin.forms


    【解决方案1】:

    Microsoft 文档在此处提供了与您的需求相似的内容:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/theming/system-theme-changes

    <ContentPage ...>
    <StackLayout Margin="20">
        <Label Text="This text is green in light mode, and red in dark mode."
               TextColor="{AppThemeBinding Light=Green, Dark=Red}" />
        <Image Source="{AppThemeBinding Light=lightlogo.png, Dark=darklogo.png}" />
    </StackLayout>
    

    然后

    <ContentPage ...>
    <ContentPage.Resources>
    
        <!-- Light colors -->
        <Color x:Key="LightPrimaryColor">WhiteSmoke</Color>
        <Color x:Key="LightSecondaryColor">Black</Color>
    
        <!-- Dark colors -->
        <Color x:Key="DarkPrimaryColor">Teal</Color>
        <Color x:Key="DarkSecondaryColor">White</Color>
    
        <Style x:Key="ButtonStyle"
               TargetType="Button">
            <Setter Property="BackgroundColor"
                    Value="{AppThemeBinding Light={StaticResource LightPrimaryColor}, Dark={StaticResource DarkPrimaryColor}}" />
            <Setter Property="TextColor"
                    Value="{AppThemeBinding Light={StaticResource LightSecondaryColor}, Dark={StaticResource DarkSecondaryColor}}" />
        </Style>
    
    </ContentPage.Resources>
    
    <Grid BackgroundColor="{AppThemeBinding Light={StaticResource LightPrimaryColor}, Dark={StaticResource DarkPrimaryColor}}">
      <Button Text="MORE INFO"
              Style="{StaticResource ButtonStyle}" />
    </Grid>    
    

    【讨论】:

    • 这个恐怕不会用到平台动态颜色
    • 所以你想让平台决定应该是什么颜色?
    • 对于iOS basic colors,是的。不要试图复制动态系统颜色。根据各种环境变量,动态系统颜色可能会随着版本的不同而波动。不要尝试创建与系统颜色匹配的自定义颜色,而是使用动态系统颜色。
    猜你喜欢
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 2023-03-15
    相关资源
    最近更新 更多