温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用模板Editor ViewPort Adornment实现扩展

发布时间:2022-03-31 14:19:41 来源:亿速云 阅读:154 作者:iii 栏目:编程语言

今天小编给大家分享一下如何使用模板Editor ViewPort Adornment实现扩展的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

第一步:创建一个Viewport Adornment项目

我们从Extensibility中选择Viewport Adornment模板创建一个项目。这将生成一个SourceManifest文件和两个类文件。一个是Adornment类本身,另外一个是AdornmentFactory类。

第二步:添加一个WPF用户控件

右键单击项目,选择添加一个新的WPF用户控件。为了简单起见,我使用了一个用户控件。这个用户控件实际上包含一个Expander控件,设置它的ExpandDirection = Left,它里面又包含了一些TextBlock控件和另外一个Expander ,设置里面的这个Expander的ExpandDirection = Down。看下面的代码(我删除不必要的元素,使其更简单):

<Expander ExpandDirection="Left" Style="{DynamicResource ExpanderStyle1}"   x:Name="expMain" > <StackPanel> <TextBlock x:Name="txtNoLines"   Text="No of Lines : {0}"   Margin="25 25 25 0"   FontSize="12"   FontFamily="Verdana"   FontWeight="Bold"   Foreground="Yellow"></TextBlock> <TextBlock x:Name="txtNoCharacters"   Text="No of Characters : {0}"   Margin="25 5 25 15"   FontSize="12"   FontFamily="Verdana"   FontWeight="Bold"   Foreground="Yellow"></TextBlock> <Expander x:Name="expCodeInfo" ExpandDirection="Down"   Header="Code Information"> <StackPanel> <TextBlock x:Name="txtClassInfo"   Margin="25 25 25 0"   FontSize="12"   FontFamily="Verdana"   FontWeight="Bold"   Foreground="LightYellow"/> <Line Margin="0,4" SnapsToDevicePixels="True" Stroke="Gold" Stretch="Fill" X1="0" X2="1"   /> <TextBlock x:Name="txtFileSize" Margin="25 5 25 15"   FontSize="12"   FontFamily="Verdana"   FontWeight="Bold"   Foreground="AliceBlue"/> </StackPanel> </Expander> </StackPanel> </Expander>

你可以看到,代码很简单,两个Expanders,一个用来显示基本的统计信息和另外一个显示扩展的统计信息。我还使用StackPanel来固定TextBlocks布局。现在,如果你看一下后台代码,发现它也一样简单。其实我已经创建了一个CodeInfoTracker类,用它来为我们分析源代码文件。我只是为我们的用户控件添加了一个构造函数,使用户控件更具扩展性而已。

private CodeInfoTracker _cinfo;  private CodeInfoTracker.Calculators _calculator;  public ucInfoBox(CodeInfoTracker cinfo)  : this()   {  this._cinfo = cinfo;  }  public void UpdateInfo(CodeInfoTracker info)  {  _calculator = info.PerFormCalculate();  this.txtNoLines.Text = string.Format("No of Lines : {0}",   _calculator.no_of_lines);  this.txtNoCharacters.Text = string.Format("No of Characters : {0}",   _calculator.no_of_characters);  this.txtFileSize.Text = string.Format("Total File Size : {0}",   _calculator.totalfilesize);   StringBuilder builder = new StringBuilder();  if (this._calculator.interfaces != 0)  builder.AppendFormat("Interfaces : {0}\n\r",   this._calculator.interfaces);  if (this._calculator.namespaces != 0)  builder.AppendFormat("NameSpaces : {0}\n\r",   this._calculator.namespaces);  if (this._calculator.classes != 0)  builder.AppendFormat("Classes : {0}\n\r",   this._calculator.classes);  if (this._calculator.methods != 0)  builder.AppendFormat("Methods : {0}\n\r", this._calculator.methods);  if (this._calculator.properties != 0)  builder.AppendFormat("Properties : {0}\n\r",   this._calculator.properties);  if (this._calculator.fields != 0)  builder.AppendFormat("Fields : {0}\n\r", this._calculator.fields);  if (this._calculator.comments != 0)  builder.AppendFormat("Comments : {0}\n\r", this._calculator.comments);  if (builder.Length > 0)  {  this.txtClassInfo.Visibility = System.Windows.Visibility.Visible;  this.txtClassInfo.Text = builder.ToString();  }  else  {  this.txtClassInfo.Text = "";  this.txtClassInfo.Visibility = System.Windows.Visibility.Hidden;  }  }

使用了一个结构体Calculators ,这个结构体放置在我们的自定义类中,它有几个int属性用来保存分析源文件获取的所有信息。 info.PerFormCalculate(); 给出分析的结果。这里使用的所有获取的信息来更新了UIElements。

第三步:创建获取源文件信息的类

虽然代码存在一些复杂性,但是这个类其实很简单。我很感谢CS Parser [^],它帮助我自动地解析源代码。这个类需要一个IWpfTextView对象,它代表着Visual Studio 2010文本编辑器。实际上WpfTextView实现了IWpfTextView。在执行期间这个类接受这个对象。

我可以从WPFTextView.TextSnapshot.GetText()获得到了源代码。在我调用的这个分析的时候,我只需要检测的代码是什么语言写的。开始我想自己来实现,但是感谢上帝,我在WPFTextView中发现已经存在这个对象了。

public enum Language  {  CSharp, VisualBasic, Indeterminate  }  internal Language DetectLanguage  {  get  {  string langtype =   this._view.FormattedLineSource.TextAndAdornmentSequencer.  SourceBuffer.ContentType.DisplayName;  if(langtype.Equals("CSHARP",   StringComparison.InvariantCultureIgnoreCase))  return Language.CSharp;  else if(langtype.Equals("BASIC",   StringComparison.InvariantCultureIgnoreCase))  return Language.VisualBasic;  else  return Language.Indeterminate;  }  }

DetectLanguage妥善地利用WPFTextView对象的FormattedLineSource.TextAndAdornmentSequencer。SourceBuffer.ContentType.DisplayName,这个属性告诉我是使用了哪种语言。之后我创建了一个新的方法PerFormCalculate,用它来解析源代码,它返回一个Calculation结构对象。

第四步:创建 Adornment Factory 类

回到这个扩展,我创建一个Adornment(InfoBoxAdornmentFactory)的Factory类。这个类继承IWpfTextViewCreationListener,用来监听WPF的编辑和创建事件。

[Export(typeof(IWpfTextViewCreationListener))]  [ContentType("text")]  [TextViewRole(PredefinedTextViewRoles.Document)]   internal sealed class InfoBoxAdornmentFactory : IWpfTextViewCreationListener  {  [Export(typeof(AdornmentLayerDefinition))]  [Name("AlwaysVisibleInfoBox")]  [Order(After = PredefinedAdornmentLayers.Selection)]  [TextViewRole(PredefinedTextViewRoles.Interactive)]  public AdornmentLayerDefinition editorAdornmentLayer = null;  public void TextViewCreated(IWpfTextView textView)  {  new AlwaysVisibleInfoBox(textView);  }  }

这里,你可以看到我在这个类上使用了很多Attributes,像ContentType,它定义了我们只处理文本格式的编辑器;还有TextViewRole,它定义了将被这个类处理的textview的类型。在这个类中,我创建了一个AdornmentLayerDefination对象。可能你想知道我们没有使用它,无什么还需要定义它呢,它只是用来配置属性的。Order属性指定,当,InfoBox在层被选之后监听,Name是编辑扩展的名字。

第五步:创建Adornment 类

Adornment类实际创建了一个WPF用户控件对象,并设置它的视图画布。在内部构造函数中,我处理IWpfTextView.LayoutChanged事件,当代码修改或者布局改变的时候,就触发这个事件。

因此,通过这一事件,当我们编辑的文档时,我们可以很容易地得到回调。当浏览器编辑器的大小改变时,我还通过处理WPFTextView.ViewportHeightChanged,WPFTextView.ViewportWidthChanged得到回调,使我们可以重新定位相应的UserControl。

public AlwaysVisibleInfoBox(IWpfTextView view)  {  _view.LayoutChanged += this.OnLayoutChanged;  this.GetLayer();   }  private void GetLayer()  {   _adornmentLayer = this._view.GetAdornmentLayer("AlwaysVisibleInfoBox");  _view.ViewportHeightChanged += delegate { this.onSizeChange(); };  _view.ViewportWidthChanged += delegate { this.onSizeChange(); };  }  private void OnLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)  {  this._info = new CodeInfoTracker(_view);  this.infobox.UpdateInfo(this._info);  }  public void onSizeChange()  {   _adornmentLayer.RemoveAllAdornments();  Canvas.SetLeft(infobox, _view.ViewportRight - 255);   Canvas.SetTop(infobox, _view.ViewportTop + 10);   _adornmentLayer.AddAdornment(AdornmentPositioningBehavior.ViewportRelative,    null, null,   infobox, null);   }

因此,构造函数只是调用GetLayer来获取的Layer对象,发生在ViewportHeightChanged和ViewportWidthChanged ViewPortSizeChage事件。当一个布局改变时,我就能更新这个用户的控件。至此,我们成功地建立我们的扩展。你可以使用F5运行它,它会打开一个Visual Studio 2010的Experimental实例。

安装和卸载这个扩展:

安装和卸载这个扩展是非常容易的。当您编译项目后,它会产生一个VSIX文件。您可以只需双击这个文件,它会自动安装到Visual Studio 2010。

如何使用模板Editor ViewPort Adornment实现扩展

以上就是“如何使用模板Editor ViewPort Adornment实现扩展”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI