編碼器,我正在構建一個VS 2010擴展,並且我正在嘗試VS 2010 SDK附帶的一些示例。向我解釋以下VS 2010擴展示例代碼
其中一個示例項目稱爲TextAdornment。在該項目中有一個怪人類,如下所示:
[Export(typeof(IWpfTextViewCreationListener))]
[ContentType("text")]
[TextViewRole(PredefinedTextViewRoles.Document)]
internal sealed class TextAdornment1Factory : IWpfTextViewCreationListener
當我和這個項目的實驗,我試圖調試項目,看看該程序的流量,我注意到,這個類被擊中當我第一次開始調試。
現在我的問題如下:是什麼讓這個類成爲VS啓動時調用的第一個類?換句話說,爲什麼這個類變得活躍,並且它在一些代碼中運行時實例化了這個類類型的對象?
下面是示例項目中僅有的兩個文件:
TextAdornment1Factory.cs
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Utilities;
namespace TextAdornment1
{
#region Adornment Factory
/// <summary>
/// Establishes an <see cref="IAdornmentLayer"/> to place the adornment on and exports the <see cref="IWpfTextViewCreationListener"/>
/// that instantiates the adornment on the event of a <see cref="IWpfTextView"/>'s creation
/// </summary>
[Export(typeof(IWpfTextViewCreationListener))]
[ContentType("text")]
[TextViewRole(PredefinedTextViewRoles.Document)]
internal sealed class TextAdornment1Factory : IWpfTextViewCreationListener
{
/// <summary>
/// Defines the adornment layer for the adornment. This layer is ordered
/// after the selection layer in the Z-order
/// </summary>
[Export(typeof(AdornmentLayerDefinition))]
[Name("TextAdornment1")]
[Order(After = PredefinedAdornmentLayers.Selection, Before = PredefinedAdornmentLayers.Text)]
[TextViewRole(PredefinedTextViewRoles.Document)]
public AdornmentLayerDefinition editorAdornmentLayer = null;
/// <summary>
/// Instantiates a TextAdornment1 manager when a textView is created.
/// </summary>
/// <param name="textView">The <see cref="IWpfTextView"/> upon which the adornment should be placed</param>
public void TextViewCreated(IWpfTextView textView)
{
new TextAdornment1(textView);
}
}
#endregion //Adornment Factory
}
TextAdornment1.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Formatting;
namespace TextAdornment1
{
///<summary>
///TextAdornment1 places red boxes behind all the "A"s in the editor window
///</summary>
public class TextAdornment1
{
IAdornmentLayer _layer;
IWpfTextView _view;
Brush _brush;
Pen _pen;
ITextView textView;
public TextAdornment1(IWpfTextView view)
{
_view = view;
_layer = view.GetAdornmentLayer("TextAdornment1");
textView = view;
//Listen to any event that changes the layout (text changes, scrolling, etc)
_view.LayoutChanged += OnLayoutChanged;
_view.Closed += new System.EventHandler(_view_Closed);
//selectedText();
//Create the pen and brush to color the box behind the a's
Brush brush = new SolidColorBrush(Color.FromArgb(0x20, 0x00, 0x00, 0xff));
brush.Freeze();
Brush penBrush = new SolidColorBrush(Colors.Red);
penBrush.Freeze();
Pen pen = new Pen(penBrush, 0.5);
pen.Freeze();
_brush = brush;
_pen = pen;
}
void _view_Closed(object sender, System.EventArgs e)
{
MessageBox.Show(textView.Selection.IsEmpty.ToString());
}
/// <summary>
/// On layout change add the adornment to any reformatted lines
/// </summary>
private void OnLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
{
foreach (ITextViewLine line in e.NewOrReformattedLines)
{
this.CreateVisuals(line);
}
}
private void selectedText()
{
}
/// <summary>
/// Within the given line add the scarlet box behind the a
/// </summary>
private void CreateVisuals(ITextViewLine line)
{
//grab a reference to the lines in the current TextView
IWpfTextViewLineCollection textViewLines = _view.TextViewLines;
int start = line.Start;
int end = line.End;
//Loop through each character, and place a box around any a
for (int i = start; (i < end); ++i)
{
if (_view.TextSnapshot[i] == 'a')
{
SnapshotSpan span = new SnapshotSpan(_view.TextSnapshot, Span.FromBounds(i, i + 1));
Geometry g = textViewLines.GetMarkerGeometry(span);
if (g != null)
{
GeometryDrawing drawing = new GeometryDrawing(_brush, _pen, g);
drawing.Freeze();
DrawingImage drawingImage = new DrawingImage(drawing);
drawingImage.Freeze();
Image image = new Image();
image.Source = drawingImage;
//Align the image with the top of the bounds of the text geometry
Canvas.SetLeft(image, g.Bounds.Left);
Canvas.SetTop(image, g.Bounds.Top);
_layer.AddAdornment(AdornmentPositioningBehavior.TextRelative, span, null, image, null);
}
}
}
}
}
}
抱歉,傢伙,stackoverflow的代碼渲染似乎不工作得很好! – ealshabaan 2011-02-17 07:25:38