diff --git a/src/Core/src/Fonts/FontManager.Windows.cs b/src/Core/src/Fonts/FontManager.Windows.cs index f1af4d246824..6f78d96da556 100644 --- a/src/Core/src/Fonts/FontManager.Windows.cs +++ b/src/Core/src/Fonts/FontManager.Windows.cs @@ -1,5 +1,4 @@ -#nullable enable -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; @@ -7,6 +6,8 @@ using Microsoft.Graphics.Canvas.Text; using Microsoft.Maui.ApplicationModel; using Microsoft.Maui.Storage; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Media; namespace Microsoft.Maui @@ -27,6 +28,9 @@ public class FontManager : IFontManager readonly IFontRegistrar _fontRegistrar; readonly IServiceProvider? _serviceProvider; + FontFamily? _defaultFontFamily; + double? _defaultFontSize; + /// /// Creates a new instance. /// @@ -37,15 +41,42 @@ public FontManager(IFontRegistrar fontRegistrar, IServiceProvider? serviceProvid { _fontRegistrar = fontRegistrar; _serviceProvider = serviceProvider; + + Application.Current.Resources.RegisterPropertyChangedCallback(Control.FontFamilyProperty, OnPropertyChanged); + Application.Current.Resources.RegisterPropertyChangedCallback(Control.FontSizeProperty, OnPropertyChanged); + } + + private void OnPropertyChanged(DependencyObject sender, DependencyProperty dp) + { + if (dp == Control.FontFamilyProperty) + { + _defaultFontFamily = (FontFamily)Application.Current.Resources[SystemFontFamily]; + } + else if (dp == Control.FontSizeProperty) + { + _defaultFontSize = (double)Application.Current.Resources[SystemFontSize]; + } } /// - public FontFamily DefaultFontFamily => - (FontFamily)UI.Xaml.Application.Current.Resources[SystemFontFamily]; + public FontFamily DefaultFontFamily + { + get + { + _defaultFontFamily ??= (FontFamily)Application.Current.Resources[SystemFontFamily]; + return _defaultFontFamily; + } + } /// - public double DefaultFontSize => - (double)UI.Xaml.Application.Current.Resources[SystemFontSize]; + public double DefaultFontSize + { + get + { + _defaultFontSize ??= (double)Application.Current.Resources[SystemFontSize]; + return _defaultFontSize.Value; + } + } /// public FontFamily GetFontFamily(Font font)