码迷,mamicode.com
首页 > 其他好文 > 详细

avalonia项目在银河麒麟操作系统arm架构上运行报错:default font family is not be null or empty

时间:2021-01-30 12:05:55      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:col   width   class   银河   table   span   运行   xaml   官方   

最近在做c#跨平台项目的时候,遇到了avalonia项目在银河麒麟操作系统上运行时报错:default font family is not be null or empty。但是在windows、ubuntu上运行没有问题。最终通过查看avalonia源码和官方提供的测试示例找到解决方案。(记录一下,避免以后忘了。。。)

第一步:将字体最为项目的嵌入资源导入进项目。

技术图片

<ItemGroup>
    <EmbeddedResource Include="Assets\Fonts\msyh.ttc" />
    <EmbeddedResource Include="Assets\Fonts\msyhbd.ttc" />
    <EmbeddedResource Include="Assets\Fonts\msyhl.ttc" />
  </ItemGroup>

第二步:新建一个类,作为自定义字体管理类。

public class CustomFontManagerImpl : IFontManagerImpl
    {
        private readonly Typeface[] _customTypefaces;
        private readonly string _defaultFamilyName;

        //Load font resources in the project, you can load multiple font resources
        private readonly Typeface _defaultTypeface =
            new Typeface("resm:AvaloniaApplication1.Assets.Fonts.msyh#微软雅黑");

        public CustomFontManagerImpl()
        {
            _customTypefaces = new[] { _defaultTypeface };
            _defaultFamilyName = _defaultTypeface.FontFamily.FamilyNames.PrimaryFamilyName;
        }

        public string GetDefaultFontFamilyName()
        {
            return _defaultFamilyName;
        }

        public IEnumerable<string> GetInstalledFontFamilyNames(bool checkForUpdates = false)
        {
            return _customTypefaces.Select(x => x.FontFamily.Name);
        }

        private readonly string[] _bcp47 = { CultureInfo.CurrentCulture.ThreeLetterISOLanguageName, CultureInfo.CurrentCulture.TwoLetterISOLanguageName };

        public bool TryMatchCharacter(int codepoint, FontStyle fontStyle, FontWeight fontWeight, FontFamily fontFamily,
            CultureInfo culture, out Typeface typeface)
        {
            foreach (var customTypeface in _customTypefaces)
            {
                if (customTypeface.GlyphTypeface.GetGlyph((uint)codepoint) == 0)
                {
                    continue;
                }

                typeface = new Typeface(customTypeface.FontFamily.Name, fontStyle, fontWeight);

                return true;
            }

            var fallback = SKFontManager.Default.MatchCharacter(fontFamily?.Name, (SKFontStyleWeight)fontWeight,
                SKFontStyleWidth.Normal, (SKFontStyleSlant)fontStyle, _bcp47, codepoint);

            typeface = new Typeface(fallback?.FamilyName ?? _defaultFamilyName, fontStyle, fontWeight);

            return true;
        }

        public IGlyphTypefaceImpl CreateGlyphTypeface(Typeface typeface)
        {
            SKTypeface skTypeface;

            switch (typeface.FontFamily.Name)
            {
                case FontFamily.DefaultFontFamilyName:
                case "微软雅黑":  //font family name
                    skTypeface = SKTypeface.FromFamilyName(_defaultTypeface.FontFamily.Name); break;
                default:
                    skTypeface = SKTypeface.FromFamilyName(typeface.FontFamily.Name,
                        (SKFontStyleWeight)typeface.Weight, SKFontStyleWidth.Normal, (SKFontStyleSlant)typeface.Style);
                    break;
            }

            return new GlyphTypefaceImpl(skTypeface);
        }
    }

第三步:在App.axaml.cs中重写RegisterServices()函数,将我们自定义的字体管理对象注册进去。

/// <summary>
        /// override RegisterServices register custom service
        /// </summary>
        public override void RegisterServices()
        {
            AvaloniaLocator.CurrentMutable.Bind<IFontManagerImpl>().ToConstant(new CustomFontManagerImpl());
            base.RegisterServices();
        }

 

经过以上三个步骤,我的程序可以在windows、Ubuntu、银河麒麟操作系统上正常运行,没有出错。

avalonia项目在银河麒麟操作系统arm架构上运行报错:default font family is not be null or empty

标签:col   width   class   银河   table   span   运行   xaml   官方   

原文地址:https://www.cnblogs.com/joyandjoys/p/14346935.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!