Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Forms - Label FontSize OnPlatform - XAML error

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>

...and get this error:

No property, bindable property, or event found for FontSize

What i'm doing wrong?

Thanks.

like image 733
OPunktSchmidt Avatar asked Dec 23 '22 14:12

OPunktSchmidt


2 Answers

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:

  1. Use OnPlatform with x:Double as type argument.

    <OnPlatform x:TypeArguments="x:Double"
        iOS="20"
        Android="25" />
    
  2. 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}" />
    
  3. 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>
    

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.

like image 108
Sharada Gururaj Avatar answered Jan 09 '23 16:01

Sharada Gururaj


old thread, but i was close to implement item 3 from answer above, when i find this syntax

<Style TargetType="Button" x:Key="ListButtonStyle">
       <Setter Property="FontSize" Value="{OnIdiom Phone={OnPlatform Android=Header, iOS=Micro} , Tablet=Large, Desktop=Default}" />
</Style>

checked it with phones with ios and android and it is working nicely . decided to leave it here. xamarin.forms 4.8.0.1687

like image 21
ish1313 Avatar answered Jan 09 '23 17:01

ish1313