UWP - 仅在设计器中应用的字体
UWP - Font only applied in Designer
我的主要 UWP 应用程序 (Imp.Dash) 引用了一个 class 库 (Imp.Dash.Cook)。在上述 class 库的一个页面中,我有以下内容 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"/>
在尝试更改字体时,只有第三行有影响。字体确实改变了,但只在设计器中。在运行时,我什么也得不到。没有输出错误或类似错误。
有没有人知道我做错了什么,或者我该如何调试它?
该字体是一个 .ttf 字体,位于我的 class 库的 Resources
文件夹中。它设置为 Content
和 Do not copy
。后者没有轴承,即使设置为 Copy Always
。我也试过将它放在主项目中,在 Fonts
.
下
我对图像资源有类似的问题,但在这种情况下它不是嵌入式资源。 (参见 )
在 UWP 应用程序中,如果我们想使用另一个 class 库中的一些资源,我们需要使用如下 ms-appx:
方案:
<TextBlock FontFamily="ms-appx:///Imp.Dash.Cook/Fonts/ARCADE_I.TTF#Arcade Interlaced"
FontSize="40"
Text="Banana" />
在此示例中,.ttf
文件位于 Imp.Dash.Cook
class 库的 Fonts
文件夹中。
如果我们在 FontFamily
中使用了错误的 URI,应用程序将无法获取字体文件,如果系统没有安装此字体,应用程序将只使用默认字体。所以我们必须将字体添加到应用程序中。
对于你的情况,我不确定为什么字体只适用于设计器。但是我构建了 a simple sample in GitHub 自定义字体应用于设计器和运行时。你可以检查一下。
此外,如果您的 class 库与您的主要 UWP 应用程序不在同一个解决方案中,您必须检查 "Generate library layout" 选项 在 class 库的属性页面下构建 配置。
因为在WinRT环境下,资源不再嵌入到程序集中,而是作为内容放在dll旁边。所以我们需要生成库布局,方便我们在其他项目中引用dll。
我的主要 UWP 应用程序 (Imp.Dash) 引用了一个 class 库 (Imp.Dash.Cook)。在上述 class 库的一个页面中,我有以下内容 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"/>
在尝试更改字体时,只有第三行有影响。字体确实改变了,但只在设计器中。在运行时,我什么也得不到。没有输出错误或类似错误。
有没有人知道我做错了什么,或者我该如何调试它?
该字体是一个 .ttf 字体,位于我的 class 库的 Resources
文件夹中。它设置为 Content
和 Do not copy
。后者没有轴承,即使设置为 Copy Always
。我也试过将它放在主项目中,在 Fonts
.
我对图像资源有类似的问题,但在这种情况下它不是嵌入式资源。 (参见
在 UWP 应用程序中,如果我们想使用另一个 class 库中的一些资源,我们需要使用如下 ms-appx:
方案:
<TextBlock FontFamily="ms-appx:///Imp.Dash.Cook/Fonts/ARCADE_I.TTF#Arcade Interlaced"
FontSize="40"
Text="Banana" />
在此示例中,.ttf
文件位于 Imp.Dash.Cook
class 库的 Fonts
文件夹中。
如果我们在 FontFamily
中使用了错误的 URI,应用程序将无法获取字体文件,如果系统没有安装此字体,应用程序将只使用默认字体。所以我们必须将字体添加到应用程序中。
对于你的情况,我不确定为什么字体只适用于设计器。但是我构建了 a simple sample in GitHub 自定义字体应用于设计器和运行时。你可以检查一下。
此外,如果您的 class 库与您的主要 UWP 应用程序不在同一个解决方案中,您必须检查 "Generate library layout" 选项 在 class 库的属性页面下构建 配置。
因为在WinRT环境下,资源不再嵌入到程序集中,而是作为内容放在dll旁边。所以我们需要生成库布局,方便我们在其他项目中引用dll。