我收到了一些编译错误,我不知道为什么,我做错了什么?
static void Main(string[] args)
{
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
Menu startMenu = new Menu() { Title = "Enter a drive" };
// The error is here: Invalid initializer member declarator
MenuItem d = new MenuItem() {Text = "{0}" , drive.Name};
if (drive.IsReady)
{
Console.CursorLeft = 5;
Console.Write(drive.VolumeLabel);
}
// another error: 'System.IO.DriveInfo' does not contain a definition for 'Selected' and no extension method 'Selected' accepting a first argument of type 'System.IO.DriveInfo' could be found (are you missing a using directive or an assembly reference?)
drive.Selected += drive_Selected;
startMenu.Items = new[] {d};
startMenu.Show();
}
}
private static void drive_Selected()
{
}
答案 0 :(得分:1)
第一个错误 - 您应该在班级初始化程序中提供MenuItem
的属性名称
MenuItem d = new MenuItem() {Text = "{0}" , YourPropertyName = drive.Name};
或者只是将驱动器名称分配给Text
属性(如果您只是尝试进行一些格式化,因为文本的“{0}”对我来说很奇怪)
MenuItem d = new MenuItem() {Text = drive.Name };
第二次错误 - DriveInfo
没有Selected
个事件或任何其他事件。它只包含有关驱动器的信息。并且驱动器不知道你已经“选择”了它。假设您需要将Selected
个事件添加到MenuItem
班级。
但是为什么不使用WinForms或WPF来完成这项任务呢?在控制台应用程序中创建这种菜单需要做很多工作。