如何在Xamarin中使用XAML创建自定义字体大小,以便根据设备更改字体大小?

huangapple go评论96阅读模式
英文:

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

问题

Is there a 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?

英文:

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:

&lt;Label Text=&quot;Test&quot; FontSize=&quot;MyFontSize&quot;/&gt;

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

&lt;FontSize x:Key=&quot;MyFontSize&quot;&gt;
    &lt;OnPlatform x:TypeArguments=&quot;FontSize&quot;&gt;
        &lt;On Platform=&quot;UWP&quot;&gt;25&lt;/On&gt;
        &lt;On Platform=&quot;iOS&quot;&gt;12&lt;/On&gt;
    &lt;/OnPlatform&gt;
&lt;/FontSize&gt;

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?

答案1

得分: 4

You need to use x:Double instead:

&lt;OnPlatform x:Key=&quot;MyFontSize&quot; x:TypeArguments=&quot;x:Double&quot;&gt;
    &lt;On Platform=&quot;UWP&quot;&gt;25&lt;/On&gt;
    &lt;On Platform=&quot;iOS&quot;&gt;12&lt;/On&gt;
&lt;/OnPlatform&gt;

And then reference the resource like this:

&lt;Label Text=&quot;Test&quot; FontSize=&quot;{StaticResource MyFontSize}&quot;/&gt;

Or you can specify it directly on the view:

&lt;Label Text=&quot;Test&quot;&gt;
   &lt;Label.FontSize&gt;
      &lt;OnPlatform x:TypeArguments=&quot;x:Double&quot;&gt;
          &lt;On Platform=&quot;UWP&quot;&gt;25&lt;/On&gt;
          &lt;On Platform=&quot;iOS&quot;&gt;12&lt;/On&gt;
      &lt;/OnPlatform&gt;
   &lt;/Label.FontSize&gt;
&lt;/Label&gt;
英文:

You need to use x:Double instead:

&lt;OnPlatform x:Key=&quot;MyFontSize&quot; x:TypeArguments=&quot;x:Double&quot;&gt;
    &lt;On Platform=&quot;UWP&quot;&gt;25&lt;/On&gt;
    &lt;On Platform=&quot;iOS&quot;&gt;12&lt;/On&gt;
&lt;/OnPlatform&gt;

And then reference the resource like this:

&lt;Label Text=&quot;Test&quot; FontSize=&quot;{StaticResource MyFontSize}&quot;/&gt;

Or you can specify it directly on the view:

&lt;Label Text=&quot;Test&quot;&gt;
   &lt;Label.FontSize&gt;
      &lt;OnPlatform x:TypeArguments=&quot;x:Double&quot;&gt;
          &lt;On Platform=&quot;UWP&quot;&gt;25&lt;/On&gt;
          &lt;On Platform=&quot;iOS&quot;&gt;12&lt;/On&gt;
      &lt;/OnPlatform&gt;
   &lt;/Label.FontSize&gt;
&lt;/Label&gt;

huangapple
  • 本文由 发表于 2020年1月6日 22:54:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定