How to only load current used font
Posted: 17 Sep 2024, 10:45
We're using different fonts depending on the language, but we want to only load the current font needed in order to save memory (some platforms have very limited RAM).
This is what we have in our Root View:
This is All.Font.xaml:
This is Default.Font.xaml:
This is Japanese.Font.xaml:
This is how we handle the resource dictionaries in the RootView code:
With this setup we've seen that all the fonts are currently being loaded into memory. Looks like that by just using noesis:Dependency the font will be loaded. Is there a way to define the fonts the project needs, but only load the specific font needed for each case?
This is what we have in our Root View:
Code: Select all
[...]
<noesis:Xaml.Dependencies>
<noesis:Dependency Source="/Assets/UI/Fonts/All.Font.xaml"/>
</noesis:Xaml.Dependencies>
[...]
Code: Select all
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions">
<FrameworkElement x:Key="Dependencies">
<noesis:Xaml.Dependencies>
<noesis:Dependency Source="Default/Default.Font.xaml"/>
<noesis:Dependency Source="JP/Japanese.Font.xaml"/>
<noesis:Dependency Source="KR/Korean.Font.xaml"/>
<noesis:Dependency Source="SC/SimplifiedChinese.Font.xaml"/>
<noesis:Dependency Source="TC/TraditionalChinese.Font.xaml"/>
</noesis:Xaml.Dependencies>
</FrameworkElement>
</ResourceDictionary>
Code: Select all
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<FontFamily x:Key="MainFont">
/Assets/UI/Fonts/#OurDefaultFont
</FontFamily>
</ResourceDictionary>
Code: Select all
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<FontFamily x:Key="MainFont">
/Assets/UI/Fonts/JP/#OurJapaneseFont
</FontFamily>
</ResourceDictionary>
Code: Select all
protected void UpdateFont(LocGeneralLanguage generalLanguage)
{
ResourceDictionary newFont = GetFont("Default/Default.Font.xaml");
switch (generalLanguage)
{
case LocGeneralLanguage.ja:
newFont = GetFont("JP/Japanese.Font.xaml");
break;
case LocGeneralLanguage.ko:
newFont = GetFont("KR/Korean.Font.xaml");
break;
case LocGeneralLanguage.zn_ch:
newFont = GetFont("SC/SimplifiedChinese.Font.xaml");
break;
case LocGeneralLanguage.zh_tw:
newFont = GetFont("TC/TraditionalChinese.Font.xaml");
break;
}
RefreshResources(newFont);
}
protected ResourceDictionary GetFont(string fontName)
{
if (ResourceDictionaries.TryGetValue(fontName, out ResourceDictionary resourceDictionaryFont))
{
return resourceDictionaryFont;
}
Uri uri = new Uri("Assets/UI/Fonts/" + fontName, UriKind.Relative);
resourceDictionaryFont = new ResourceDictionary { Source = uri };
ResourceDictionaries.Add(fontName, resourceDictionaryFont);
return resourceDictionaryFont;
}
private void RefreshResources(ResourceDictionary newFont)
{
Resources.MergedDictionaries.Clear();
Resources.MergedDictionaries.Add(newFont);
PersistentContext.GetPreSessionState().CurrentFont = newFont;
}