Xamarin.forms自定义字体异常

时间:2016-11-18 12:22:37

标签: c# ios xaml xamarin xamarin.forms

您好我正在尝试使用以下代码将自定义字体应用于Label

    <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ExerClub.Pages.Profile" Title="Profile">
  <StackLayout>
    <Label Text="Login">
      <Label.FontFamily>
        <OnPlatform x:TypeArguments="x:String">
          <OnPlatform.iOS>MarkerFelt-Thin</OnPlatform.iOS>
          <OnPlatform.Android></OnPlatform.Android>
          <OnPlatform.WinPhone></OnPlatform.WinPhone>
        </OnPlatform>
      </Label.FontFamily>
    </Label>
    <Label Text="Club Name"/>
    <Label Text="More randomness"/>

  </StackLayout>
</ContentPage>

背后的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;

namespace ExerClub.Pages
{
    public partial class Profile : ContentPage
    {
        public Profile()
        {
            InitializeComponent();
        }
    }
}

这取自Xamarin.forms Documentation

我在iOS上收到以下错误

System.ArgumentException: Object of type 'System.String' cannot be converted to type 'Xamarin.Forms.View'.

我是Xamarin的新手,并且真的不明白为什么会失败。

1 个答案:

答案 0 :(得分:1)

您需要输入字体名称<x:String>fontName</x:String>标签,或者只需写下面的内容:

<Label Text="Login">
  <Label.FontFamily>
  <OnPlatform x:TypeArguments="x:String"
              iOS="MarkerFelt-Thin" Android=""
              WinPhone=""/>
  </Label.FontFamily>
</Label>