3

I couldn't find any guidance on Microsoft websites or on the winui github. I'm working on a WinUI 3 application, and have previously worked on WPF. I tried setting a default FontFamily for my application at the highest level using WPF methodologies, but it doesn't seem to work.

For example, in WPF I would include the FontFamily as a resource in the App.xaml.cs file.

<ResourceDictionary>
    <FontFamily x:Key="MyCustomFont">pack://application:,,,/MyAssembly;component/Fonts/#CustomFontName</FontFamily
    <ResourceDictionary.MergedDictionaries>
        <!-- Removed for brevity -->
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Then I would set this accordingly in my Window and all textblocks etc. would change to my custom font:

<Window FontFamily="{StaticResource MyCustomFont}">

I tried to do the same in WinUI but there is no FontFamily dependency property anymore on the Window, so I'm not sure how to do it.

Any help would be greatly appreciated!

Tam Bui
  • 2,940
  • 2
  • 18
  • 27

1 Answers1

3

After further digging through online help, I found a strategy that worked for WinUI applications.

Add the following to your App.xaml.cs and your custom font will now be the Font that you can use as default on other UIElements such as TextBlock, TextBox, etc.

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
            <!-- Other merged dictionaries here -->
        </ResourceDictionary.MergedDictionaries>
        <!-- Other app resources here -->
        <ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary x:Key="Default">
                <FontFamily x:Key="MyCustomFont">ms-appx:///MyOtherAssembly/Subfolder/fontfile.ttf#MyCustomName</FontFamily>
                <Style TargetType="TextBlock">
                   <Setter Property="FontFamily" Value="{StaticResource MyCustomFont}"/>
                </Style>
            </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>
    </ResourceDictionary>
</Application.Resources>
Tam Bui
  • 2,940
  • 2
  • 18
  • 27
  • 1
    Is there any official msft documentation about this ? – IronHide Feb 21 '22 at 08:26
  • This is silently failing. How are the fonts supposed to be added to the project? Resources? Content? – Emperor Eto Jun 13 '22 at 17:36
  • 2
    @PeterMoore It's been awhile since I posted this, and I have since lost my sample app. But I just did another test and found my instructions to be incomplete. See the update. Also to answer your question, it works if you add the fonts as Build Action "None" or "Content". I couldn't get it to work as "Resource", for some reason it kept removing this setting on my VS project whenever I selected it. – Tam Bui Jun 15 '22 at 17:42
  • 1
    Thank you! I fiddled with it for several hours the other day and got it working but it's good to have the complete example up for others. Boy they really don't make this easy do they? – Emperor Eto Jun 15 '22 at 22:27
  • 1
    I found out how to get it to work if you add the fonts as Build Action "Resource". You need to modify the FontFamily from using 'ms-appx', and instead use the prefix 'ms-resource'. – Tam Bui Feb 28 '23 at 00:43