C# 向我解释以下VS2010扩展示例代码

C# 向我解释以下VS2010扩展示例代码,c#,visual-studio-2010,C#,Visual Studio 2010,程序员们,我正在构建一个VS2010扩展,我正在围绕VS2010 SDK附带的一些示例进行实验 其中一个示例项目称为TextAdorment。在该项目中,有一个古怪的类,如下所示: [Export(typeof(IWpfTextViewCreationListener))] [ContentType("text")] [TextViewRole(PredefinedTextViewRoles.Document)] internal sealed class TextAdornment1Facto

程序员们,我正在构建一个VS2010扩展,我正在围绕VS2010 SDK附带的一些示例进行实验

其中一个示例项目称为TextAdorment。在该项目中,有一个古怪的类,如下所示:

[Export(typeof(IWpfTextViewCreationListener))]
[ContentType("text")]
[TextViewRole(PredefinedTextViewRoles.Document)]
internal sealed class TextAdornment1Factory : IWpfTextViewCreationListener
当我在试验这个项目时,我试着调试这个项目,以查看程序的流程,我注意到当我第一次开始调试时,这个类被击中了

现在我的问题是:是什么让这个类成为VS启动时第一个被调用的类?换句话说,为什么这个类是活动的,并且它作为实例化这个类类型的对象的代码运行

以下是示例项目中仅有的两个文件:

textAdorment1Factory.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
}
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);
                    }
                }
            }
        }
    }
}
使用System.ComponentModel.Composition;
使用Microsoft.VisualStudio.Text.Editor;
使用Microsoft.VisualStudio.Utilities;
命名空间TextAdorment1
{
#区域装饰厂
/// 
///建立一个用于放置装饰并导出
///在a的创建事件上实例化装饰的
/// 
[导出(类型(IWPTextViewCreationListener))]
[内容类型(“文本”)]
[TextViewRole(预定义的textViewRoles.Document)]
内部密封类TextAdorment1Factory:IWPTextViewCreationListener
{
/// 
///定义装饰的装饰层。此层是有序的
///在Z顺序的选择层之后
/// 
[导出(类型(装饰层定义))]
[名称(“文本装饰1”)]
[顺序(在=预定义的AdormentLayers.Selection之后,在=预定义的AdormentLayers.Text之前)]
[TextViewRole(预定义的textViewRoles.Document)]
公共装饰层定义编辑器ADORNMENTLAYER=null;
/// 
///创建textView时实例化TextAdorment1管理器。
/// 
///装饰物应该放在上面的地方
已创建公共无效文本视图(IWPTextView文本视图)
{
新的文本装饰1(文本视图);
}
}
#endregion/装饰厂
}
文本装饰1.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
}
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);
                    }
                }
            }
        }
    }
}
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Media;
使用Microsoft.VisualStudio.Text;
使用Microsoft.VisualStudio.Text.Editor;
使用Microsoft.VisualStudio.Text.Formatting;
命名空间TextAdorment1
{
///
///TextAdorment1将红色框放在编辑器窗口中所有“A”的后面
///
公共类TextAdorment1
{
IAdornmentLayer\u层;
IWpfTextView\u视图;
刷子;
钢笔;
ITextView文本视图;
公共文本装饰1(IWPTextView视图)
{
_视图=视图;
_layer=view.getAdormentLayer(“TextAdorment1”);
textView=视图;
//收听任何改变布局的事件(文本更改、滚动等)
_view.LayoutChanged+=仅布局更改;
_view.Closed+=新的System.EventHandler(\u view\u Closed);
//selectedText();
//创建钢笔和画笔,为a后面的方框上色
笔刷=新的SolidColorBrush(Color.FromArgb(0x20,0x00,0x00,0xff));
刷子。冻结();
画笔画笔=新的SolidColorBrush(Colors.Red);
冻结;
钢笔=新钢笔(钢笔刷,0.5);
笔。冻结();
_刷子=刷子;
_笔=笔;
}
void\u view\u Closed(对象发送方,System.EventArgs e)
{
Show(textView.Selection.IsEmpty.ToString());
}
/// 
///布局更改时,将装饰添加到任何重新格式化的线
/// 
private void OnLayoutChanged(对象发送者,TextViewLayoutChangedEventArgs e)
{
foreach(e.new中的ITextViewLine或ReformatedLines)
{
这个.CreateVisuals(行);
}
}
私有void selectedText()
{
}
/// 
///在给定的行中,在a后面添加红色框
/// 
专用void CreateVisuals(ITextViewLine)
{
//获取对当前文本视图中的行的引用
IWPTextViewLineCollection textViewLines=_view.textViewLines;
int start=line.start;
int end=line.end;
//循环遍历每个字符,并在任意字符周围放置一个框
for(int i=开始;(i<结束);+i)
{
如果(_view.TextSnapshot[i]=='a')
{
快照span=新快照span(_view.TextSnapshot,span.FromBounds(i,i+1));
几何g=文本视图线。GetMarkerGeometry(span);
如果(g!=null)
{
几何绘图=新几何绘图(_画笔,_画笔,g);
绘图。冻结();
DrawingImage DrawingImage=新的DrawingImage(图纸);
drawingImage.Freeze();
图像=新图像();
image.Source=drawingImage;
//将图像与文本几何体边界的顶部对齐
SetLeft(图像,g.Bounds.Left);
Canvas.SetTop(图像,g.Bounds.Top);
_layer.AddAdorment(AdormentPositionBehavior.TextRelative,span,null,image,null);
}
}
}
}
}
}

如果我理解正确,您想知道
TextViewCreated(IWPTextView textview)
中textview的值来自哪里。由于这是一个示例项目VS插件,它在编辑器窗口中的所有a下绘制红色框,我怀疑textview是一个指向编辑器win的变量