Problem referencing a NuGet package with a custom font in WinUI 3

132 Views Asked by At

I created a NUGet package (winui 3) that has a set of icons and a custom font. I have set my font as ¨content" and "copy always" and my application displays the font correctly if the project containing the code is referenced directly.

Below is the example of how I referenced this in my App.xaml.

Example 1 (worked with a local reference to the code, but not as a .nupkg):

<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="FontIcon">
                   <Setter Property="FontFamily" Value="{StaticResource MyCustomFont}"/>
                </Style>
            </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>
    </ResourceDictionary>
</Application.Resources>

The expected result is for the font to be displayed. I also tried setting the font as "resource" and changing to this in App.xaml:

Example 2 (didn´t work):

<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-resource:///MyOtherAssembly/Subfolder/fontfile.ttf#MyCustomName</FontFamily>
                <Style TargetType="FontIcon">
                   <Setter Property="FontFamily" Value="{StaticResource MyCustomFont}"/>
                </Style>
            </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>
    </ResourceDictionary>
</Application.Resources>

My problem is that when the code is packaged as .nupkg and referenced as Example 1, the font isn't displayed. Is there a workaround available?

EDIT (full code for example 1):

<Application
    x:Class="WinUI3.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    >
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!-- WinUI Icon library reference -->
                <ResourceDictionary Source="ms-appx:///BP.Beat.WinUI.Icon/ResourceDictionary.xaml"/>

                <!-- WinUI Token library reference -->
                <ResourceDictionary Source="ms-appx:///BP.Beat.WinUI.Token/Themes/VeneerTheme.xaml"/>
                <!-- Other merged dictionaries here -->

                <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
                <ResourceDictionary Source="/Styles/FontSizes.xaml" />
                <ResourceDictionary Source="/Styles/Thickness.xaml" />
                <ResourceDictionary Source="/Styles/TextBlock.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <!--solution for custom Font to work in Winui3-->
            <ResourceDictionary.ThemeDictionaries>
                <ResourceDictionary x:Key="Default">
                    <FontFamily x:Key="BeatIconFont">ms-appx:///BP.Beat.WinUI.Icon/Assets/Fonts/BeatIconFont.ttf#BeatIconFont</FontFamily>
                    <Style TargetType="FontIcon">
                        <Setter Property="FontFamily" Value="{StaticResource BeatIconFont}"/>
                    </Style>
                </ResourceDictionary>
            </ResourceDictionary.ThemeDictionaries>
            <SolidColorBrush x:Key="WindowCaptionBackground">Transparent</SolidColorBrush>
            <SolidColorBrush x:Key="WindowCaptionBackgroundDisabled">Transparent</SolidColorBrush>
            
        </ResourceDictionary>
    </Application.Resources>
    
</Application>

Is there something I'm missing?

0

There are 0 best solutions below