简体   繁体   中英

UWP - Font only applied in Designer

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 )

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM