diff --git a/src/Avalonia.Base/Media/FontManager.cs b/src/Avalonia.Base/Media/FontManager.cs
index dfce362297b..c56aae702c5 100644
--- a/src/Avalonia.Base/Media/FontManager.cs
+++ b/src/Avalonia.Base/Media/FontManager.cs
@@ -44,26 +44,7 @@ public FontManager(IFontManagerImpl platformImpl)
///
/// Get the current font manager instance.
///
- public static FontManager Current
- {
- get
- {
- var current = AvaloniaLocator.Current.GetService();
-
- if (current != null)
- {
- return current;
- }
-
- var fontManagerImpl = AvaloniaLocator.Current.GetRequiredService();
-
- current = new FontManager(fontManagerImpl);
-
- AvaloniaLocator.CurrentMutable.Bind().ToConstant(current);
-
- return current;
- }
- }
+ public static FontManager Current => AvaloniaLocator.Current.GetRequiredService();
///
/// Gets the system's default font family.
diff --git a/src/Avalonia.Controls/Application.cs b/src/Avalonia.Controls/Application.cs
index 0bad04bb23d..c0a201ccd19 100644
--- a/src/Avalonia.Controls/Application.cs
+++ b/src/Avalonia.Controls/Application.cs
@@ -278,8 +278,10 @@ public virtual void RegisterServices()
.Bind().ToConstant(this)
.Bind().ToConstant(this)
.Bind().ToConstant(this)
+ .Bind().ToLazy(() => new FontManager(
+ AvaloniaLocator.Current.GetRequiredService()))
.Bind().ToConstant(InputManager)
- .Bind< IToolTipService>().ToConstant(new ToolTipService(InputManager))
+ .Bind().ToConstant(new ToolTipService(InputManager))
.Bind().ToTransient()
.Bind().ToConstant(DragDropDevice.Instance);
diff --git a/tests/Avalonia.Base.UnitTests/Media/FontManagerTests.cs b/tests/Avalonia.Base.UnitTests/Media/FontManagerTests.cs
index fe72a9dfd1b..9d459c09086 100644
--- a/tests/Avalonia.Base.UnitTests/Media/FontManagerTests.cs
+++ b/tests/Avalonia.Base.UnitTests/Media/FontManagerTests.cs
@@ -28,13 +28,17 @@ public void Should_Create_Single_Instance_Typeface()
[Fact]
public void Should_Throw_When_Default_FamilyName_Is_Null_And_Installed_Font_Family_Names_Is_Empty()
{
- using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface
- .With(fontManagerImpl: new HeadlessFontManagerWithMultipleSystemFontsStub(
- installedFontFamilyNames: new string[] { },
- defaultFamilyName: null))))
+ // Wrap whole UnitTestApplication in the Assert.Throws, because disposal will try to access FontManager.Current again.
+ Assert.Throws(() =>
{
- Assert.Throws(() => FontManager.Current);
- }
+ using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface
+ .With(fontManagerImpl: new HeadlessFontManagerWithMultipleSystemFontsStub(
+ installedFontFamilyNames: new string[] { },
+ defaultFamilyName: null))))
+ {
+ Assert.NotNull(FontManager.Current);
+ }
+ });
}
[Fact]
diff --git a/tests/Avalonia.RenderTests/TestRenderHelper.cs b/tests/Avalonia.RenderTests/TestRenderHelper.cs
index 53ef16a6e71..e29e1d35855 100644
--- a/tests/Avalonia.RenderTests/TestRenderHelper.cs
+++ b/tests/Avalonia.RenderTests/TestRenderHelper.cs
@@ -41,13 +41,15 @@ static class TestRenderHelper
static TestRenderHelper()
{
#if AVALONIA_SKIA
- SkiaPlatform.Initialize();
+ SkiaPlatform.Initialize();
#else
Direct2D1Platform.Initialize();
#endif
AvaloniaLocator.CurrentMutable
.Bind()
- .ToConstant(s_dispatcherImpl);
+ .ToConstant(s_dispatcherImpl)
+ .Bind().ToLazy(() => new FontManager(
+ AvaloniaLocator.Current.GetRequiredService()));
AvaloniaLocator.CurrentMutable.Bind().ToConstant(new StandardAssetLoader());
}
diff --git a/tests/Avalonia.UnitTests/UnitTestApplication.cs b/tests/Avalonia.UnitTests/UnitTestApplication.cs
index 296c62d5af1..18b7723cbbf 100644
--- a/tests/Avalonia.UnitTests/UnitTestApplication.cs
+++ b/tests/Avalonia.UnitTests/UnitTestApplication.cs
@@ -73,19 +73,22 @@ public override void RegisterServices()
.Bind().ToFunc(Services.KeyboardNavigation ?? (() => null))
.Bind().ToConstant(Services.Platform)
.Bind().ToConstant(Services.RenderInterface)
- .Bind().ToConstant(Services.FontManagerImpl)
.Bind().ToConstant(Services.TextShaperImpl)
.Bind().ToConstant(Services.DispatcherImpl)
.Bind().ToConstant(Services.StandardCursorFactory)
.Bind().ToConstant(Services.WindowingPlatform)
.Bind().ToSingleton()
.Bind().ToSingleton()
- .Bind().ToConstant(Services.AccessKeyHandler)
- ;
-
- // This is a hack to make tests work, we need to refactor the way font manager is registered
- // See https://github.com/AvaloniaUI/Avalonia/issues/10081
- AvaloniaLocator.CurrentMutable.Bind().ToConstant((FontManager)null!);
+ .Bind().ToConstant(Services.AccessKeyHandler);
+
+ if (Services.FontManagerImpl is not null)
+ {
+ AvaloniaLocator.CurrentMutable
+ .Bind().ToConstant(Services.FontManagerImpl)
+ .Bind().ToLazy(() => new FontManager(
+ AvaloniaLocator.Current.GetRequiredService()));
+ }
+
var theme = Services.Theme?.Invoke();
if (theme is Style styles)