Xamarin.Forms-标签FontSize OnPlatform-XAML错误 [英] Xamarin.Forms - Label FontSize OnPlatform - XAML error
问题描述
我有此代码:
<Label x:Name="questionGroupHintInfoLabel" FontAttributes="Bold" Text="Folgende Hinweismeldung wurde für die aktuelle Fragengruppe hinterlegt:">
<Label.FontSize>
<OnPlatform x:TypeArguments="NamedSize"
iOS="Small"
Android="Small" />
</Label.FontSize>
</Label>
...并收到此错误:
...and get this error:
No property, bindable property, or event found for FontSize
我做错了什么?
谢谢.
推荐答案
通常,当我们设置FontSize="value"
,期望的类型(为double
)来设置值.
Usually when we set FontSize="value"
, FontSizeConverter
does the conversion to expected type (which is double
) to set the value.
但是当我们使用OnPlatform
时,似乎未使用此转换器.因此,我们有两个选择:
But it looks like this converter is not used when we use OnPlatform
. So we have two options:
-
使用
OnPlatform
和x:Double
作为类型参数.
<OnPlatform x:TypeArguments="x:Double"
iOS="20"
Android="25" />
或者,欺骗XAML处理器为我们进行转换-我们可以使用StaticResource
标记扩展名来进行转换. 注意: 这仅在未应用XAMLC的情况下有效.
Or, trick the XAML processor to do the conversion for us - we can do that by using the StaticResource
markup extension. Note: This only works if XAMLC is not applied.
<!-- App.Resources or ContentPage.Resources -->
<ResourceDictionary>
<OnPlatform x:Key="FontNamedSize" x:TypeArguments="x:String"
iOS="Small"
Android="Large" />
</ResourceDictionary>
<!-- now you can use static-resource extension to use above defined value -->
<Label x:Name="questionGroupHintInfoLabel"
FontAttributes="Bold"
Text="Folgende Hinweismeldung wurde für die aktuelle Fragengruppe hinterlegt:"
FontSize="{StaticResource FontNamedSize}" />
推荐使用NamedSize
可绑定属性扩展Label
并转换为FontSize
(基本上是FontSizeConverter
的作用).
Recommended Extend Label
with NamedSize
bindable property and convert to FontSize
(basically what the FontSizeConverter
does).
public class ExLabel : Label
{
public static readonly BindableProperty FontNamedSizeProperty =
BindableProperty.Create(
"FontNamedSize", typeof(NamedSize), typeof(ExLabel),
defaultValue: default(NamedSize), propertyChanged: OnFontNamedSizeChanged);
public NamedSize FontNamedSize
{
get { return (NamedSize)GetValue(FontNamedSizeProperty); }
set { SetValue(FontNamedSizeProperty, value); }
}
private static void OnFontNamedSizeChanged(BindableObject bindable, object oldValue, object newValue)
{
((ExLabel)bindable).OnFontNamedSizeChangedImpl((NamedSize)oldValue, (NamedSize)newValue);
}
protected virtual void OnFontNamedSizeChangedImpl(NamedSize oldValue, NamedSize newValue)
{
FontSize = Device.GetNamedSize(FontNamedSize, typeof(Label));
}
}
<!-- Usage -->
<local:ExLabel HorizontalOptions="Center" VerticalOptions="Center" Text="This is a custom label">
<local:ExLabel.FontNamedSize>
<OnPlatform x:TypeArguments="NamedSize"
iOS="Large"
Android="Medium" />
</local:ExLabel.FontNamedSize>
</local:ExLabel>
选项2仅在未应用XAMLC的情况下有效.
EDIT 1: Option 2 only works if XAMLC is not applied.
添加选项3.
EDIT 2: Add option 3.
注意:之前的版本中提供了错误修复程序发布版本,也可以将其视为替代修补程序.但是我无法确认它是否在最新版本中已修复.
Note: There is a bug fix available in pre-release versions that can also be considered as an alternative fix. But I haven't been able to confirm if it is fixed in the latest release.
这篇关于Xamarin.Forms-标签FontSize OnPlatform-XAML错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!