简体   繁体   English

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

[英]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.有没有办法创建一个自定义字体大小,它的工作方式与 Xamarin 中已经内置的 Micro 或 Small 或 Title 相同。 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" “找不到类型 FontSize”

It's not a runtime error.这不是运行时错误。 It's just a green underline in the XAML editor.它只是 XAML 编辑器中的绿色下划线。

Can it be done?可以做到吗?

You need to use x:Double instead:您需要使用x:Double代替:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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