Xamarin.Forms - Label 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>
...并收到此错误:
No property, bindable property, or event found for FontSize
我做错了什么?
谢谢.
推荐答案
通常我们设置 FontSize="value"
时,FontSizeConverter
转换为 预期类型(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
作为类型参数.
Use
OnPlatform
withx:Double
as type argument.
<OnPlatform x:TypeArguments="x:Double"
iOS="20"
Android="25" />
或者,欺骗 XAML 处理器为我们进行转换 - 我们可以通过使用 StaticResource
标记扩展来实现.注意: 这仅在未应用 XAMLC 时有效.
<!-- 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>
编辑 1: 选项 2 仅在未应用 XAMLC 时才有效.
EDIT 1: Option 2 only works if XAMLC is not applied.
编辑 2: 添加选项 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 - Label FontSize OnPlatform - XAML 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!