-3

我收到几个编译错误,我不知道为什么,我做错了什么?

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()
{
}
4

1 回答 1

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 来完成这项任务呢?您必须做很多工作才能在控制台应用程序中创建这种菜单。

于 2012-11-15T09:29:59.487 回答