如果这会是一件混乱的事情,我深表歉意。我对 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?