# Avalonia項目在OpenKylin運行踩坑 本篇博客記錄OpenKylin開源操作系統中運行Avalonia項目遇到的各種問題,會一直更新,最新的內容請點擊文末的鏈接跳轉到我的[博客原文地址](https://www.raokun.top/archives/avalonia-xiang-m ...
Avalonia項目在OpenKylin運行踩坑
本篇博客記錄OpenKylin開源操作系統中運行Avalonia項目遇到的各種問題,會一直更新,最新的內容請點擊文末的鏈接跳轉到我的博客原文地址查看。
1.Default font family name can't be null or empty
報錯內容:
Unhandled exception. System.InvalidOperationException: Default font family name can't be null or empty.
at Avalonia.Media.FontManager..ctor(IFontManagerImpl platformImpl) in /_/src/Avalonia.Base/Media/FontManager.cs:line 37
at Avalonia.Media.FontManager.get_Current() in /_/src/Avalonia.Base/Media/FontManager.cs:line 59
at Avalonia.AppBuilder.<>c__DisplayClass61_0.<ConfigureFonts>b__0(AppBuilder appBuilder) in /_/src/Avalonia.Controls/AppBuilder.cs:line 256
at Avalonia.AppBuilder.SetupUnsafe() in /_/src/Avalonia.Controls/AppBuilder.cs:line 310
at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime(AppBuilder builder, String[] args, ShutdownMode shutdownMode) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 219
at TerraMours.Chat.Ava.Program.Main(String[] args)
報錯分析:
根據報錯信息,應用程式在使用 Avalonia UI 框架時遇到了問題。報錯信息表明預設字體系列名稱為空或為空字元串。
可以通過以下命令查看系統已安裝的字體列表:
fc-list
重點issue
這是一個很久的問題,參考這個issue
https://github.com/AvaloniaUI/Avalonia/issues/4427
嘗試第一個解決方案(0.10版本及以前有效)
網上的解決方案,針對0.10版本及以前有效 解決方案一
由於 11.0.0 更改了 Avalonia.Skia.GlyphTypefaceImpl 的訪問級別,從公共訪問級別更改為內部訪問級別,因此上述實現 Avalonia.Platform.IFontManagerImpl 的示例代碼現在均不可用。
V11版本解決方案
1.修改環境變數 export Lang=en_ US.UTF-8
https://github.com/AvaloniaUI/Avalonia/issues/12140
將環境變數設置 export Lang=en_ US.UTF-8
1.打開終端。
2.運行以下命令來查看當前的 LANG
環境變數的值:
echo $LANG
3.如果當前的 LANG
值不是 en_US.UTF-8
,就需要設置它為 en_US.UTF-8
。可以使用以下命令來設置:
export LANG=en_US.UTF-8
4.使用 echo
命令驗證環境變數是否已更改:
echo $LANG
5.如果您希望永久設置 LANG
變數,可以將上述命令添加到您的 shell 配置文件中(如 ~/.bashrc
、~/.bash_profile
等),這樣每次啟動終端時都會自動設置。
代碼中修改掉linux不支持的字體
將字體修改後
<Setter Property="FontFamily" Value="宋體,國標黑體,Simsun,微軟雅黑,Microsoft YaHei,蘋方-簡,宋體-簡" />
linux中沒有宋體,會自動選擇後面的國標黑體
成果展示
2.Cannot access a disposed context instance
報錯記錄
Unhandled exception. System.ObjectDisposedException: Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
Object name: 'ChatDbcontext'.
at Microsoft.EntityFrameworkCore.DbContext.CheckDisposed()
at Microsoft.EntityFrameworkCore.DbContext.get_ContextServices()
at Microsoft.EntityFrameworkCore.DbContext.get_Model()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Collections.Generic.IEnumerable<TEntity>.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Collections.ObjectModel.ObservableCollection`1..ctor(IEnumerable`1 collection)
at TerraMours.Chat.Ava.Views.MainWindow.MainWindow_Loaded(Object sender, RoutedEventArgs e)
at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state)
at Avalonia.Threading.SendOrPostCallbackDispatcherOperation.InvokeCore() in /_/src/Avalonia.Base/Threading/DispatcherOperation.cs:line 412
at Avalonia.Threading.DispatcherOperation.Execute() in /_/src/Avalonia.Base/Threading/DispatcherOperation.cs:line 268
at Avalonia.Threading.Dispatcher.ExecuteJob(DispatcherOperation job) in /_/src/Avalonia.Base/Threading/Dispatcher.Queue.cs:line 126
at Avalonia.Threading.Dispatcher.ExecuteJobsCore() in /_/src/Avalonia.Base/Threading/Dispatcher.Queue.cs:line 181
at Avalonia.Threading.Dispatcher.Signaled() in /_/src/Avalonia.Base/Threading/Dispatcher.Queue.cs:line 134
at Avalonia.X11.X11PlatformThreading.CheckSignaled() in /_/src/Avalonia.X11/X11PlatformThreading.cs:line 119
at Avalonia.X11.X11PlatformThreading.RunLoop(CancellationToken cancellationToken) in /_/src/Avalonia.X11/X11PlatformThreading.cs:line 192
at Avalonia.Threading.DispatcherFrame.Run(IControlledDispatcherImpl impl) in /_/src/Avalonia.Base/Threading/DispatcherFrame.cs:line 117
at Avalonia.Threading.Dispatcher.PushFrame(DispatcherFrame frame) in /_/src/Avalonia.Base/Threading/Dispatcher.MainLoop.cs:line 53
at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken) in /_/src/Avalonia.Base/Threading/Dispatcher.MainLoop.cs:line 80
at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 127
at TerraMours.Chat.Ava.Program.Main(String[] args)
原因
在 Linux 環境下發生此錯誤可能是因為 Linux 環境的非同步處理方式與 Windows 環境有所不同。
解決方案
去掉using
public void CreateDatabase()
{
var context = new ChatDbcontext();
context.Database.Migrate();
VMLocator.ChatDbcontext = context;
}
然後,在其他需要使用 ChatDbcontext
的地方,使用 VMLocator.ChatDbcontext
來獲取上下文實例。確保不要在其他地方使用 using
語句或調用上下文實例的 Dispose
方法。
3.openkylin會自動切換到平板模式
而且切換不回來,程式點不了
解決方案
可以通過桌面側邊欄進行PC/平板模式切換,喚出側邊欄的方法有:
點擊任務欄上的側邊欄圖標;
快捷鍵 super + A;----super對應Windows鍵
通過觸摸手勢,在屏幕右邊緣向左短滑。
閱讀如遇樣式問題,請前往個人博客瀏覽: https://www.raokun.top
擁抱ChatGPT:https://ai.terramours.site
開源項目地址:https://github.com/raokun/TerraMours.Chat.Ava