前端之家收集整理的这篇文章主要介绍了
VS2013Xml文件节点导航插件开发,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_
502_0@
一、功能描述
该插件的功能跟代码文件的导航功能类似,只是下拉框里的内容是元素的某一属性值,如图-1所示
图-1
当点击下拉框的选项后,会自动定位到该内容在xml文件的位置。此功能适用于xml文件内容较多的情况。
二、选择Editor Margin插件模板
因为该插件模板会在编辑区的底部创建一个WPF控件,如图-2所示。
图-2
而你可以创建一个WPF用户控件,并将用户控件添加到该控件里,还可以改变该控件在编辑区的位置。按照Editor Margin模板的向导建立插件项目,在项目里有三个文件:source.extension.vsixmanifest、EditorMargin1、EditorMargin1Factory,改变位置是通过EditorMargin1Factory类的MarginContainerAttribute特性实现的,该特性接收PredefinedMarginNames静态类的常量字段,这些常量字段定义了控件可以停靠的位置,如图-3所示。具体的功能主要是在EditorMargin1文件里实现。
图-3
当文档打开的时候VS会加载MarginFactory类的CreateMargin方法执行。
三、创建WPF用户控件
在项目里添加一个WPF用户控件,在用户控件里添加一个ComboBox下拉控件,当下拉框的选项改变的时候触发定位操作。由于我们是在用户控件里添加下拉控件,在用户控件外部无法监控到下拉框的改变事件,所以我们需要在用户控件里添加一个事件,在下拉框改变事件里触发该事件,这样就可以间接订阅下拉框的选项改变事件。此外,还需要对外开放一个改变下拉框宽度的函数,用于编辑区大小改变的时候可以修改下拉框的宽度。具体的代码如下所示:
/// <summary>
/// MappingInfo.xaml 的交互逻辑
</summary>
public partial class MappingInfo : UserControl
{
public delegate void DelegateSelectionChanged(object sender,SelectionChangedEventArgs e);
event DelegateSelectionChanged SelectionChanged;
public MappingInfo()
{
InitializeComponent();
}
public MappingInfo(IEnumerable<XElement> elements)
{
InitializeComponent();
List<Elements> list = new List<Elements>();
foreach (var item in elements)
{
if (item.Attribute("name") == null)
continue;
Elements model = new Elements();
model.Value = item.Attribute(").Value;
string desc = item.Attribute(title") != null ? item.Attribute(").Value : item.Attribute(remarknull ? "" : item.Attribute( string cache = item.Attribute(cache").Value : "";
model.Text = desc != "" ? string.Format({0}({1})",model.Value,desc) : model.Value;
if (cache != "" && cache.Equals(true √";
}
list.Add(model);
}
cbElement.DisplayMemberPath = Text";
cbElement.SelectedValuePath = Value";
cbElement.ItemsSource = list;
cbElement.SelectedIndex = 0;
//订阅选项改变时的事件
cbElement.SelectionChanged += cbElement_SelectionChanged;
}
void cbElement_SelectionChanged(void SetComboBoxWidth(double width)
{
this.cbElement.Width = width;
}
}
class Elements
{
string Text { get; set; }
string Value { set; }
}
在EditorMargin1类的构造函数里将自定义的wpf用户控件添加到插件创建的控件里
//设置导航栏的相关信息
this.Height = 25;
this.ClipToBounds = false;
this.Background = new SolidColorBrush(Colors.WhiteSmoke);
this.Children.Add(mapInfo);
导航栏大小改变时改变下拉框的宽度
this.SizeChanged += Navigate_SizeChanged;
EnvDTE.DTE dte=ServiceProvider.GlobalProvider.GetService(typeof(DTE)) as DTE;
double width = dte.ActiveDocument.ActiveWindow.Width;
下拉框改变事件
void cb_SelectionChanged(
try
{
获取下拉框选中项
Elements model =
(Elements)((ComboBox)sender).SelectedItem;
获取DTE实例
DTE dte = ServiceProvider.GlobalProvider.GetService(
找出选中项在xml文件里的行数
string[] lines =
File.ReadAllLines(dte.ActiveDocument.FullName);
int line =
in lines)
{
line++
;
if (item !=
"" &&
item.Contains(model.Value))
{
break;
}
}
滚动条滚动到指定行数并显示光标
TextSelection selection = dte.ActiveDocument.Selection
as TextSelection;
if (selection !=
null)
{
selection.MoveToLineAndOffset(line,3);
selection.ActivePoint.TryToShow();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,0); line-height:1.5!important">提示BoxButton.OK,MessageBoxImage.Error);
}
}
dte.ActiveDocument.ProjectItem.FileCodeModel
@H_438_
403@
1 <UserControl.Resources>
2 <ControlTemplate x:Key=ComboBoxToggleButton" TargetType={x:Type ToggleButton}">
3 <Grid>
4 <Grid.ColumnDefinitions>
5 <ColumnDefinition />
6 <ColumnDefinition Width=15" />
7 </Grid.ColumnDefinitions>
8 <Border
9 x:Name=Border"
10 Grid.ColumnSpan=2"
11 CornerRadius=0 12 Background=#FCFCFC 13 BorderBrush=#9BA7B7 14 BorderThickness=1 1 1 1 15 <Border
16 Grid.Column= 17 CornerRadius= 18 Margin=1 19 Background= 20 BorderBrush= 21 BorderThickness= 22 <Path
23 x:Name=Arrow 24 Grid.Column="
25 Fill=Black 26 HorizontalAlignment=Center 27 VerticalAlignment= 28 Data=M 0 0 L 4 4 L 8 0 Z"/>
29 </Grid>
30 <ControlTemplate.Triggers>
31 <Trigger Property=ToggleButton.IsMouSEOver" Value= 32 <Setter TargetName=" Property=Background#FDF4BF 33 <Setter TargetName=BorderBrush#FFEC8B 34 </Trigger>
35 <Trigger Property=ToggleButton.IsChecked 36 <Setter TargetName= 37 </Trigger>
38 <Trigger Property=IsEnabledFalse 39 <Setter TargetName=#EEEEEE 40 <Setter TargetName=#AAAAAA 41 <Setter Property=Foreground#888888 42 <Setter TargetName=Fill 43 </Trigger>
44 </ControlTemplate.Triggers>
45 </ControlTemplate>
46
47 <ControlTemplate x:Key=ComboBoxTextBox{x:Type TextBox} 48 <Border x:Name=PART_ContentHost" Focusable=" Background={TemplateBinding Background} 49 </ControlTemplate>
50
51 <Style x:Key={x:Type ComboBox} 52 <Setter Property=SnapsToDevicePixels 53 <Setter Property=OverridesDefaultStyle 54 <Setter Property=ScrollViewer.HorizontalScrollBarVisibilityAuto 55 <Setter Property=ScrollViewer.VerticalScrollBarVisibility 56 <Setter Property=ScrollViewer.CanContentScroll 57 <Setter Property=Template 58 <Setter.Value>
59 <ControlTemplate TargetType= 60 <Grid>
61 <ToggleButton
62 Name=ToggleButton 63 Template={StaticResource ComboBoxToggleButton} 64 Grid.Column= 65 Focusable=false 66 IsChecked={Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}} 67 ClickMode=Press 68 </ToggleButton>
69 <ContentPresenter
70 Name=ContentSite 71 IsHitTestVisible= 72 Content={TemplateBinding SelectionBoxItem} 73 ContentTemplate={TemplateBinding SelectionBoxItemTemplate} 74 ContentTemplateSelector={TemplateBinding ItemTemplateSelector} 75 Margin=5,3,23,3 76 VerticalAlignment= 77 HorizontalAlignment=Left 78 <TextBox x:Name=PART_EditableTextBox 79 Style={x:Null} 80 Template={StaticResource ComboBoxTextBox} 81 HorizontalAlignment= 82 VerticalAlignment= 83 Margin= 84 Focusable=True 85 Background=Transparent 86 Visibility=Hidden 87 IsReadOnly={TemplateBinding IsReadOnly} 88 <Popup
89 Name=Popup 90 Placement=Bottom 91 IsOpen={TemplateBinding IsDropDownOpen} 92 AllowsTransparency= 93 Focusable= 94 PopupAnimation=Slide 95 <Grid
96 Name=DropDown"
97 SnapsToDevicePixels="
98 MinWidth={TemplateBinding ActualWidth} 99 MaxHeight={TemplateBinding MaxDropDownHeight}" >
100 <Border
101 x:Name=DropDownBorder102 Background=#EFEFEF103 BorderThickness=104 CornerRadius=105 BorderBrush=Gray106 <ScrollViewer Margin=" SnapsToDevicePixels=107 <StackPanel IsItemsHost=" KeyboardNavigation.DirectionalNavigation=Contained108 </ScrollViewer>
109 </Grid>
110 </Popup>
111 </Grid>
112 <ControlTemplate.Triggers>
113 <Trigger Property=HasItems114 <Setter TargetName=MinHeight95115 </Trigger>
116 <Trigger Property=117 <Setter Property=118 </Trigger>
119 <Trigger Property=IsGrouping120 <Setter Property=121 </Trigger>
122 <Trigger Property=IsEditable123 Value=124 <Setter Property=IsTabStop125 <Setter TargetName=VisibilityVisible126 <Setter TargetName=127 </Trigger>
128 </ControlTemplate.Triggers>
129 </ControlTemplate>
130 </Setter.Value>
131 </Setter>
132 <Style.Triggers>
133 </Style.Triggers>
134 </Style>
135
136 <Style x:Key={x:Type ComboBoxItem}137 <Setter Property=138 <Setter Property=139 <Setter Property=140 <Setter.Value>
141 <ControlTemplate TargetType=142 <Border
143 Name=144 Padding=145 BorderThickness=146 SnapsToDevicePixels=147 <ContentPresenter />
148 </Border>
149 <ControlTemplate.Triggers>
150 <!--<Trigger Property=IsHighlighted151 <Setter TargetName=152 </Trigger>-->
153 <Trigger Property=IsMouSEOver154 <Setter TargetName=155 <Setter TargetName=#E5C365156 </Trigger>
157 <Trigger Property=158 <Setter Property=159 </Trigger>
160 </ControlTemplate.Triggers>
161 </ControlTemplate>
162 </Setter.Value>
163 </Setter>
164 </Style>
165 </UserControl.Resources>