【发布时间】:2015-01-29 07:11:28
【问题描述】:
问题在于使 VS 扩展中的自定义编辑器看起来与当前主题所指示的不同。编辑器托管在对话框中,并且应该具有托管对话框定义的相同字体。
编辑器的内容类型定义如下:
[Export]
[Name("MyContent")]
[BaseDefinition("code")]
public static readonly ContentTypeDefinition ExportContentTypeDefinition = null;
还有一个分类类型定义:
[Export]
[Name("MyContentText")]
[BaseDefinition("text")]
public static readonly ClassificationTypeDefinition MyTextDefinition = null;
分类器提供者定义如下:
[Export(typeof(IClassifierProvider))]
[ContentType("MyContent")]
public class ClassifierProvider : IClassifierProvider
{
[Import]
public IClassificationTypeRegistryService ClassificationTypesRegistry { get; set; }
public IClassifier GetClassifier(ITextBuffer textBuffer)
{
return new Classifier(
ClassificationTypesRegistry.GetClassificationType("MyContentText"));
}
}
虽然分类器只是为任何快照提供相同的格式:
public class Classifier : IClassifier
{
private readonly IClassificationType _classificationType;
public Classifier(IClassificationType classificationType)
{
_classificationType = classificationType;
}
public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
{
return new [] { new ClassificationSpan(span, _classificationType)};
}
public event EventHandler<ClassificationChangedEventArgs> ClassificationChanged;
}
现在,在代码中,在创建编辑器时,我试图覆盖匹配的 IClassificationFormatMap 的属性:
var contentType = contentTypeRegistryService.GetContentType("MyContent");
var textBuffer = textBufferFactoryService.CreateTextBuffer(initialText, contentType);
var textView = textEditorFactoryService.CreateTextView(textBuffer);
...
var formatMap = classificationFomatMapService
.GetClassificationFormatMap("MyContentText");
formatMap.DefaultTextProperties = formatMap.DefaultTextProperties
.SetFontRenderingEmSize(dialog.FontSize)
.SetTypeface(
new Typeface(
dialog.FontFamily,
dialog.FontStyle,
dialog.FontWeight,
dialog.FontStretch));
但是,更改不会影响我的编辑器实例。
此外,classificationFomatMapService.GetClassificationFormatMap(ITextView) 重载返回的格式映射与我上面使用的重载返回的格式映射不同。并且更改另一种格式实例也会影响正在运行的 Visual Studio 实例中的所有代码编辑器,因此我必须得出结论,尽管我做出了努力,但 textView 还是会以某种方式映射到默认编辑器的分类。
我的问题是:我应该怎么做才能控制为自定义内容类型指定的自定义编辑器的文本外观?
【问题讨论】:
-
DefaultTextProperties适用于所有分类类型,因此这似乎是个问题。您是否尝试过SetTextProperties()? msdn.microsoft.com/en-us/library/… -
即使它们确实适用于所有类型,在我通过类型名称获得的格式映射上更改它们也没有任何可见的效果。因此,我想设置文本属性也不会产生任何影响。不过我会试试的,谢谢你的建议。
-
嗯...听起来“MyContentText”分类未应用于您的 TextView。这就是为什么改变它不会有任何明显的效果。
GetClassifier()在创建缓冲区时是否正在运行?此外,您正在编辑formatMap创建缓冲区。我相信你需要事先这样做。 -
GetClassifier 方法被调用,分类器的 GetClassificationSpans 也被调用,带有预期的文本。正如我所说,更改从 GetClassificationFormatMap(ITextView) 重载返回的格式映射的 DefaultTextProperties 甚至会立即影响主 VS 编辑器。所以缓冲区是否已经创建并不重要。
-
@galenus,我有same issue,但仅用于重量,另一种字体的字段应用得很好。您找到解决此问题的方法了吗?
标签: c# visual-studio visual-studio-2012