简体   繁体   中英

How to create a custom font size in xaml using Xamarin, so that you can change font sizes depending on device?

Is there are way to create a custom fontsize that would work the same as Micro or Small or Title that are already built into Xamarin. I want to be able to use it like the following:

<Label Text="Test" FontSize="MyFontSize"/>

I think it would be implemented something like the following inside my resource dictionary but it isn't working:

<FontSize x:Key="MyFontSize">
    <OnPlatform x:TypeArguments="FontSize">
        <On Platform="UWP">25</On>
        <On Platform="iOS">12</On>
    </OnPlatform>
</FontSize>

The error says:

"The type FontSize can not be found"

It's not a runtime error. It's just a green underline in the XAML editor.

Can it be done?

You need to use x:Double instead:

<OnPlatform x:Key="MyFontSize" x:TypeArguments="x:Double">
    <On Platform="UWP">25</On>
    <On Platform="iOS">12</On>
</OnPlatform>

And then reference the resource like this:

<Label Text="Test" FontSize="{StaticResource MyFontSize}"/>

Or you can specify it directly on the view:

<Label Text="Test">
   <Label.FontSize>
      <OnPlatform x:TypeArguments="x:Double">
          <On Platform="UWP">25</On>
          <On Platform="iOS">12</On>
      </OnPlatform>
   </Label.FontSize>
</Label>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM