如何在 XAML 上设置 ResourceDictionary FontSize?

2024-04-24

如果这会是一件混乱的事情,我深表歉意。我对 Xamarin 还很陌生。目前我正在尝试将此资源字典代码转换为 XAML:

Current.Resources = new ResourceDictionary {
  { FontResources.DefaultButtonFontAttribute, FontAttributes.Bold }, 
  { FontResources.DefaultLabelFontSize, 
 Xamarin.Forms.Device.GetNamedSize(NamedSize.Small, typeof(Label)) }, 
  { StyleResources.DefaultLabelStyle, LABEL_STYLE }    
}

(FontResources 和 StyleResources 是包含键名称的 RESX 文件)

public static Style LABEL_STYLE = new Style(typeof(Label))
        {
            Setters = {
                new Setter { Property = Label.FontSizeProperty, Value = new DynamicResource( FontResources.DefaultLabelFontSize )},
                new Setter { Property = Label.FontAttributesProperty, Value = new DynamicResource( FontResources.DefaultLabelFontAttribute )},
            }
};

我尝试这样做的方式是这样的:

<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.App">
    <Application.Resources>
        <ResourceDictionary>
            <FontAttributes x:Key="DefaultLabelFontAttribute"></FontAttributes>
            <FontSize x:Key="DefaultLabelFontSize"></FontSize>
            <Style x:Key="DefaultLabelStyle" TargetType="Label">
                <Setter Property="FontAttributes" Value="{DynamicResource DefaultLabelFontAttribute}"></Setter>
                <Setter Property="FontSize" Value="{DynamicResource DefaultLabelFontSize}"></Setter>
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

但似乎我没有以正确的方式这样做,因为没有类似于 FontAttribute 的 FontSize 属性。我只能看到 FontSizeConverter。还有一种方法可以调用此代码:Xamarin.Forms.Device.GetNamedSize(NamedSize.Small, typeof(Label))到Xaml?


还有一种方法可以调用此代码:Xamarin.Forms.Device.GetNamedSize(NamedSize.Small, typeof(Label))到Xaml?

资源字典:

<ResourceDictionary>
    <Style x:Key="Labelfont" TargetType="Label">
        <Setter Property="FontSize" Value="Small" />
    </Style>
</ResourceDictionary>

样式用法:

Label Text="UsingSmallFont" Style="{StaticResource Labelfont}"/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 XAML 上设置 ResourceDictionary FontSize? 的相关文章

随机推荐