UWP - Font only applied in Designer

早过忘川 提交于 2019-12-22 18:43:29

问题


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)


回答1:


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.



来源:https://stackoverflow.com/questions/35465865/uwp-font-only-applied-in-designer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!