更新 TreeView 文件资源管理器代码以在 UWP 中使用 Microsoft.UI.Xaml

全力以赴

我正在尝试将我的旧源代码更新为 UWP,引用 Microsoft.UI.Xaml.dll TreeView 控件以显示/复制存储在字符串 [] 中的文件和文件夹。

string[] 存储一个或多个文件的完整路径。

例子:

C:\Users\User\Documents\Test1.txt

C:\Users\User\Documents\Test2.txt

C:\用户\用户\文档\文件夹\Test1.txt

C:\用户\用户\文档\文件夹\Test2.txt

我想更新的代码如下:

private void PopulateTreeView(TreeView treeView, string[] paths, char pathSeparator)
        {
            TreeNode lastNode = null;
            string subPathAgg;
            long count = 0;

            foreach (string path in paths)
            {
                subPathAgg = string.Empty;
                foreach (string subPath in path.Split(pathSeparator))
                {
                    Application.DoEvents();
                    subPathAgg += subPath + pathSeparator;
                    TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
                    if (nodes.Length == 0)
                    {
                        if (lastNode == null)
                        {
                            lastNode = treeView.Nodes.Add(subPathAgg, subPath);
                        }
                        else
                        {
                            lastNode = lastNode.Nodes.Add(subPathAgg, subPath);
                        }
                        count++;
                    }
                    else
                    {
                        lastNode = nodes[0];
                    }
                }
                lastNode = null; // This is the place code was changed
            }
        }

有谁知道如何使用 Microsoft.UI.Xaml.dll TreeView 控件更新此代码?我认为这应该很容易,但我觉得我错过了一些东西。

Richard Zhang - MSFT

您的代码应该是 WPF 或 WinForm。TreeView在UWP有很多比他们的变化。如果你想从头开始处理这个控件,你可以查看这个文档

现在让我们看一下代码。如果您打算TreeView通过路径字符串创建一个,则在 UWP 中会更复杂。

  1. 创建一个类来保存显示文本和真实路径
public class PathDisplay
{
    public string DisplayText { get; set; }
    public string Path { get; set; }
    public PathDisplay(){ }
    public PathDisplay(string d, string p)
    {
        DisplayText = d;
        Path = p;
    }
    public override string ToString()
    {
        return DisplayText;
    }
}
  1. 创建一个递归方法来检测相同的节点subPathAgg(该Find方法在 中不再可用TreeView
private IEnumerable<TreeViewNode> GetSameNodes(IList<TreeViewNode> nodes, string path)
{
    foreach (var node in nodes)
    {
        var content = node.Content as PathDisplay;
        if (content?.Path == path)
        {
            yield return node;
        }
        else
        {
            if (node.Children.Count > 0)
            {
                foreach (var item in GetSameNodes(node.Children,path))
                {
                    yield return item;
                }
            }
        }
    }
}
  1. 重写PopulateTreeView方法。在 UWP 中,许多类名和方法都发生了变化。
private void PopulateTreeView(TreeView treeView, string[] paths, char pathSeparator)
{
    TreeViewNode lastNode = null;
    string subPathAgg;
    long count = 0;

    foreach (string path in paths)
    {
        subPathAgg = string.Empty;
        foreach (string subPath in path.Split(pathSeparator))
        {
            subPathAgg += subPath + pathSeparator;
            var displayModel = new PathDisplay(subPath, subPathAgg);
            TreeViewNode[] nodes = GetSameNodes(treeView.RootNodes,subPathAgg).ToArray();
            if (nodes.Length == 0)
            {
                if (lastNode == null)
                {
                    lastNode = new TreeViewNode() { Content = displayModel };
                    treeView.RootNodes.Add(lastNode);
                }
                else
                {
                    var node = new TreeViewNode() { Content = displayModel };
                    lastNode.Children.Add(node);
                    lastNode = node;
                }
                count++;
            }
            else
            {
                lastNode = nodes[0];
            }
        }
        lastNode = null; // This is the place code was changed
    }
}

这样我们就可以使用它

xml

<controls:TreeView x:Name="TestTreeView"
          />

xml文件

public TreeViewPage()
{
    this.InitializeComponent();
    PopulateTreeView(TestTreeView, new string[]{
        @"C:\Users\User\Documents\Test1.txt",
        @"C:\Users\User\Documents\Test2.txt",
        @"C:\Users\User\Documents\folder\Test1.txt"
    }, '\\');
}

它看起来像这样:

e34da227-e5a6-417c-b976-bb6dc4f3109b.png


代码可以这样改写,但是我推荐文档中的集合绑定,更利于后期维护。

最好的祝福。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Microsoft Edge的HTML超链接打开映射的Windows资源管理器文件夹

当Microsoft AD图形接近EOF时使用Azure资源管理器

无法将我的帐户与Microsoft图形资源管理器一起使用

Azure资源管理器-无法在访问策略中使用Microsoft App Service部署Keyvault

在文件资源管理器中更新Onedrive

如何在UWP背景任务中使用XAML UI元素?

如何在(Uno 2.4)中使用Microsoft.UI.Xaml.Controls资源

Microsoft Graph API查询可在资源管理器上运行,但不能在Microsoft Graph .NET客户端库中使用

Azure 数据资源管理器:如何使用 Kusto 从原始 JSON 事件更新表并更新策略?

是否可以使用自定义UI扩展Service Fabric资源管理器?

在xaml中使用backgroundworker和绑定时,UI不更新

使用团队资源管理器安装Visual Studio 2015企业更新1时出错

资源管理器在我的应用中使用

在资源管理器中使用PowerShell脚本打开文件

如何使用Java代码打开Windows文件资源管理器并突出显示指定的文件?

Windows 10更新KB4013429文件资源管理器崩溃

使用Microsoft Edge打开Windows资源管理器,而不显示“您是要切换应用程序吗?” 信息

UWP Xaml Textblock数据绑定-即使属性已更新,UI也不会更新

Visual Studio 使用代码 1 构建后事件资源管理器退出

创建调用使用图形资源管理器时收到错误代码 401

如何直接从Windows资源管理器中使Spyder打开python脚本(.py文件)

使用资源管理器在Total Commander中删除文件/文件夹

使用BHO扩展Windows资源管理器

使用LoopBack API资源管理器

使用纱线资源管理器的困惑

有什么方法可以打开文件资源管理器并从UWP应用中选择文件?

使用Java的文件资源管理器-如何处理?

使用Windows资源管理器复制文件时如何跳过“计算”时间?

如何更改 .vhd 文件关联以使用 Windows 资源管理器打开?