windows 我的程序不显示选择菜单,除非我按向上或向下箭头键

5lhxktic  于 2023-06-30  发布在  Windows
关注(0)|答案(1)|浏览(125)

所以我一直在研究一个项目,基本上可以治愈你的无聊。(我想在学校的人使用计算机时向他们展示这个程序)。
代码中的选择菜单取自这篇文章:
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;
        }
    }
}
juzqafwq

juzqafwq1#

正如@MathiasR.Jessen已经指出的那样,在用户按下Enter键之后,您还必须调用WriteMenu
你可以做一些简化。最引人注目的一点是,您已经多次定义了菜单项。相反,只定义一次菜单选项,并跟踪最后选择的索引,并使用一些逻辑在相应的菜单条目前面写入>。我使用了Console.Write(i == lastSelection ? "> " : " ");,并从开始菜单选项中删除白色。
此外,您可以使用switch语句来处理keyinfo.Key,以使其更容易。

internal class Program
{
    private static List<Option> options;
    private static int lastSelection = -1; // Where we write the ">".

    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)
            switch (keyinfo.Key) {
                case ConsoleKey.DownArrow:
                    if (index + 1 < options.Count) {
                        index++;
                    }
                    break;
                case ConsoleKey.UpArrow:
                    if (index > 0) {
                        index--;
                    }
                    break;
                case ConsoleKey.Enter:
                    Console.Clear(); // The only place where we clear.
                    options[index].Selected.Invoke();
                    lastSelection = index;
                    index = 0;
                    break;
            }
            WriteMenu(options, options[index]);
        }
        while (keyinfo.Key != ConsoleKey.X);

        Console.ReadKey();
    }

    static void WriteAt(int left, int top, string text)
    {
        Console.CursorLeft = left;
        Console.CursorTop = top;
        Console.Write(text);
    }

    static void WriteMenu(List<Option> options, Option selectedOption)
    {
        // Don't clear here since we want to preserve the output the selected menu item.
        // Instead, let's use our new method WriteAt to write at a specific place.
        for (int i = 0; i < options.Count; i++) {
            Option option = options[i];
            if (option == selectedOption) {
                Console.Write("\u001b[48;2;50;50;50m");
            } else {
                Console.Write("\u001b[48;2;12;12;12m");
            }
            WriteAt(1, i, i == lastSelection ? "> " : "  ");
            Console.Write(option.Name);
        }
    }

    static void HomePageLink()
    {
        WriteAt(40, 0, "My");
        WriteAt(40, 1, "Home");
        WriteAt(40, 2, "Page");
    }

    static void RequestPageLink()
    {
        WriteAt(40, 0, "The");
        WriteAt(40, 1, "Request");
        WriteAt(40, 2, "Page");
    }

    static void UpdatePageLink()
    {
        WriteAt(40, 0, "The");
        WriteAt(40, 1, "Update");
        WriteAt(40, 2, "Page");
    }

    static void ConfigurePageLink()
    {
        WriteAt(40, 0, "The");
        WriteAt(40, 1, "Configuration");
        WriteAt(40, 2, "Page");
    }
}

class Option未显示。

相关问题