如何在XAML上设置ResourceDictionary FontSize? [英] How can I set ResourceDictionary FontSize on XAML?
问题描述
很抱歉,如果那会是一团糟.我是Xamarin的新手.目前,我正在尝试将此资源字典代码转换为XAML:
Apologies if this would be a messy one. I'm pretty new to Xamarin. Currently I'm trying to convert this resource dictionary code to a 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文件)具有
(FontResources and StyleResources is a RESX file that contains the key name) Having
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 )},
}
};
我尝试执行此操作的方式如下:
The way I'm trying to do this is like this:
<?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.还有一种方法可以调用此代码:Xaml中的Xamarin.Forms.Device.GetNamedSize(NamedSize.Small, typeof(Label))
?
But it seems that I'm not doing it the right way as there's no FontSize property similar to the FontAttribute. All I can see is FontSizeConverter. Also is there a way to call this code: Xamarin.Forms.Device.GetNamedSize(NamedSize.Small, typeof(Label))
to the Xaml?
推荐答案
还有一种方法可以调用此代码:Xaml中的
Xamarin.Forms.Device.GetNamedSize(NamedSize.Small, typeof(Label))
吗?
ResourceDictionary:
<ResourceDictionary>
<Style x:Key="Labelfont" TargetType="Label">
<Setter Property="FontSize" Value="Small" />
</Style>
</ResourceDictionary>
样式用法:
Label Text="UsingSmallFont" Style="{StaticResource Labelfont}"/>
这篇关于如何在XAML上设置ResourceDictionary FontSize?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!