日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
詳解VisualStudio2010Extension的應(yīng)用

對(duì)于即將發(fā)布正式版的Visual Studio 2010,有一個(gè)Visual Studio 2010 Extension功能。平時(shí)大家關(guān)注Visual Studio 2010 Extension不多,希望通過(guò)本文能讓大家更好的了解。

創(chuàng)新互聯(lián)建站專(zhuān)業(yè)為企業(yè)提供新豐網(wǎng)站建設(shè)、新豐做網(wǎng)站、新豐網(wǎng)站設(shè)計(jì)、新豐網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、新豐企業(yè)網(wǎng)站模板建站服務(wù),10余年新豐做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

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

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

建好工程以后,會(huì)自動(dòng)生成TextViewCreationListener,這里實(shí)現(xiàn)了IWpfTextViewCreationListener接口,并通過(guò)MEF導(dǎo)出IWpfTextViewCreationListener對(duì)象:

 
 
 
 
  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就會(huì)在合適的時(shí)候調(diào)用IWpfTextViewCreationListener.TextViewCreated方法來(lái)通知文字編輯的狀態(tài)改變。

為了實(shí)現(xiàn)浮動(dòng)一個(gè)自己的工具欄,這里還需要導(dǎo)出一個(gè)AdornmentLayerDefinition,并通過(guò)Order Attribute來(lái)定制這個(gè)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"); 

扯得遠(yuǎn)了,回到IWpfTextViewCreationListener.TextViewCreated,通過(guò)這里,可以得到一個(gè)IWpfTextView,

這是所有操作的目標(biāo)和展現(xiàn),另外,還需要掛他的Closed、LayoutChanged、MouseHovered、SelectionChanged等事件,以響應(yīng)用戶(hù)行為。

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

接下來(lái)要實(shí)現(xiàn)工具欄的界面,這個(gè)就不多說(shuō)了,建一個(gè)UserControl,里面放上ToolBar就搞定了。那么何時(shí)何地顯示這個(gè)ToolBar呢?這就要依賴(lài)IWpfTextView的SelectionChanged事件了,上面提到會(huì)掛這個(gè)事件就是為這里用的。

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(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事件處理要記得取消所有掛這個(gè)事件等等收尾工作。

接下來(lái)編譯工程,打包VSIX就完成了,目前實(shí)現(xiàn)的主要Feature:
1、當(dāng)在代碼編輯器中選擇一段文字,并將鼠標(biāo)移到文字區(qū)域時(shí),QuickToolbar會(huì)以半透明的方式“浮”文字的旁邊。
2、當(dāng)鼠標(biāo)移到QuickToolbar區(qū)域,QuickToolbar會(huì)變成不透明,其上的按鈕會(huì)響應(yīng)鼠標(biāo)動(dòng)作。

3、目前支持的操作有:

  • 剪切(Cut)
  • 復(fù)制(Copy)
  • 粘貼(Paste)
  • 刪除(Delete)
  • 減小縮進(jìn)(Decrease Indent)
  • 增加縮進(jìn)(Increase Indent)
  • 注釋代碼(Comment)
  • 取消注釋?zhuān)║ncomment)
  • 等等

上面主要展示了如何使用MEF擴(kuò)展VS2010,來(lái)擴(kuò)展編輯控制和展現(xiàn)自己的UI;在實(shí)現(xiàn)QuickToolbar的時(shí)候,發(fā)現(xiàn)MEF僅僅提供了很基本的編輯控制,如果需要高級(jí)的操作,比如注釋選擇的代碼,就捉襟見(jiàn)肘,很是麻煩。

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

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

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

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

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

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

也就是說(shuō)這是一個(gè)由IDE提供的全局的Service,可以創(chuàng)建、訪問(wèn)工具窗口和編輯窗口。瀏覽一下這個(gè)所有Member,發(fā)現(xiàn)了一個(gè)叫IVsUIShell.PostExecCommand(...)(http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.shell.interop.ivsuishell.postexeccommand%28VS.100%29.aspx)的方法,MSDN描述說(shuō)通過(guò)它可以異步執(zhí)行Command,那么,只要找到注釋代碼的Command,在通過(guò)這個(gè)接口就可以實(shí)現(xiàn)VS IDE一樣的注釋代碼的Feature了??釘懒?,就是它,當(dāng)怎么得到它呢?現(xiàn)在請(qǐng)留心MSDN上的解釋?zhuān)褪巧厦嫖沂褂眉t色粗體表示出來(lái)的部分——這個(gè)由IDE提供的全局的Service,那么可以通過(guò)Package.GetGlobalService(...)(http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.shell.package.getglobalservice%28VS.100%29.aspx)來(lái)獲?。?/p>

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

接下來(lái)是找到自己需要Command,然后PostExecCommand就搞定了;而VS提供的Command有兩部分組成:Guid和CommandID,這個(gè)大部分都在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.  } 

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


分享文章:詳解VisualStudio2010Extension的應(yīng)用
網(wǎng)站URL:http://m.5511xx.com/article/dpdihic.html