C# console basic UI with timer
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
}
}
我正在制作一个基本的控制台应用程序(不幸的是,这是必须的),它显示时间和一个非常基本的界面,用户可以在其中输入选项。当他们这样做时,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
}
}