gpt4 book ai didi

c# - Visual Studio 包 - 在代码中包含和使用外部资源(图像)

转载 作者:太空宇宙 更新时间:2023-11-03 15:23:17 26 4
gpt4 key购买 nike

我目前正在使用 C# 开发一个 visual studio 包项目,它提供了一个新的工具窗口来存储和复制自定义代码片段。

至此,我设计的小窗口如下:

Overview about the graphical design

窗口的 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 绑定(bind)中实现了图像属性。

现在的问题是我不知道如何将这些图像添加到项目中并通过代码访问它们。正如我已经研究过的,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's KnownMonikers

关于c# - Visual Studio 包 - 在代码中包含和使用外部资源(图像),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36397078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com