[英]Visual Studio Package - Include and use external resources (Images) over the code
我目前正在使用C#開發一個Visual Studio軟件包項目,該項目提供了一個新的工具窗口來存儲和復制自定義代碼段。
到目前為止,我已將小窗口設計如下:
該窗口的XAML代碼為:
<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; }
}
}
打開工具窗口后,以下源代碼將加載模板文件夾的內容和子目錄內容:
/// <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);
}
}
在下一步中,我想用特定的圖像可視化treeview控件中的文件夾和文件。 如您所見,我已經在數據模型和XAML綁定中實現了圖像屬性。
現在的問題是,我不知道如何將這些圖像添加到項目中並通過代碼訪問它們。 由於我已經研究過Visual Studio打包項目,因此無法訪問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>
步驟2:更改模型以使用綽號
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; }
...
}
}
步驟3:設定綽號
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);
}
}
在此處查看所有KnownMonikers: Visual Studio的KnownMonikers
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.