C# VisualStudio包-通过代码包含和使用外部资源(图像)
我目前正在使用C#开发一个visual studio包项目,该项目提供了一个新的工具窗口来存储和复制自定义代码段 到目前为止,我设计的小窗口如下: 该窗口的XAML代码为:C# VisualStudio包-通过代码包含和使用外部资源(图像),c#,.net,visual-studio-2013,vsix,visual-studio-package,C#,.net,Visual Studio 2013,Vsix,Visual Studio Package,我目前正在使用C#开发一个visual studio包项目,该项目提供了一个新的工具窗口来存储和复制自定义代码段 到目前为止,我设计的小窗口如下: 该窗口的XAML代码为: <TreeView Name="tevTemplates" Background="#00000000"> <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Items}"&g
<TreeView Name="tevTemplates" Background="#00000000">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Items}">
<StackPanel Orientation="Horizontal">
<Image Margin="0,0,5,0" Source="{Binding Image}" />
<TextBlock Text="{Binding Title}" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
以及我使用的自定义数据模型:
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
namespace Sago.TemQWindow.Classes
{
/// <summary>
/// Represents a simple data item to display easy an item inside a view
/// </summary>
public class MenuNode
{
/// <summary>
/// Initializes a new instance of a Sago.TemQWindow.Classes.MenuNode object with the display title
/// </summary>
/// <param name="title"></param>
public MenuNode(string title) : this(title, null) { }
/// <summary>
/// Initializes a new instance of a Sago.TemQWindow.Classes.MenuNode object with the display title and image
/// </summary>
/// <param name="title"></param>
/// <param name="image"></param>
public MenuNode(string title, Image image)
{
this.Items = new ObservableCollection<MenuNode>();
Title = title;
Image = image;
}
/// <summary>
/// Gets or sets the display title
/// </summary>
public string Title { get; set; }
/// <summary>
/// Gets or sets the display image
/// </summary>
public Image Image { get; set; }
/// <summary>
/// Gets or sets the sub items of the node
/// </summary>
public ObservableCollection<MenuNode> Items { get; set; }
}
}
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用系统图;
名称空间Sago.TemQWindow.Classes
{
///
///表示一个简单的数据项,以便在视图中轻松显示该项
///
公共类菜单节点
{
///
///使用显示标题初始化Sago.TemQWindow.Classes.MenuNode对象的新实例
///
///
公共菜单节点(字符串标题):此(标题,null){}
///
///使用显示标题和图像初始化Sago.TemQWindow.Classes.MenuNode对象的新实例
///
///
///
公共菜单项(字符串标题、图像)
{
this.Items=新的ObservableCollection();
头衔=头衔;
图像=图像;
}
///
///获取或设置显示标题
///
公共字符串标题{get;set;}
///
///获取或设置显示图像
///
公共映像映像{get;set;}
///
///获取或设置节点的子项
///
公共ObservableCollection项{get;set;}
}
}
打开工具窗口后,以下源代码将加载模板文件夹的内容和子目录内容:
/// <summary>
/// Loads the whole folder structure of the given path recursive
/// </summary>
private void LoadStructure(MenuNode rootNode, string path)
{
// Gets all files
string[] files = IO.Directory.GetFiles(path);
foreach (string file in files)
{
// Creates and adds the sub node for all files inside the given folder
string clearName = IO.Path.GetFileNameWithoutExtension(file);
MenuNode node = new MenuNode(clearName);
rootNode.Items.Add(node);
}
// Gets all sub directories
string[] directories = IO.Directory.GetDirectories(path);
foreach (string directory in directories)
{
// Creates and adds the sub directory as a sub node
string clearName = IO.Path.GetFileNameWithoutExtension(directory);
MenuNode node = new MenuNode(clearName);
rootNode.Items.Add(node);
// Calls the method recursive
LoadStructure(node, directory);
}
}
//
///加载给定路径的整个文件夹结构
///
私有void加载结构(菜单节点根节点,字符串路径)
{
//获取所有文件
string[]files=IO.Directory.GetFiles(路径);
foreach(文件中的字符串文件)
{
//为给定文件夹中的所有文件创建并添加子节点
string clearName=IO.Path.GetFileNameWithoutExtension(文件);
MenuNode节点=新的MenuNode(clearName);
rootNode.Items.Add(节点);
}
//获取所有子目录
string[]directories=IO.Directory.GetDirectories(路径);
foreach(目录中的字符串目录)
{
//创建子目录并将其添加为子节点
string clearName=IO.Path.GetFileNameWithoutExtension(目录);
MenuNode节点=新的MenuNode(clearName);
rootNode.Items.Add(节点);
//调用递归方法
LoadStructure(节点、目录);
}
}
在下一步中,我想用特定的图像可视化treeview控件中的文件夹和文件。如您所见,我已经在数据模型和XAML绑定中实现了图像属性
现在的问题是,我不知道如何将这些图像添加到项目中,并通过代码访问它们。因为我已经研究过VisualStudio包项目,所以无法访问Properties.Resources内容
如果有人能帮我解决这个问题,我将不胜感激。一种方法就是这样 步骤1:更改XAML以使用名字对象
<ResourceDictionary ...
xmlns:imaging="clr-namespace:Microsoft.VisualStudio.Imaging;assembly=Microsoft.VisualStudio.Imaging">
<TreeView Name="tevTemplates" Background="#00000000">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Items}">
<StackPanel Orientation="Horizontal">
<imaging:CrispImage Moniker="{Binding Image}" />
<TextBlock Text="{Binding Title}" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using Microsoft.VisualStudio.Imaging.Interop;
namespace Sago.TemQWindow.Classes
{
/// <summary>
/// Represents a simple data item to display easy an item inside a view
/// </summary>
public class MenuNode
{
...
/// <summary>
/// Gets or sets the display image
/// </summary>
public ImageMoniker Image { get; set; }
...
}
}
查看此处了解所有KnownMonikers:
private void LoadStructure(MenuNode rootNode, string path)
{
// Gets all files
string[] files = IO.Directory.GetFiles(path);
foreach (string file in files)
{
// Creates and adds the sub node for all files inside the given folder
string clearName = IO.Path.GetFileNameWithoutExtension(file);
MenuNode node = new MenuNode(clearName);
node.Image = KnownMonikers.TextFile;
rootNode.Items.Add(node);
}
// Gets all sub directories
string[] directories = IO.Directory.GetDirectories(path);
foreach (string directory in directories)
{
// Creates and adds the sub directory as a sub node
string clearName = IO.Path.GetFileNameWithoutExtension(directory);
MenuNode node = new MenuNode(clearName);
node.Image = KnownMonikers.FolderClosed;
rootNode.Items.Add(node);
// Calls the method recursive
LoadStructure(node, directory);
}
}