本文介绍了如何在Xamarin Forms中使用自定义字体(任何外部字体)作为嵌入式资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Xamarin Forms项目中使用自定义外部字体(Monotype Corsiva)作为嵌入式资源.我正在使用Xamarin.Forms版本4.5.0.617

I want to use custom external font (Monotype Corsiva) as Embedded resource in my Xamarin Forms project. I am using Xamarin.Forms version 4.5.0.617

我已在 App.xaml.cs

我正在 XAML 页面的Label内部使用它:

I am using it inside a Label in my XAML page:

<Label Text="Hello" FontFamily="MonotypeCorsiva" FontSize="Medium" />

我所做的一切都正确吗,我错过了什么吗?

Am I doing everything correct, am I missing anything?

我遇到错误:

在UWP构建中: MyApplication.UWP \ App.xaml.cs

在Android Build中

我在 MyApplication.Droid \ MainActivity.cs

System.TypeLoadException: 'Could not resolve type with token 01000072 from typeref (expected class 'Xamarin.Forms.ExportFontAttribute' in assembly 'Xamarin.Forms.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null')'

推荐答案

我可以使用它,只是在我的解决方案中的所有项目中,将NuGet包 Xamarin.Forms 更新为版本4.5.0.617.

I got it working, just updated the NuGet package Xamarin.Forms to version 4.5.0.617 in all projects in my Solution.

以前,我仅在共享项目中将Xamarin.Forms NuGet软件包更新为版本4.5x.因此,遇到此错误

Previously I had updated Xamarin.Forms NuGet Package to version 4.5x only in the shared project. Hence this error was encountered

System.TypeLoadException: 'Could not load type Xamarin.Forms.ExportFontAttribute'`

现在,我在版本4.5x的解决方案中的所有项目(Android,iOS和UWP)中都更新了Xamarin.Forms NuGet.请注意,在Xamarin Forms中使用外部自定义字体作为嵌入式资源需要Xamarin.Forms版本4.5x或更高版本请参阅此链接以获取更多信息

Now I have Updated Xamarin.Forms NuGet in all projects (Android, iOS and UWP) in my solution to version 4.5x. Please note that using external custom fonts as Embedded resource in Xamarin Forms requires Xamarin.Forms version 4.5x or later refer to this link for more information

现在正确的方法:

  • 假设我想在项目中使用 Corsiva单色 字体并将其用作嵌入式资源.

  • Suppose I want to use Monotype Corsiva font in my project and use it as embedded resource.

在解决方案(Android,iOS,UWP等)中的所有项目中将Xamarin.Forms更新为版本4.5.0.530或更高版本.为此,请在解决方案资源管理器中,点击右键解决方案名称-> 管理NuGet软件包以获取解决方案-> 更新Xamarin.Forms软件包到4.5x或更高版本-> 选择所有项目并更新.

Update Xamarin.Forms to version 4.5.0.530 or later in all projects in solution (Android, iOS, UWP, etc.). To do it, In Solution Explorer, Right Click Solution name -> Manage NuGet Packages for solution -> Update Xamarin.Forms package to 4.5x or later -> Select all projects and update.

在共享项目中添加字体文件(.ttf).

Add a font file (.ttf) in your shared project.

将文件设置为嵌入式资源(右键单击 Solution Explorer 中的字体文件-> 属性 >-> 构建操作:设置为嵌入式资源).

Set the file as Embedded resource (Right Click Font file in Solution Explorer -> Properties -> Build Action: Set as Embedded resource).

在项目中任何类的任何命名空间之前添加[assembly: ExportFont("FontFileName.ttf", Alias = "MyFont")].无需添加字体文件的完整路径.最好在启动名称空间之前添加新类并添加上述代码.就像:

Add [assembly: ExportFont("FontFileName.ttf", Alias = "MyFont")] before any namespace of any class in your project. No need to add full path of the font file. It would be better if you add new class and add the above mentioned code before starting namespace. Just like:

[assembly: ExportFont("MonotypeCorsiva.ttf", Alias = "MyFont")]
namespace MyApplication.Extensions
{
    public class ExportFont
    {
        // You can have an empty class
        ...
    }
}

  • 现在像在任何XAML页面中一样使用它
  • <Label Text="Hello" FontFamily="MonotypeCorsiva" FontSize="Medium" HorizontalOptions="CenterAndExpand" />
    

    • 您可以在XAML代码中使用别名或字体名称. FontFamily = "MyFont"也是正确的.

      • You can use Alias name or Font Name in XAML code. FontFamily = "MyFont" is also correct.

        现在运行项目.

        这篇关于如何在Xamarin Forms中使用自定义字体(任何外部字体)作为嵌入式资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:15