技术开发 频道

VS2010 Extension特性实践:Feature开发

  【IT168 技术文档】首先,要想建立VS Extension工程,你需要安装VS2010 SDK,目前是Beta2版本,你可以到这里可以下载:http://go.microsoft.com/fwlink/?LinkID=165597),这里我是通过Editor Text Adornment模板创建的工程,嗯,我就不详细写如何通过模板创建自己Extension工程了。

  建好工程以后,会自动生成TextViewCreationListener,这里实现了IWpfTextViewCreationListener接口,并通过MEF导出IWpfTextViewCreationListener对象:

  [TextViewRole("DOCUMENT")]

  [Export(
typeof(IWpfTextViewCreationListener))]

  [ContentType(
"text")]

  
internal sealed class PETextViewCreationListener : IWpfTextViewCreationListener

  {

  
void IWpfTextViewCreationListener.TextViewCreated(IWpfTextView textView)

  {

  
//...

  }

  }

  这样VS就会在合适的时候调用IWpfTextViewCreationListener.TextViewCreated方法来通知文字编辑的状态改变。

  为了实现浮动一个自己的工具栏,这里还需要导出一个AdornmentLayerDefinition,并通过Order Attribute来定制这个Adornment层的显示位置和次序:

  [Name("QuickToolbarAdornmentLayer")]

  [Order(After
= "Text")]

  [Export(
typeof(AdornmentLayerDefinition))]

  
public AdornmentLayerDefinition QuickToolbarLayerDefinition

  {

  
get;

  
set;

  }

  这里的Name Attribute很重要,以后的代码中要获取我们的AdornmentLayer就得靠它了:

  this._adornmentLayer = this._textView.GetAdornmentLayer("QuickToolbarAdornmentLayer");

  扯得远了,回到IWpfTextViewCreationListener.TextViewCreated,通过这里,可以得到一个IWpfTextView,

  这是所有操作的目标和展现,另外,还需要挂他的Closed、LayoutChanged、MouseHovered、SelectionChanged等事件,以响应用户行为。

  由于我们要通过工具栏操作代码,所以需要通过MEF导入IEditorOperationsFactoryService:

  [Import]

  
internal IEditorOperationsFactoryService EditorOperationsFactoryService

  {

  
get;

  
set;

  }

  这样就可以在IWpfTextViewCreationListener.TextViewCreated中通过IEditorOperationsFactoryService.GetEditorOperations(ITextView)来获得IEditorOperations,有了它,就可以方便快捷的编辑代码了。

0
相关文章