【问题标题】:Xamarin Forms FontSize by PlatformXamarin 按平台形成 FontSize
【发布时间】:2019-01-25 22:37:01
【问题描述】:

UWP Xamarin 应用程序存在一个已知问题,即为 Windows Mobile(不确定桌面 Windows 10)呈现的字体大小比在其他两个平台 iOS 和 Android 上呈现的字体大得多。我为此找到的解决方法是在 UWP 应用程序中放置一个带有小数点的数字字体大小(例如 24.1 表示大字体大小)。这很好用。我不想在我的 App.xaml 中定义一个适用于所有平台的静态资源。我试过的,显然不起作用,如下:

        <OnPlatform x:Key="CustLarge" x:TypeArguments="x:Double">
            <On Platform="iOS|Android">Large</On>
            <On Platform="UWP">24.1</On>
        </OnPlatform>

理论上,在我的 XAML 中,我只需将所有大字体编码为“CustLarge”,如下所示:

FontSize = "{StaticResource CustLarge}" />

有什么想法可以在不使用 OnPlatform 的情况下为我的应用中的每个 FontSize 完成此操作吗?任何帮助,将不胜感激。

更新:以下是我正在谈论的内容的屏幕截图。 iOS 模拟器适用于 iPhone 6 4.7" 宽。Windows 10 模拟器是 10.0.15254.9 5" 手机。

您可以看到 Map All 文本比应有的大得多。 (我正在对独立按钮右侧的段控件中的文本进行相对比较。)在这两种情况下,字体大小都设置为 MICRO。

回到我的问题 - 有人知道怎么做吗?

【问题讨论】:

  • 首先,在为资源定义多个平台时(根据On codeListStringTypeConverter),您应该使用,(逗号)分隔符而不是| /跨度>
  • 我已经用 Xamarin .NetStandard 进行了测试,它运行良好。你使用 Xamarin PCL 吗?
  • 是的,这是使用 Xamarin Forms 的 PCL,这是 UWP 应用程序关于字体大小的记录问题。
  • 关于逗号的使用,我使用了| (条形分隔符)每个 Xamarin XAML 文档。如果他们关于 XAML 中 onplatform 的文档发生了变化,您可以向我提供更新文档的链接,我将不胜感激,
  • @GeorgeMCeaserJr 我已经使用 Xamarin.Forms v2.4.0.91020 进行了测试。效果很好请尝试更新到相同版本(包含安装在uwp平台的nuget包)。

标签: xaml xamarin.forms uwp font-size staticresource


【解决方案1】:

我能够通过创建自定义 ResourceDictionnary 并在构造函数中添加 FontSizes 来找到解决方法。

public class SResourceDictionnary : ResourceDictionary
{
    public SResourceDictionnary()
    {
        Add("Micro", new OnPlatform<double>
        {
            Default = Device.GetNamedSize(NamedSize.Micro, typeof(Label)),
            Platforms = { new On
                {
                    Value = 12d,
                    Platform = new List<string>{"UWP"}
                }
            }
        });
        Add("Small", new OnPlatform<double>
        {
            Default = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
            Platforms = { new On
                {
                    Value = 14d,
                    Platform = new List<string>{"UWP"}
                }
            }
        });
        Add("Medium", new OnPlatform<double>
        {
            Default = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
            Platforms = { new On
                {
                    Value = 18d,
                    Platform = new List<string>{"UWP"}
                }
            }
        });
        Add("Large", new OnPlatform<double>
        {
            Default = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
            Platforms = { new On
                {
                    Value = 20d,
                    Platform = new List<string>{"UWP"}
                }
            }
        });
    }

然后我像这样将字典合并到我的 App.xaml 中

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <helpers:SResourceDictionnary></helpers:SResourceDictionnary>
        </ResourceDictionary.MergedDictionaries>
        <Style TargetType="Label">
            <Setter Property="FontSize" Value="{StaticResource Small}" ></Setter>
        </Style>
    </ResourceDictionary>
</Application.Resources>

它允许我使用 FontSize 作为静态资源。 唯一的缺点是你在 Xaml 中失去了智能感知

【讨论】:

  • 感谢 Elias - 这听起来很棒。我会试试看。
  • Elias,这项工作适用于任何控制类型吗?我看到您引用了适用于 Label 控件的命名大小,所以我想仔细检查一下。
  • 我正在为按钮和条目使用标签,它工作正常。但是您可以为每个控件创建一个资源大小。
  • 所以这看起来确实有效。在您的 App.xaml 中,请务必向您的项目添加和程序集引用 - 类似于 xmlns:ctrl="clr-namespace:GBarScene;assembly=GBarScene" 在这种情况下,ctrl 将替换 Elias 示例代码中的帮助程序
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-05
  • 2020-04-02
  • 2016-12-16
  • 1970-01-01
  • 1970-01-01
  • 2021-02-14
相关资源
最近更新 更多