Xamarin.forms Xamarin中的Fontsome形成UWP

Xamarin.forms Xamarin中的Fontsome形成UWP,xamarin.forms,font-awesome,xamarin.uwp,Xamarin.forms,Font Awesome,Xamarin.uwp,我有一个自定义的渲染器,在Android中使用Fontawesome。 我是按照这个指南做的 使用自定义标签类型尝试在UWP中工作 public class FontAwesomeLabel: Xamarin.Forms.Label { public FontAwesomeLabel() { switch (Device.RuntimePlatform) { case Device.UWP:

我有一个自定义的渲染器,在Android中使用Fontawesome。 我是按照这个指南做的 使用自定义标签类型尝试在UWP中工作

public class FontAwesomeLabel: Xamarin.Forms.Label    {
    public FontAwesomeLabel()        {
        switch (Device.RuntimePlatform)
      {
           case Device.UWP:
                FontFamily = @"/Assets/FontAwesome.otf#FontAwesome";
                break;
        }
    }
}
字体

作为ttf和otf加载。两种我都试过了。 他们认为字体具有构建操作“内容”

仅适用于Android,不适用于UWP

我看到一个奇怪的框,而不是预期的Android图标


你知道我做错了什么吗?

在UWP上添加字体的正确路径是
/Assets/fontsawesome.ttf#fontsawesome
似乎你必须添加
字体
文件夹

像这样:

此外,您还可以使用并检查以下答案:


问题的解决方案是使用UWP,使用正确的字体系列名称。 Font Awesome 5自由立体版 不仅仅是“太棒了”

对于任何对细节感兴趣的人:
我下载了一个字体编辑器来检查内部字体名称和字体族名称。我犯了一个错误,即代码复制了使用类似/旧字体的值的博客文章。有关实际内容,请参见图片

我得到了两种解决方案:
1) 共享代码/.netStandard公共项目中的自定义控件。
2) 自定义渲染器

解决方案1:自定义标签

public class FontAwesomeLabel: Xamarin.Forms.Label
{
    public FontAwesomeLabel()
    {
        switch (Device.RuntimePlatform)
        {
           case Device.UWP:
             // make sure the correct font family name is used. Check in a font editor
               this.FontFamily = @"/Assets/Fonts/FontAwesome.otf#Font Awesome 5 Free Solid";
                break;
        }
    }
}
[assembly: ExportRenderer(typeof(Label), typeof(FontAwesomeLabelRenderer))]
namespace Oxando.UWP.CustomRenderers
{
public class FontAwesomeLabelRenderer : LabelRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);
        if (Control != null)
        {
            // on UWP be sure use the Font Family name.
            // get a font editor and check the name, if it doesnt match UWP wont load it
            if (FontAwesomeUtil.CheckIsFA(e.NewElement.Text))
            {
                Control.FontFamily = new FontFamily("/Assets/Fonts/FontAwesome.otf#Font Awesome 5 Free Solid");
            }
        }
    }
}
internal static class FontAwesomeUtil
{
    public static bool CheckIsFA(string text)
    {
        if (text.Length == 0) return false;
        if (text.Length > 1 || text[0] < 0xf000) return false;
        return true;
    }
}
解决方案2:标准控件上的客户渲染器

public class FontAwesomeLabel: Xamarin.Forms.Label
{
    public FontAwesomeLabel()
    {
        switch (Device.RuntimePlatform)
        {
           case Device.UWP:
             // make sure the correct font family name is used. Check in a font editor
               this.FontFamily = @"/Assets/Fonts/FontAwesome.otf#Font Awesome 5 Free Solid";
                break;
        }
    }
}
[assembly: ExportRenderer(typeof(Label), typeof(FontAwesomeLabelRenderer))]
namespace Oxando.UWP.CustomRenderers
{
public class FontAwesomeLabelRenderer : LabelRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);
        if (Control != null)
        {
            // on UWP be sure use the Font Family name.
            // get a font editor and check the name, if it doesnt match UWP wont load it
            if (FontAwesomeUtil.CheckIsFA(e.NewElement.Text))
            {
                Control.FontFamily = new FontFamily("/Assets/Fonts/FontAwesome.otf#Font Awesome 5 Free Solid");
            }
        }
    }
}
internal static class FontAwesomeUtil
{
    public static bool CheckIsFA(string text)
    {
        if (text.Length == 0) return false;
        if (text.Length > 1 || text[0] < 0xf000) return false;
        return true;
    }
}
[程序集:ExportRenderer(typeof(标签)、typeof(FontAwesomeLabelRenderer))]
命名空间Oxando.UWP.CustomRenders
{
公共类FontAwesomeLabelRenderer:LabelRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(控件!=null)
{
//在UWP上,请确保使用字体系列名称。
//获取字体编辑器并检查名称,如果名称与UWP不匹配,则不会加载它
if(FontAwesomeUtil.CheckIsFA(e.NewElement.Text))
{
Control.FontFamily=新FontFamily(“/Assets/Fonts/FontAwesome.otf#Font Awesome 5 Free Solid”);
}
}
}
}
内部静态类FontAwesomeUtil
{
公共静态bool CheckIsFA(字符串文本)
{
if(text.Length==0)返回false;
如果(text.Length>1 | | text[0]<0xf000)返回false;
返回true;
}
}
}

字体编辑器中显示的实际内部名称

我尝试将字体移动到目录中。不走运。不知道为什么会有必要。也许你应该使用它。我安装了Iconize.UWP,如果我知道如何使用它,我会使用它。我不知道如何在代码隐藏中使用它来设置自定义标签类型。我试图为其他人记录答案,但忘了标记为已回答。