WPF Fonts.GetFontFamilies() 缓存字体列表,如何清除缓存?

发布于 2024-10-29 00:16:33 字数 508 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘寄 2024-11-05 00:16:33

我相信您可能需要禁用字体缓存服务,因为它可以在需要时自动启动。

编辑:

您可能必须自己获取 FontFamily 对象的列表,如下所示:

private static FontFamily CreateFontFamily(string path) {
    Uri uri;
    if (!Uri.TryCreate(path, UriKind.Absolute, out uri))
        throw new ArgumentException("Must provide a valid location", "path");

    return new FontFamily(uri, string.Empty);
}

public static IEnumerable<FontFamily> GetNonCachedFontFamilies(string location) {
    if (string.IsNullOrEmpty("location"))
        throw new ArgumentException("Must provide a location", "location");

    DirectoryInfo directoryInfo = new DirectoryInfo(location);
    if (directoryInfo.Exists) {
        FileInfo[] fileInfos = directoryInfo.GetFiles("*.?tf");
        foreach (FileInfo fileInfo in fileInfos)
            yield return CreateFontFamily(fileInfo.FullName);
    }
    else {
        FileInfo fileInfo = new FileInfo(location);
        if (fileInfo.Exists)
            yield return CreateFontFamily(location);
    }
}

姓氏可能存在一些问题,但上面的内容应该可以为您提供大部分相关信息。

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:

private static FontFamily CreateFontFamily(string path) {
    Uri uri;
    if (!Uri.TryCreate(path, UriKind.Absolute, out uri))
        throw new ArgumentException("Must provide a valid location", "path");

    return new FontFamily(uri, string.Empty);
}

public static IEnumerable<FontFamily> GetNonCachedFontFamilies(string location) {
    if (string.IsNullOrEmpty("location"))
        throw new ArgumentException("Must provide a location", "location");

    DirectoryInfo directoryInfo = new DirectoryInfo(location);
    if (directoryInfo.Exists) {
        FileInfo[] fileInfos = directoryInfo.GetFiles("*.?tf");
        foreach (FileInfo fileInfo in fileInfos)
            yield return CreateFontFamily(fileInfo.FullName);
    }
    else {
        FileInfo fileInfo = new FileInfo(location);
        if (fileInfo.Exists)
            yield return CreateFontFamily(location);
    }
}

There may be some issues with the family name, but the above should get you most of the relevant information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文