OPu*_*idt 2 c# xamarin.ios xamarin.android xamarin xamarin.forms
I have this code:
<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>
Run Code Online (Sandbox Code Playgroud)
...and get this error:
No property, bindable property, or event found for FontSize
Run Code Online (Sandbox Code Playgroud)
What i'm doing wrong?
Thanks.
Usually when we set FontSize="value", FontSizeConverter does the conversion to expected type (which is double) to set the value.
But it looks like this converter is not used when we use OnPlatform. So we have two options:
Use OnPlatform with x:Double as type argument.
<OnPlatform x:TypeArguments="x:Double"
iOS="20"
Android="25" />
Run Code Online (Sandbox Code Playgroud)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}" />
Run Code Online (Sandbox Code Playgroud)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>
Run Code Online (Sandbox Code Playgroud)EDIT 1: Option 2 only works if XAMLC is not applied.
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.
小智 5
旧线程,但当我发现此语法时,我即将实现上面答案中的第 3 项
<Style TargetType="Button" x:Key="ListButtonStyle">
<Setter Property="FontSize" Value="{OnIdiom Phone={OnPlatform Android=Header, iOS=Micro} , Tablet=Large, Desktop=Default}" />
</Style>
Run Code Online (Sandbox Code Playgroud)
用ios和android手机检查了一下,效果很好。决定把它留在这里。xamarin.forms 4.8.0.1687