c# - 具有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
. 有谁知道为什么这对我不起作用?
解决方案
您的代码中有两个问题:
您的
GetLogicalDrives
方法在其主体中调用自身,它将无法使用您的逻辑,因为没有要浏览的列表。您的
using
文件中缺少指令。要访问Directory
类,然后调用GetLogicalDrivers
必须using System.IO
在文件顶部添加的方法。
我希望这能帮到您。
推荐阅读
- cypress - 如何在 cypress 中制作变量 globle,我们可以在规范文件中的所有 TestCase 中使用它
- batch-file - 要求用户输入时语法不正确
- twitter-bootstrap - 使单选按钮组全宽 BootstrapVue
- html - 在 tumblr 网站顶部添加基本横幅
- javascript - 第二个输入 onclick() 函数不起作用
- c# - 使用客户端凭据授予类型自定义 Azure AD 令牌
- python - excel文件的文件路径
- c# - 如何在c#中阅读所有邮件,包括来自outlook的特定主题
- vb.net - VB.NET 如何一次性将整个 DataGridView 数据导出/插入到 Sqlite 表中
- noraui - 在 NoraUI 中从 excel 文件中读取数据时出现读取问题