成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

詳解Visual Studio 2010 Extension的應用

開發 后端
關于Visual Studio 2010 Extension,很多開發者并不是很熟悉。在本篇文章中將詳細介紹,以供大家參考。

對于即將發布正式版的Visual Studio 2010,有一個Visual Studio 2010 Extension功能。平時大家關注Visual Studio 2010 Extension不多,希望通過本文能讓大家更好的了解。

最近Visual Studio 2010 Extension在Visual Studio Blog(http://blogs.msdn.com/visualstudio/)上提得很頻繁,于是也想翻來文檔研究研究,結果居然找了半天,居然沒有一丁點完整介紹這一塊的,于是,只好自己找著VS IDE上的模板提供的內容和Visual Studio Blog上的講解,一邊Reflector參演,一邊涂鴉一些代碼,準備實彈演練一下,但是覺得這個模板建出來的Extension也太簡單了,剛好看到AxTool(http://www.axtools.com/products-vs2010-extensions.php)有一個代碼編輯器擴展,也是VS Extension的,于是就照著這個,自己一步一步做一下。

首先,要想建立VS Extension工程,你需要安裝VS2010 SDK,目前是Beta2版本,你可以到這里可以下載:http://go.microsoft.com/fwlink/?LinkID=165597),這里我是通過Editor Text Adornment模板創建的工程,嗯,我就不詳細寫如何通過模板創建自己Extension工程了,如果你不熟悉這里,可以參考Quan To的這篇帖子——Building and publishing an extension for Visual Studio 2010。

建好工程以后,會自動生成TextViewCreationListener,這里實現了IWpfTextViewCreationListener接口,并通過MEF導出IWpfTextViewCreationListener對象:

  1. [TextViewRole("DOCUMENT")]  
  2. [Export(typeof(IWpfTextViewCreationListener))]  
  3. [ContentType("text")]  
  4. internal sealed class PETextViewCreationListener : IWpfTextViewCreationListener  
  5. {  
  6. void IWpfTextViewCreationListener.TextViewCreated(IWpfTextView textView)  
  7. {  
  8. //...  
  9. }  

這樣VS就會在合適的時候調用IWpfTextViewCreationListener.TextViewCreated方法來通知文字編輯的狀態改變。

為了實現浮動一個自己的工具欄,這里還需要導出一個AdornmentLayerDefinition,并通過Order Attribute來定制這個Adornment層的顯示位置和次序:

  1. [Name("QuickToolbarAdornmentLayer")]  
  2. [Order(After = "Text")]  
  3. [Export(typeof(AdornmentLayerDefinition))]  
  4. public AdornmentLayerDefinition QuickToolbarLayerDefinition  
  5. {  
  6. get;  
  7. set;  

這里的Name Attribute很重要,以后的代碼中要獲取我們的AdornmentLayer就得靠它了:

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

扯得遠了,回到IWpfTextViewCreationListener.TextViewCreated,通過這里,可以得到一個IWpfTextView,

這是所有操作的目標和展現,另外,還需要掛他的Closed、LayoutChanged、MouseHovered、SelectionChanged等事件,以響應用戶行為。

由于我們要通過工具欄操作代碼,所以需要通過MEF導入IEditorOperationsFactoryService:這樣就可以在IWpfTextViewCreationListener.TextViewCreated中通過IEditorOperationsFactoryService.GetEditorOperations(ITextView)來獲得IEditorOperations,有了它,就可以方便快捷的編輯代碼了。

接下來要實現工具欄的界面,這個就不多說了,建一個UserControl,里面放上ToolBar就搞定了。那么何時何地顯示這個ToolBar呢?這就要依賴IWpfTextView的SelectionChanged事件了,上面提到會掛這個事件就是為這里用的。

1 private void MayBeAdornmentShowCondition()
2 {
3 if (!this._textView.Selection.IsEmpty)
4 {
5 SnapshotPoint startPos = this._textView.Selection.Start.Position;
6 SnapshotPoint endPos = this._textView.Selection.End.Position;
7 IWpfTextViewLine textViewLineContainingBufferPosition = this._textView.GetTextViewLineContainingBufferPosition(startPos);
8 TextBounds characterBounds = textViewLineContainingBufferPosition.GetCharacterBounds(startPos);
9 TextBounds bounds2 = this._textView.GetTextViewLineContainingBufferPosition(endPos).GetCharacterBounds(endPos);
10 if (this._fromMouseHover)
11 {
12 this._mustHaveAdornmentDisplayed = true;
13 }
14 else
15 {
16 PELeftButtonMouseProcessor property = null;
17 try
18 {
19 property = this._textView.Properties.GetProperty<PELeftButtonMouseProcessor>(typeof(PELeftButtonMouseProcessor));
20 }
21 catch
22 {
23 }
24 this._mustHaveAdornmentDisplayed = (property != null)
25 && (property.IsLeftButtonDown
26 || ((DateTime.Now - property.LastLeftButtonDownTime).TotalMilliseconds < 400.0));
27 }
28 if (this._mustHaveAdornmentDisplayed)
29 {
30 TextBounds selectionBounds = !this._textView.Selection.IsReversed ? bounds2 : characterBounds;
31 int offset = 7;
32 double top = selectionBounds.Top + (!this._textView.Selection.IsReversed ?

 
(offset + textViewLineContainingBufferPosition.Height) : (-offset - this._adornmentUI.ActualHeight));
33 if (top < 0.0)
34 {
35 top = 0.0;
36 }
37 double left = characterBounds.Left + ((bounds2.Left - characterBounds.Left) / 2.0);
38 if ((left + this._adornmentUI.ActualWidth) > this._textView.ViewportWidth)
39 {
40 left = this._textView.ViewportWidth - this._adornmentUI.ActualWidth;
41 }
42 Canvas.SetTop(this._adornmentUI, top);
43 Canvas.SetLeft(this._adornmentUI, left);
44 long chars = 0L;
45 try
46 {
47 chars = this._textView.Selection.SelectedSpans[0].Span.Length;
48 }
49 catch
50 {
51 }
52 this._adornmentUI.SetStatus(chars);
53 this.RenderSelectionPopup();
54 }
55 }
56 else
57 {
58 this._mustHaveAdornmentDisplayed = false;
59 this._adornmentLayer.RemoveAdornmentsByTag(this._adornmentTag);
60 }
61 }
62
63 private void RenderSelectionPopup()
64 {
65 if (this._mustHaveAdornmentDisplayed)
66 {
67 IAdornmentLayerElement element = null;
68 try
69 {
70 element = this._adornmentLayer.Elements.First<IAdornmentLayerElement>(
71 (IAdornmentLayerElement ile) => ile.Tag.ToString() == this._adornmentTag);
72 }
73 catch (InvalidOperationException)
74 {
75 }
76 if (element == null)
77 {
78 this._adornmentLayer.AddAdornment(this._textView.Selection.SelectedSpans[0], this._adornmentTag, this._adornmentUI);
79 }
80 this._timer.Stop();
81 this._timer.Start();
82 }
83 }
84
85 private void selection_SelectionChanged(object sender, EventArgs e)
86 {
87 this._fromMouseHover = false;
88 this.MayBeAdornmentShowCondition();
89 }
90  
 

然后要注意的是IWpfTextView的Closed事件處理要記得取消所有掛這個事件等等收尾工作。

接下來編譯工程,打包VSIX就完成了,目前實現的主要Feature:
1、當在代碼編輯器中選擇一段文字,并將鼠標移到文字區域時,QuickToolbar會以半透明的方式“浮”文字的旁邊。
代碼段 2、當鼠標移到QuickToolbar區域,QuickToolbar會變成不透明,其上的按鈕會響應鼠標動作。
代碼段2
3、目前支持的操作有:

  • 剪切(Cut)
  • 復制(Copy)
  • 粘貼(Paste)
  • 刪除(Delete)
  • 減小縮進(Decrease Indent)
  • 增加縮進(Increase Indent)
  • 注釋代碼(Comment)
  • 取消注釋(Uncomment)
  • 等等

上面主要展示了如何使用MEF擴展VS2010,來擴展編輯控制和展現自己的UI;在實現QuickToolbar的時候,發現MEF僅僅提供了很基本的編輯控制,如果需要高級的操作,比如注釋選擇的代碼,就捉襟見肘,很是麻煩。

我將展示如何深入挖掘VS2010 Extension,使它成為鋒利的軍刀,而不是繡花枕頭。鑒于此,這里就從上面提到了的Feature——注釋和取消注釋選擇的代碼來剖析,希望可以為大家拓寬思路,更好的利用VS2010。

首先回顧一下上篇中的實現,當時是基于TextViewLine做注釋代碼的,這里有兩個潛在問題:其一,TextViewLine,顧名思義,是“可視區域”的行,所以如果選擇超出可視區域,超出的部分就沒有注釋掉;其二,當選擇的結束位置在行的結尾時,無法實現IDE注釋代碼后保持Caret在選擇結尾而不跳到下一行的行為,當嘗試自己重新選擇并移動Caret就會收到ITextSpanshot無效的異常。

上面提到了VS2010 Extension對編輯器的編輯行為的控制能力僅僅提供了通用的,比如Cut/Copy/Paste等等,而其他的諸如注釋/取消注釋代碼,添加、刪除、導航到Bookmark等程序員常用功能沒有暴露出來,具體可以參考IEditorOperations Interface(http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.text.operations.ieditoroperations_methods%28VS.100%29.aspx),這里的所有Member表達了其所支持的編輯操作。總之,這條路只有這么幾個目的地。

那么,還有其他方法嗎?貌似走到了死胡同了,但是當我們使用IDE時候,卻是可以很容易的通過Edit菜單找到所有的功能的,問題是,它們要怎樣才能為我所用呢?

我首先想到的是在VSSDK中找找,結果一個名字看起來很順眼的接口撞到眼里,它就是IVsUIShell Interface(http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.shell.interop.ivsuishell%28VS.100%29.aspx),MSDN上市這么說的:

This interface provides access to basic windowing functionality, including access to and creation of tool windows and document windows. provided by the environment.

也就是說這是一個由IDE提供的全局的Service,可以創建、訪問工具窗口和編輯窗口。瀏覽一下這個所有Member,發現了一個叫IVsUIShell.PostExecCommand(...)(http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.shell.interop.ivsuishell.postexeccommand%28VS.100%29.aspx)的方法,MSDN描述說通過它可以異步執行Command,那么,只要找到注釋代碼的Command,在通過這個接口就可以實現VS IDE一樣的注釋代碼的Feature了。酷斃了,就是它,當怎么得到它呢?現在請留心MSDN上的解釋,就是上面我使用紅色粗體表示出來的部分——這個由IDE提供的全局的Service,那么可以通過Package.GetGlobalService(...)(http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.shell.package.getglobalservice%28VS.100%29.aspx)來獲取:

IVsUIShell shell = Package.GetGlobalService(typeof(IVsUIShell)) as IVsUIShell;

接下來是找到自己需要Command,然后PostExecCommand就搞定了;而VS提供的Command有兩部分組成:Guid和CommandID,這個大部分都在VSConstants Class(http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.vsconstants%28VS.100%29.aspx)里面,以注釋代碼為例,其Guid是:VsConstants.VSStd2k,而CommandID是VSConstants.VSStd2kCmdID.COMMENTBLOCK。下面是我包裝的注釋和取消注釋的代碼片段:

  1.  public static void ProcessComments(bool comment)  
  2.  {  
  3.  IVsUIShell shell = Package.GetGlobalService(typeof(IVsUIShell)) as IVsUIShell;  
  4.  if (shell != null)  
  5.  {  
  6.  Guid std2k = VSConstants.VSStd2K;  
  7.  uint cmdId = comment ?   
  8. (uint)VSConstants.VSStd2KCmdID.COMMENT_BLOCK :  
  9. (uint)VSConstants.VSStd2KCmdID.UNCOMMENT_BLOCK;  
  10.  object arg = null;  
  11.  shell.PostExecCommand(ref std2k, cmdId, 0, ref arg);  
  12.  }  
  13.  } 

至此,我們通過VSSDK提供的能力,順利的挖掘出VS2010 Extension的部分寶藏,你是不是也有點心動,要自己去挖掘一點呢?

原文標題:VS2010 Extension實踐(2)

鏈接:http://www.cnblogs.com/winkingzhang/archive/2010/02/04/1663865.html

【編輯推薦】

  1. Visual Studio 2010中UML瀏覽器詳解
  2. 詳解Visual Studio 2010中WF 4.0的應用
  3. 細數Visual Studio 2010的11大新功能
  4. 詳解Visual Studio 2010中ASP.NET新增23項功能
  5. 詳細介紹Visual Studio 2010F#使用

 

責任編輯:彭凡 來源: 博客園
相關推薦

2010-01-14 14:12:14

Visual Stud

2009-12-02 09:43:38

Visual Stud

2010-04-01 15:10:06

Visual Stud

2009-11-24 09:00:02

Visual Stud

2010-03-11 14:37:47

Visual StudScrum

2010-07-27 09:40:01

敏捷Visual Stud

2010-07-29 09:41:23

Visual Stud

2010-02-23 09:02:00

Visual Stud

2010-12-16 10:00:20

QtVisual Stud

2010-03-19 16:43:37

Visual Stud

2009-12-15 09:36:32

Visual Stud

2010-04-01 14:51:52

Visual Stud

2009-11-10 13:43:37

Visual Stud

2010-01-12 09:11:18

Visual StudVisual Stud

2009-11-18 09:14:49

Visual Stud

2010-07-12 16:40:39

Visual Stud

2010-01-22 09:51:31

Visual Stud

2010-03-22 09:08:24

Visual Stud

2009-11-19 09:59:47

Visual Stud

2010-11-16 09:23:13

敏捷開發Visual Stud
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 羞羞羞视频 | 日韩免费网 | 羞羞视频网站免费看 | 99久久国产综合精品麻豆 | 国产精品美女www爽爽爽 | 龙珠z在线观看 | 国产激情片在线观看 | 欧美一a | 亚洲一区二区久久久 | 在线电影日韩 | 日韩欧美中文字幕在线观看 | 国产精品久久久久久久久免费丝袜 | 草草草影院 | 成人一区二区三区 | 欧美国产精品一区二区 | 男女视频在线观看网站 | 亚洲成人中文字幕 | 久久久激情视频 | 国产一区二区毛片 | 成人精品系列 | 日本不卡在线视频 | 久久伊人久久 | 亚洲精品一区二区三区四区高清 | 精品国产一区二区国模嫣然 | 日韩一区二区在线播放 | 国产精品久久久久久久午夜 | 欧美精品一区二区三区四区五区 | 日本亚洲一区 | 黄色片免费看视频 | 欧美高清成人 | 亚洲精品免费在线 | 91精品国产91久久久久久吃药 | 国产精品一区二区在线播放 | 欧美日韩专区 | 91麻豆产精品久久久久久 | 爱爱视频日本 | 欧美在线观看一区 | 久久丝袜| 啪一啪 | 亚洲一区二区在线视频 | 91传媒在线观看 |