PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 13, 2022

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

 September 13, 2022     c#, cross-platform, uwp, xamarin, xaml     No comments   

Issue

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?


Solution

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>


Answered By - Martin Zikmund
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing