如何更改ClassificationFormatDefinition

问题描述 投票:0回答:1

我的Visual Studio扩展(VSIX)派生自Ook语言示例(找到here)。基本上,我有以下ClassificationFormatDefinition功能loadSavedColor加载用户配置的颜色。一切正常。

[Name("some_unique_name")]
internal sealed class OokE : ClassificationFormatDefinition
{
    public OokE()
    {
        DisplayName = "ook!"; //human readable version of the name
        ForegroundColor = loadSavedColor();
    }
}

问题:在用户配置了新颜色后,我想使类OokE的现有实例无效或更改现有实例并设置ForegroundColor。但无论我做什么,语法颜色都不会更新。

我试过了:

  1. 获取类OokE的引用并更新ForegroundColor
  2. 使相应的ClassificationTypeDefinition无效: [Export(typeof(ClassificationTypeDefinition))] [Name(“ook!”)] internal static ClassificationTypeDefinition ookExclamation = null;
c# visual-studio vsix
1个回答
1
投票

经过几个小时的筛选代码,我可以创建一些有用的东西。下面用UpdateFont调用colorKeyName的方法等于“some_unique_name”就可以了。我希望它对某人有用。

private void UpdateFont(string colorKeyName, Color c)
{
    var guid2 = Guid.Parse("{A27B4E24-A735-4d1d-B8E7-9716E1E3D8E0}");
    var flags = __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS | __FCSTORAGEFLAGS.FCSF_PROPAGATECHANGES;
    var store = GetService(typeof(SVsFontAndColorStorage)) as IVsFontAndColorStorage;

    if (store.OpenCategory(ref guid2, (uint)flags) != VSConstants.S_OK) return;
    store.SetItem(colorKeyName, new[]{ new ColorableItemInfo
        {
            bForegroundValid = 1,
            crForeground = (uint)ColorTranslator.ToWin32(c)
        }});
    store.CloseCategory();
}

设置新颜色后,您需要使用以下代码清除缓存:

IVsFontAndColorCacheManager cacheManager = this.GetService(typeof(SVsFontAndColorCacheManager)) as IVsFontAndColorCacheManager;
cacheManager.ClearAllCaches();
var guid = new Guid("00000000-0000-0000-0000-000000000000");
cacheManager.RefreshCache(ref guid);
guid = new Guid("{A27B4E24-A735-4d1d-B8E7-9716E1E3D8E0}"); // Text editor category
© www.soinside.com 2019 - 2024. All rights reserved.