所以我一直在研究一个项目,基本上可以治愈你的无聊。(我想在学校的人使用计算机时向他们展示这个程序)。
代码中的选择菜单取自这篇文章:
C# Console app - How do I make an interactive menu?
但是有一个bug(或者更像是我的错误)当你在一个选择上按回车键时,屏幕清除,只有当我按向上或向下箭头键时才刷新?
下面是代码
using System.Text;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
namespace Awesome_C__Project
{
internal class Program
{
public static List<Option> options;
static void Main(string[] args)
{
Encoding UTF8 = Encoding.UTF8;
Console.OutputEncoding = UTF8;
// Create options that you want your menu to have
options = new List<Option>
{
new Option(" 🏡 Home ", () => HomePageLink()),
new Option(" 📩 Request ", () => RequestPageLink()),
new Option(" ⚙️ Configure ", () => ConfigurePageLink()),
new Option(" 💫 Updates ", () => UpdatePageLink()),
new Option(" ❌ Exit ", () => Environment.Exit(0)),
};
// Set the default index of the selected item to be the first
int index = 0;
// Write the menu out
WriteMenu(options, options[index]);
// Store key info in here
ConsoleKeyInfo keyinfo;
do
{
keyinfo = Console.ReadKey();
// Handle each key input (down arrow will write the menu again with a different selected item)
if (keyinfo.Key == ConsoleKey.DownArrow)
{
if (index + 1 < options.Count)
{
index++;
WriteMenu(options, options[index]);
}
}
if (keyinfo.Key == ConsoleKey.UpArrow)
{
if (index - 1 >= 0)
{
index--;
WriteMenu(options, options[index]);
}
}
// Handle different action for the option
if (keyinfo.Key == ConsoleKey.Enter)
{
options[index].Selected.Invoke();
index = 0;
}
}
while (keyinfo.Key != ConsoleKey.X);
Console.ReadKey();
}
// Default action of all the options. You can create more methods
static void HomePageLink()
{
Console.Clear();
options = new List<Option>
{
new Option("> 🏡 Home ", () => HomePageLink()),
new Option(" 📩 Request ", () => RequestPageLink()),
new Option(" ⚙️ Configure ", () => ConfigurePageLink()),
new Option(" 💫 Updates ", () => UpdatePageLink()),
new Option(" ❌ Exit ", () => Environment.Exit(0)),
};
}
static void RequestPageLink()
{
Console.Clear();
options = new List<Option>
{
new Option(" 🏡 Home ", () => HomePageLink()),
new Option("> 📩 Request ", () => RequestPageLink()),
new Option(" ⚙️ Configure ", () => ConfigurePageLink()),
new Option(" 💫 Updates ", () => UpdatePageLink()),
new Option(" ❌ Exit ", () => Environment.Exit(0)),
};
}
static void UpdatePageLink()
{
Console.Clear();
options = new List<Option>
{
new Option(" 🏡 Home ", () => HomePageLink()),
new Option(" 📩 Request ", () => RequestPageLink()),
new Option(" ⚙️ Configure ", () => ConfigurePageLink()),
new Option("> 💫 Updates ", () => UpdatePageLink()),
new Option(" ❌ Exit ", () => Environment.Exit(0)),
};
}
static void ConfigurePageLink()
{
Console.Clear();
options = new List<Option>
{
new Option(" 🏡 Home ", () => HomePageLink()),
new Option(" 📩 Request ", () => RequestPageLink()),
new Option("> ⚙️ Configure ", () => ConfigurePageLink()),
new Option(" 💫 Updates ", () => UpdatePageLink()),
new Option(" ❌ Exit ", () => Environment.Exit(0)),
};
}
static void WriteMenu(List<Option> options, Option selectedOption)
{
Console.Clear();
foreach (Option option in options)
{
if (option == selectedOption)
{
Console.Write("\u001b[48;2;50;50;50m");
}
else
{
Console.Write("\u001b[48;2;12;12;12m");
}
Console.WriteLine(option.Name);
}
}
}
public class Option
{
public string Name { get; }
public Action Selected { get; }
public Option(string name, Action selected)
{
Name = name;
Selected = selected;
}
}
}
1条答案
按热度按时间juzqafwq1#
正如@MathiasR.Jessen已经指出的那样,在用户按下Enter键之后,您还必须调用
WriteMenu
。你可以做一些简化。最引人注目的一点是,您已经多次定义了菜单项。相反,只定义一次菜单选项,并跟踪最后选择的索引,并使用一些逻辑在相应的菜单条目前面写入
>
。我使用了Console.Write(i == lastSelection ? "> " : " ");
,并从开始菜单选项中删除白色。此外,您可以使用switch语句来处理
keyinfo.Key
,以使其更容易。class Option
未显示。