经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Linux/Shell » 查看文章
avalonia项目在银河麒麟操作系统arm架构上运行报错:default font family is not be null or empty
来源:cnblogs  作者:JoyandJoys  时间:2021/2/1 11:48:43  对本文有异议

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

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

  1. <ItemGroup>
  2. <EmbeddedResource Include="Assets\Fonts\msyh.ttc" />
  3. <EmbeddedResource Include="Assets\Fonts\msyhbd.ttc" />
  4. <EmbeddedResource Include="Assets\Fonts\msyhl.ttc" />
  5. </ItemGroup>

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

  1. public class CustomFontManagerImpl : IFontManagerImpl
  2. {
  3. private readonly Typeface[] _customTypefaces;
  4. private readonly string _defaultFamilyName;
  5. //Load font resources in the project, you can load multiple font resources
  6. private readonly Typeface _defaultTypeface =
  7. new Typeface("resm:AvaloniaApplication1.Assets.Fonts.msyh#微软雅黑");
  8. public CustomFontManagerImpl()
  9. {
  10. _customTypefaces = new[] { _defaultTypeface };
  11. _defaultFamilyName = _defaultTypeface.FontFamily.FamilyNames.PrimaryFamilyName;
  12. }
  13. public string GetDefaultFontFamilyName()
  14. {
  15. return _defaultFamilyName;
  16. }
  17. public IEnumerable<string> GetInstalledFontFamilyNames(bool checkForUpdates = false)
  18. {
  19. return _customTypefaces.Select(x => x.FontFamily.Name);
  20. }
  21. private readonly string[] _bcp47 = { CultureInfo.CurrentCulture.ThreeLetterISOLanguageName, CultureInfo.CurrentCulture.TwoLetterISOLanguageName };
  22. public bool TryMatchCharacter(int codepoint, FontStyle fontStyle, FontWeight fontWeight, FontFamily fontFamily,
  23. CultureInfo culture, out Typeface typeface)
  24. {
  25. foreach (var customTypeface in _customTypefaces)
  26. {
  27. if (customTypeface.GlyphTypeface.GetGlyph((uint)codepoint) == 0)
  28. {
  29. continue;
  30. }
  31. typeface = new Typeface(customTypeface.FontFamily.Name, fontStyle, fontWeight);
  32. return true;
  33. }
  34. var fallback = SKFontManager.Default.MatchCharacter(fontFamily?.Name, (SKFontStyleWeight)fontWeight,
  35. SKFontStyleWidth.Normal, (SKFontStyleSlant)fontStyle, _bcp47, codepoint);
  36. typeface = new Typeface(fallback?.FamilyName ?? _defaultFamilyName, fontStyle, fontWeight);
  37. return true;
  38. }
  39. public IGlyphTypefaceImpl CreateGlyphTypeface(Typeface typeface)
  40. {
  41. SKTypeface skTypeface;
  42. switch (typeface.FontFamily.Name)
  43. {
  44. case FontFamily.DefaultFontFamilyName:
  45. case "微软雅黑": //font family name
  46. skTypeface = SKTypeface.FromFamilyName(_defaultTypeface.FontFamily.Name); break;
  47. default:
  48. skTypeface = SKTypeface.FromFamilyName(typeface.FontFamily.Name,
  49. (SKFontStyleWeight)typeface.Weight, SKFontStyleWidth.Normal, (SKFontStyleSlant)typeface.Style);
  50. break;
  51. }
  52. return new GlyphTypefaceImpl(skTypeface);
  53. }
  54. }

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

  1. /// <summary>
  2. /// override RegisterServices register custom service
  3. /// </summary>
  4. public override void RegisterServices()
  5. {
  6. AvaloniaLocator.CurrentMutable.Bind<IFontManagerImpl>().ToConstant(new CustomFontManagerImpl());
  7. base.RegisterServices();
  8. }

 

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

原文链接:http://www.cnblogs.com/joyandjoys/p/14346935.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号