我按照https://developer.xamarin.com/guides/xamarin-forms/user-interface/text/fonts/#Using_a_Custom_Font上的步骤进行操作。
在UWP上,我已将字体文件包含在Assets / Fonts / DSEG7Modern-Regular.ttf中(生成操作:内容。从不复制)。
Android位于Assets / DSEG7Modern-Regular.ttf(构建操作:AndroidAsset。从不复制)
码
<Label Text="Hello Forms with XAML">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.iOS>DSEG7Modern-Regular</OnPlatform.iOS>
<OnPlatform.Android>DSEG7Modern-Regular.ttf#DSEG7 Modern</OnPlatform.Android>
<OnPlatform.WinPhone>Assets/DSEG7Modern-Regular.ttf#DSEG7 Modern</OnPlatform.WinPhone>
</OnPlatform>
</Label.FontFamily>
</Label>
这可以在Android设备上正常运行。如果在本地系统上安装字体,它也可以正常工作。如果我没有安装字体(我的用户不会安装),它将无法正常工作。
我也尝试过将其与后面的代码一起使用,但它也不起作用。
private string GetDSEG7FontForCurrentDevice()
{
switch (Device.RuntimePlatform)
{
case Device.Windows:
case Device.WinPhone:
return "Assets/Fonts/DSEG7Modern-Regular.ttf#DSEG7 Modern";
case Device.Android:
return "DSEG7Modern-Regular.ttf#DSEG7 Modern";
case Device.iOS:
return "DSEG7Modern-Regular";
}
return MainLabel.FontFamily;
}
最佳答案
如下所示,在“资产”之前添加“ /”
在xaml中
<Label x:Name="LabelModernFont" Text="Hello Forms with XAML">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String" >
<On Platform ="Windows">/Assets/Fonts/DSEG7Modern-Regular.ttf#DSEG7 Modern</On>
</OnPlatform>
</Label.FontFamily>
</Label>
在代码中:
switch (Device.RuntimePlatform)
{
case "Windows":
LabelModernFont.FontFamily = "/Assets/Fonts/DSEG7Modern-Bold.ttf#DSEG7 Modern";
break;
}