簡體   English   中英

具有計時器的C#控制台基本UI

[英]C# console basic UI with timer

我正在制作一個基本的控制台應用程序(不幸的是,這是必須的),它顯示了時間以及一個非常基本的界面,用戶可以在其中輸入選項。 完成后,UI會更新以顯示相關的新數據,並允許進一步的輸入等。 始終在頂部顯示帶有秒的實時時鍾。

我基本上要做的是每秒重新繪制整個UI,並嘗試聽用戶輸入以指導下次如何繪制UI。 重繪UI時如何管理用戶輸入? 有沒有更好的辦法?

這是整個程序(非常簡化的版本):

using System;
using System.Diagnostics;
using System.Timers;

namespace TimerTest
{
    class Program
    {
        static bool useStartUI = false;
        static string currentInput = "";
        static Timer aTimer = new System.Timers.Timer();
        static void Main(string[] args)
        {
            aTimer.Elapsed += new ElapsedEventHandler(DrawUI);
            aTimer.Interval = 1000;
            aTimer.Enabled = true;
            Console.ReadLine();
            currentInput = Console.ReadLine();
        }
        static void DrawUI(object source, ElapsedEventArgs e)
        {
            Debug.WriteLine("currentInput = " + currentInput);
            if (currentInput == "A")
            {
                useStartUI = true;
            } else
            {
                useStartUI = false;
            }
            if (!useStartUI)
            {
                DisplayStartUI();
            }
            else
            {
                DisplayCurrentUI();
            }
        }
        private static void DisplayStartUI()
        {
            Console.Clear();
            Console.WriteLine("DisplayStartUI - " +     DateTime.Now.ToString("HH:mm:ss tt"));
            Console.WriteLine("Press 'B' to switch to CurrentUI");
            Console.ReadLine();
            currentInput = Console.ReadLine();
        }
        private static void DisplayCurrentUI()
        {
            Console.Clear();
            Console.WriteLine("DisplayCurrentUI - " +     DateTime.Now.ToString("HH:mm:ss tt"));
            Console.WriteLine("Press 'A' to switch to StartUI");
            Console.ReadLine();
            currentInput = Console.ReadLine();
        }
    }
}

目前我有Console.ReadLine(); 計時器啟動后,如果我不這樣做,則應用程序立即打開和關閉。 另外兩個Console.ReadLine(); 被完全忽略,但是正是在這些時候,我希望記錄用戶輸入。

似乎我在考慮這個問題,@ elgonzo向我指出了正確的方向。 現在,我只在一行上顯示計時器,並在需要時清除其他行。 我有一個基本的工作示例,將把我的主程序擴展到其中。 它似乎完全符合我的要求(顯然缺少驗證和功能),請對此進行評論,如果可以在以下方面進行改進:

using System;
using System.Diagnostics;
using System.Timers;

namespace TimerTest
{
    class Program
    {
        static bool appOpen = true;
        static bool useStartUI = false;
        static string currentInput = "";
        static Timer aTimer = new System.Timers.Timer();
        static void Main(string[] args)
        {
            Console.WriteLine("Main - " + DateTime.Now.ToString("HH:mm:ss tt"));
            aTimer.Elapsed += new ElapsedEventHandler(DrawClock);
            aTimer.Interval = 1000;
            aTimer.Enabled = true;

            while (appOpen) // to keep the application running and give a possible exit - will expand this
            {
                DrawUI();
            }
        }
        #region UI region
        static void DrawClock(object source, ElapsedEventArgs e)
        {
            Console.SetCursorPosition(0, 0);
            Console.WriteLine("DrawClock - " + DateTime.Now.ToString("HH:mm:ss     tt"));
            Console.SetCursorPosition(0, 7);
        }
        static void DrawUI()
        {
            Debug.WriteLine("currentInput = " + currentInput);
            if (currentInput == "A" || currentInput == "a")
            {
                useStartUI = true;
            }
            else if (currentInput == "B" || currentInput == "b")
            {
                useStartUI = false;
            }
            if (useStartUI)
            {
                DisplayStartUI();
            }
            else
            {
                DisplayCurrentUI();
            }
        }
        private static void DisplayStartUI()
        {
            CleanUI();
            Console.WriteLine("DisplayStartUI");
            Console.WriteLine("Press 'B' to switch to CurrentUI");

            ConsoleKeyInfo keyPressed = Console.ReadKey();
            Console.SetCursorPosition(0, 7);
            Console.WriteLine("You pressed {0}", keyPressed.KeyChar);
            currentInput = keyPressed.KeyChar.ToString();
            if (keyPressed.KeyChar.ToString() == "b")
            {
                DrawUI();
            }
        }
        private static void DisplayCurrentUI()
        {
            CleanUI();
            Console.WriteLine("DisplayCurrentUI");
            Console.WriteLine("Press 'A' to switch to StartUI");
            ConsoleKeyInfo keyPressed = Console.ReadKey();
            Console.SetCursorPosition(0, 7);
            Console.WriteLine("You pressed {0}", keyPressed.KeyChar);
            currentInput = keyPressed.KeyChar.ToString();
            if (keyPressed.KeyChar.ToString() == "a")
            {
                DrawUI();
            }
        }
        #endregion
        #region functional methods
        public static void CleanUI()
        {
            Console.SetCursorPosition(0, 5);
            ClearCurrentConsoleLine();
            Console.SetCursorPosition(0, 6);
            ClearCurrentConsoleLine();
            Console.SetCursorPosition(0, 7);
            ClearCurrentConsoleLine();
            Console.SetCursorPosition(0, 5);
        }
        public static void ClearCurrentConsoleLine()
        {
            int currentLineCursor = Console.CursorTop;
            Console.SetCursorPosition(0, Console.CursorTop);
            Console.Write(new string(' ', Console.WindowWidth));
            Console.SetCursorPosition(0, currentLineCursor);
        }
        #endregion
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM