Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...