查看: 96|回复: 0

dotnet 解决 Avalonia 在 OpenKylin 麒麟系统运行找不到默认字体启动失败

[复制链接]

1

主题

0

回帖

0

积分

积极分子

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2011-2-12
发表于 2024-9-12 08:50:00 | 显示全部楼层 |阅读模式

本文的解决方法由 walterlv 提供,我只是代为记录的工具人

在 OpenKylin 系统启动 Avalonia 应用失败,异常信息是 System.InvalidOperationException: Default font family name can't be null or empty.

启动失败的异常堆栈信息如下

System.InvalidOperationException: Default font family name can't be null or empty.
  at Avalonia.Media.FontManager..ctor(IFontManagerImpl platformImpl)
  at Avalonia.Media.FontManager.get_Current()
  ...

修复方法,添加默认字体配置,代码如下

        appBuilder.With(new FontManagerOptions()
        {
            DefaultFamilyName = "Noto Sans CJK SC",
            FontFallbacks =
            [
                new FontFallback { FontFamily = "文泉驿正黑" },
                new FontFallback { FontFamily = "DejaVu Sans" },
            ],
        });

在我代码里面,加上之后的 Program.cs 代码如下。以下代码使用的是 11.0.6 的 Avalonia 版本

internal class Program
{
    // Initialization code. Don't use any Avalonia, third-party APIs or any
    // SynchronizationContext-reliant code before AppMain is called: things aren't initialized
    // yet and stuff might break.
    [STAThread]
    public static void Main(string[] args) =>
        BuildAvaloniaApp()
            .StartWithClassicDesktopLifetime(args);

    // Avalonia configuration, don't remove; also used by visual designer.
    public static AppBuilder BuildAvaloniaApp()
        => AppBuilder.Configure<App>()
            .UsePlatformDetect()
            .WithInterFont()
            .LogToTrace()
            // 修复麒麟丢失字体
            .With(new FontManagerOptions()
            {
                DefaultFamilyName = "Noto Sans CJK SC",
                FontFallbacks =
                [
                    new FontFallback { FontFamily = "文泉驿正黑" },
                    new FontFallback { FontFamily = "DejaVu Sans" },
                ],
            });
}

参考文档:

  • OpenKylin上运行Avalonia应用 - louzi - 博客园
  • https://github.com/AvaloniaUI/Avalonia/issues/16612
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部