通过创建一个自定义的ResourceDictionary,并在构造函数中添加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>
唯一的缺点是在Xaml中失去了智能感知