我的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
。但无论我做什么,语法颜色都不会更新。
我试过了:
OokE
的引用并更新ForegroundColor
。ClassificationTypeDefinition
无效:
[Export(typeof(ClassificationTypeDefinition))] [Name(“ook!”)] internal static ClassificationTypeDefinition ookExclamation = null;经过几个小时的筛选代码,我可以创建一些有用的东西。下面用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