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

Update TreeView file explorer code to use Microsoft.UI.Xaml in UWP

我正在尝试将我的旧源代码更新为 UWP 引用 Microsoft.UI.Xaml.dll TreeView 控件到 display/replicate 存储在字符串 [].

中的文件和文件夹

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

示例:

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

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

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

C:\Users\User\Documents\folder\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 控件更新此代码? 我以为这应该很容易,但我觉得我错过了什么。

您的代码应该是 WPF 或 WinForm。 UWP中的TreeView相对于它们有很多变化。如果你想从头开始处理这个控件,你可以检查这个 document.

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

  1. 创建一个class来保存显示文字和真实路径
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 中,许多 class 名称和方法已更改。
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
    }
}

以便我们可以使用它

Xaml

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

Xaml.cs

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"
    }, '\');
}

看起来像这样:


代码可以这样重写,但是我推荐document中的集合绑定,更有利于后期维护

此致。