2

I have a class library (Imp.Dash.Cook) referenced by my main UWP app (Imp.Dash). In a page in said class library, I have the following XAML:

<TextBlock Text="Banana" FontFamily="/Imp.Dash;component/Fonts/Portmanteau Regular.ttf#Portmanteau"/>
<TextBlock Text="Banana" FontFamily="/Imp.Dash.Cook;component/Resources/Portmanteau Regular.ttf#Portmanteau"/>
<TextBlock Text="Banana" FontFamily="Resources/Portmanteau Regular.ttf#Portmanteau"/><!-- Works in Designer-->
<TextBlock Text="Banana" FontFamily="Fonts/Portmanteau Regular.ttf#Portmanteau"/>

In trying to change the font, only the third line has any bearing. The font is indeed changed, but only in the designer. On runtime, I get nothing. No errors in output or similar.

Does anyone have any ideas what I'm doing wrong, or how I can debug it?

The font is a .ttf located in the Resources folder of my class library. It is set to Content and Do not copy. The latter has no bearing, even when set to Copy Always. I've also tried placing it in the main project, under Fonts.

I had a similar issue with image resources, but in this case it is not an embedded resource. (See UWP - Load image in class library)

Jay Zuo
  • 15,653
  • 2
  • 25
  • 49
Troels Larsen
  • 4,462
  • 2
  • 34
  • 54
  • Check the output window for any binding errors/, you can check it via View->Output – Carbine Feb 17 '16 at 19:27
  • Did you perform a project clean & rebuild? – fillobotto Feb 17 '16 at 19:28
  • There are no Output errors or anything to indicate a problem. And yes, I've cleaned, rebuilt, restarted. – Troels Larsen Feb 17 '16 at 22:36
  • It is the curse of declarative programming. There is no identifiable statement in your program that helps the debugger say "this is wrong!" and cue you to modify it. So no error and no exception is generated, there is no way to debug it, it just lets the font mapper come up with something else. Yes, it sucks. – Hans Passant Feb 17 '16 at 23:37
  • @HansPassant As much as I love XAML, I agree with you. Referencing resources of any kind is a pain point in both UWP and WPF. I'm not sure why it couldn't throw an exception when it fails to locate the font though. Just some output similar to a binding error on runtime would suit me fine. – Troels Larsen Feb 17 '16 at 23:41
  • Funny enough, I had absolutely opposite experience: XAML Designer didn't apply custom fonts at class libraries, however in run time they were rendered correctly (VS2019) – Yury Schkatula Dec 02 '21 at 09:42

1 Answers1

4

In an UWP app, if we want to use some assets in another class library, we need use the ms-appx: scheme like following:

<TextBlock FontFamily="ms-appx:///Imp.Dash.Cook/Fonts/ARCADE_I.TTF#Arcade Interlaced"
           FontSize="40"
           Text="Banana" />

In this example, the .ttf file is located in the Fonts folder of Imp.Dash.Cook class library.

If we use a wrong URI in FontFamily, the application won't get the font file and if the system doesn’t have this font installed, the application will just use the default font. So we have to add the font to the application.

For your case, I'm not sure why font only applied in designer. However I build a simple sample in GitHub that custom fonts applied both in designer and runtime. You can have a check.

Besides, if your class library is not in the same solution with your main UWP app, you must check the "Generate library layout" option in the Build configuration under the class library's Properties page.

Because in WinRT environment, the resources are no longer embedded in the assembly but are placed next to the dll as content. So we need to generate library layout so that we can reference the dll in other project conveniently.

Jay Zuo
  • 15,653
  • 2
  • 25
  • 49
  • Thanks for the detailed explanation. I will try this when I get home from work. – Troels Larsen Feb 18 '16 at 08:48
  • @jay-zuo-msft: Thank you very much for the sample. I was not able to use the font I had intended, but you example lead me to get everything working. Apparently, there is *something* in that font UWP doesn't like. I don't know enough about OpenType vs TrueType to know if that might have been it. – Troels Larsen Feb 18 '16 at 19:35
  • What is #Arcade Interlaced for? The font file should only be ms-appx:///Imp.Dash.Cook/Fonts/ARCADE_I.TTF where did you get the #name from? – Lance Aug 10 '17 at 16:13
  • 1
    @Lance It's the name of the font family stored in ARCADE_I.ttf. A font file could define more than one font family, for this reason, it's necessary to indicate the name of the one you wish to use along with the file name. `ms-appx:///Assets/Fonts/FontFilename.ttf#FontFamily` – fourwhey Mar 27 '18 at 12:48