【问题标题】:Xamarin Forms Style FontSize not workingXamarin Forms Style FontSize 不起作用
【发布时间】:2018-04-04 02:18:50
【问题描述】:

我有一个简单的样式来设置一些标签属性。除了 FontSize 似乎被忽略之外,所有的 Setter 似乎都可以正常工作。

<Style x:Key="UpgradeMainBlueHeader" TargetType="Label">
     <Setter Property="FontSize" Value="35" />
     <Setter Property="HorizontalOptions" Value="Center" />
     <Setter Property="TextColor" Value="{DynamicResource colorBase}" />
</Style>

如果我直接在 Label 上设置 FontSize 它工作正常,所以我对这里发生了什么感到困惑?

(我正在使用 VS Mac,iOS 模拟器 - iPhone X)

更新

我目前可以让它工作的唯一方法是使用 OnPlatform 创建一个资源,然后在样式中使用这个资源。

<OnPlatform x:TypeArguments="Font" 
                        Android="30" 
                        iOS="30" 
                        WinPhone="20" 
                        x:Key="TitleFontSize" />

<Style x:Key="UpgradeMainBlueHeader" TargetType="Label">
                <Setter Property="Font" Value="{StaticResource TitleFontSize}" />
            </Style>

有什么理由可以这样工作,但原始代码却不行?这可能是 Xamarin 版本或模拟器问题吗?

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

    请参考 xamarin 文档:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/text/styles

    根据文档,这应该可以工作。即使我们在样式中使用了这个属性,它也能按预期工作。

    我们使用的值:

    <Setter Property="FontSize" Value="Small" />
    <Setter Property="FontSize" Value="Medium" />
    <Setter Property="FontSize" Value="22.44" />
    

    检查您是否在您的 xaml 或 *.cs 类中覆盖了此字体大小。

    但是在旧的 xamarin 讨论中(下面的链接),请查看 nprokopic 的评论:

    他说&lt;Setter Property="FontSize" 不起作用,因此他使用了&lt;Setter Property="Font",这对他有用。

    https://forums.xamarin.com/discussion/32814/style-fontsize-onplatform-in-xaml

    【讨论】:

    • 正是对我有用的东西。问题是为什么?我看不出标准 FontSize=22.44 不起作用的任何原因,因为它似乎在我看到的所有示例中都很常用?
    • 很抱歉被您的 cmets 弄糊涂了。有用吗??
    • 是的,“Font”有效,但“FontSize”无效。这很奇怪,因为大多数 Xamarin 文档,甚至整个 Xamarin 大学,他们都使用“FontSize”并且他们坚持认为它在他们身边有效?!?
    • 检查您使用的 Xamarin 版本。它可能已过时,您可能需要对其进行更新。
    • 使用最新的 (2.4.0.282)。文档中似乎没有很清楚,这是我所看到的关于字体大小的正确处理方法。两种方式都显示了,但只有 OnPlatform 一种对我有用。只是好奇为什么。
    【解决方案2】:

    你的代码是正确的。

    App.xaml

        <Color x:Key="colorBase">#00FF77</Color>
        <Style x:Key="UpgradeMainBlueHeader" TargetType="Label">
             <Setter Property="FontSize" Value="35" />
             <Setter Property="HorizontalOptions" Value="Center" />
             <Setter Property="TextColor" Value="{DynamicResource colorBase}" />
        </Style>
    

    查看:

    <Label Text="Hello World" Style="{StaticResource UpgradeMainBlueHeader}"/>
    

    【讨论】:

    • 您使用的是哪个版本的 XCode?我刚刚从 XCode 9.2 升级到 XCode 9.4.1,并且看到了类似的行为。使用 XCode 9.2,我的样式可以正常工作。例如,使用 XCode 9.4.1 标签背景颜色,只有在我使用 OnPlatform 创建一个 StaticResource(就像您所做的那样)时才有效。
    • 嘿@JamesLavery 最好创建一个新问题,我怀疑它与字体大小有关,不要忘记包含示例代码。
    • 我同意 - 我将添加一个新问题。
    【解决方案3】:

    我在 Xamarin 大学使用 Visual Studio for Mac 7.5.3 的 XAM140/Exercise 4/Completed 项目中遇到了同样的问题——使用 ResourceDictionary 中的样式来修改标签元素的字体对标签没有影响.

    我的解决方法是在解决方案资源管理器中打开 Packages 文件夹,右键单击 Xamarin.Forms 包,然后将其更新到最新版本(当时为 3.1.0.583944)。我更新了所有目标中的 Xamarin.Forms,然后在打开和运行项目之前进行了全面清理并重新启动了 Visual Studio。

    更新后,问题得到解决,并且在设计预览和设备上运行都开始按预期工作。

    <Style x:Key="myLabelStyle" TargetType="Label">
       <Setter Property="FontSize" Value="32"   />
    </Style>
    

    现在适用于标签:

    <Label x:Name="billLabel"  Text="Bill"  Style="{StaticResource myLabelStyle}" Grid.Row="0" Grid.Column="0" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-12
      • 2023-03-24
      • 2019-12-15
      • 2020-07-22
      • 2021-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多