gpt4 book ai didi

wpf - 嵌入字体适用于设计人员,但不适用于应用程序

转载 作者:行者123 更新时间:2023-12-04 13:41:57 24 4
gpt4 key购买 nike

我正在构建一个 wpf 应用程序,我想在我的应用程序中使用 Open Sans Regular 字体。
我提到了 this嵌入字体的链接。我将 OpenSans Regular.ttf 文件添加到项目属性下的资源中。
然后我在我的申请中提到了它们,如下所述:

<Window x:Class="FontEmbeddingDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" FontFamily="Resources/#Open Sans">
<Window.Resources>

</Window.Resources>
<Grid>
<TextBlock Height="100" Text="This is test text." FontSize="14" FontFamily="Resources/#Open Sans"/>
</Grid>
</Window>
在 Visual Studio 的设计器中,我可以看到字体更改为 open sans,但是当我运行应用程序时,它采用系统默认字体(在我的系统中为 Arial)。
如果您需要任何其他信息,请告诉我。

最佳答案

来自 MSDN页:

When you add fonts as resources to your application, make sure you aresetting the <Resource> element, and not the elementin your application's project file. The <EmbeddedResource> element forthe build action is not supported.


因此,我想,您必须将此字体文件添加到您的项目中,就像添加任何其他文件并设置其 BuildAction 一样。至 Resource而不是将字体添加到项目属性下的资源,这将使其成为 EmbeddedResource。
编辑
阅读 this关于如何在 WPF 应用程序中使用自定义字体的优秀文章。
你也可以,通过@Sheridan 提到的方式让它工作 - 这是设置 BuildActionContent .但是,这种方法的问题是您将有松散的单独文件与二进制文件一起悬挂。用户可能会更改此文件,这可能会导致您的应用出现问题。最好用 ResourceBuildAction随着字体被捆绑到程序集中。

关于wpf - 嵌入字体适用于设计人员,但不适用于应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20219457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com