WPF Fonts.GetFontFamilies() 缓存字体列表,如何清除缓存?
在 WPF 中,以下代码返回给定位置中所有字体的列表:
foreach (var fontFamily in Fonts.GetFontFamilies(@"C:\Dummy\Fonts\"))
{
System.Diagnostics.Debug.WriteLine(fontFamily.Source);
}
问题是,如果您更改该文件夹的内容(添加或删除字体)并再次运行此代码,它会返回相同的列表(因为它缓存内部某处)。
在您退出应用程序之前,此缓存不会被清除!
有什么方法可以防止这种行为并始终让 WPF 在该时刻查找该文件夹中的字体吗?
注意:无论“Windows Presentation Foundation Font Cache 3.0.0.0”服务状态是启动还是停止,结果都是相同的。显然,该服务并未处理这种特定类型的缓存。
In WPF, the following code returns a list of all fonts in the given location:
foreach (var fontFamily in Fonts.GetFontFamilies(@"C:\Dummy\Fonts\"))
{
System.Diagnostics.Debug.WriteLine(fontFamily.Source);
}
The problem is if you change the content of that folder (add or remove a font) and run this code again, it returns the same list (as it is cached somewhere internally).
This cache will not be cleared until you exit the application!
Is there any way to prevent this behaviour and always make WPF to look into that folder for fonts at that moment?
NOTE: The result is the same regardless of "Windows Presentation Foundation Font Cache 3.0.0.0" service state is started or stopped. Apparently this specific type of cache is not being handled by the service.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您可能需要禁用字体缓存服务,因为它可以在需要时自动启动。
编辑:
您可能必须自己获取 FontFamily 对象的列表,如下所示:
姓氏可能存在一些问题,但上面的内容应该可以为您提供大部分相关信息。
I believe you may need to disable the font cache service, as it may be started automatically when needed.
EDIT:
You may have to get the list of FontFamily objects yourself like so:
There may be some issues with the family name, but the above should get you most of the relevant information.