C# Xamarin表单:如何在一个线性代码中设置所有标签样式?

C# Xamarin表单:如何在一个线性代码中设置所有标签样式?,c#,xamarin.forms,C#,Xamarin.forms,因此,我有一个带有多个标签的XF应用程序。我希望能够根据平台的不同,在一行中以编程方式更改所有标签的FontStyle,如有可能,如下所示,而不是逐个列出标签的名称 if(Device.RuntimePlatform == Device.Android) { label.Style = Device.Styles.ListItemDetailTextStyle; } else if(Device.RuntimePlatform == Device.iOS){ label.Styl

因此,我有一个带有多个
标签的XF应用程序。我希望能够根据平台的不同,在一行中以编程方式更改所有标签的
FontStyle
,如有可能,如下所示,而不是逐个列出标签的名称

if(Device.RuntimePlatform == Device.Android) {
    label.Style = Device.Styles.ListItemDetailTextStyle;
} else if(Device.RuntimePlatform == Device.iOS){
    label.Style = Device.Styles.ListItemTextStyle;
}

这可能吗

您应该在App.xaml文件中设置样式。我需要为应用程序中的所有标签设置默认字体(包括从标签派生的新类)


并将字体设置为

 <OnPlatform x:Key="MainFontFamily" x:TypeArguments="x:String">
  <On Platform="Android">font_file_name.ttf#Font Name</On>
  <On Platform="iOS">Font Name</On>
 </OnPlatform>

字体\文件\名称.ttf \字体名称
字体名称

您可以阅读有关样式的更多信息

另一种选择是简单地扩展
标签

public class MyLabel : Label
{
    public MyLabel()
    {
        if (Device.RuntimePlatform == Device.Android)
        {
            Style = Device.Styles.ListItemDetailTextStyle;
        }
        else if (Device.RuntimePlatform == Device.iOS)
        {
            Style = Device.Styles.ListItemTextStyle;
        }
    }
}
附言:样式很好地覆盖在里面

public class MyLabel : Label
{
    public MyLabel()
    {
        if (Device.RuntimePlatform == Device.Android)
        {
            Style = Device.Styles.ListItemDetailTextStyle;
        }
        else if (Device.RuntimePlatform == Device.iOS)
        {
            Style = Device.Styles.ListItemTextStyle;
        }
    }
}