首页 > 解决方案 > 具有MVVM结构的视图树中的文件夹视图?

问题描述

我在一个名为 data 的项目中有三个文件,它是 Visual Studio 中的一个数据库 我看过一个名为 MVVM 教程 c# 的教程和一个文件树!这是链接:https ://www.youtube.com/watch?v= U2ZvZwDZmJU 在大约 12:19 他写了这行代码

return GetLogicalDrives().Select(drive => new DirectoryItem { fullPath = drive, Type = DirectoryItemType.Drive }).ToList(); 

这段代码对我不起作用,我知道我没有Directory.GetLogicalDrives,但这不是我的文件夹视图的样子,所以我在视频中使用的三个不同文件的命名空间是数据,这是我的代码三个文件...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Data
{
    public static  class DirectoryStructure
    {
        public static List<DirectoryItem> GetLogicalDrives()
        {
           return GetLogicalDrives().Select(drive => new DirectoryItem { fullPath = drive, Type = DirectoryItemType.Drive }).ToList(); 
        }

        public static string GetFilefolderName(string path)
        {
            if(string.IsNullOrEmpty(path))
            {
                return string.Empty;    
            }

            var normalizedPath = path.Replace('/', '\\');    
            var lastIndex = normalizedPath.LastIndexOf('\\');

            if(lastIndex <= 0)
            {
                return path; 
            }

            return path.Substring(lastIndex + 1);   
        }
    }

    public class DirectoryItem
    {
        public DirectoryItemType Type { get; set;}
        public string fullPath { get; set; }
        public string Name { get { return DirectoryStructure.GetFilefolderName(this.fullPath); } }  
    }

    public enum DirectoryItemType
    {
        Drive,
        File, 
        Folder
    }
}

我不知道为什么我从该行收到错误?我的文件夹结构看起来像这个项目:这个文件夹中的 Data \Directory 我有 DirectoryStructure 和我在同一文件路径下的其余文件,但还有一个名为DataFolder. 有谁知道为什么这对我不起作用?

标签: c#mvvm

解决方案


您的代码中有两个问题:

  1. 您的GetLogicalDrives方法在其主体中调用自身,它将无法使用您的逻辑,因为没有要浏览的列表。

  2. 您的using文件中缺少指令。要访问Directory类,然后调用GetLogicalDrivers必须using System.IO在文件顶部添加的方法。

我希望这能帮到您。


推荐阅读