Program.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // This is a simple example application for a self-contained single file.
  2. using System.Diagnostics;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Globalization;
  5. using Terminal.Gui;
  6. namespace SelfContained;
  7. public static class Program
  8. {
  9. [RequiresUnreferencedCode ("Calls Terminal.Gui.Application.Run<T>(Func<Exception, Boolean>, ConsoleDriver)")]
  10. private static void Main (string [] args)
  11. {
  12. Application.Init ();
  13. #region The code in this region is not intended for use in a self-contained single-file. It's just here to make sure there is no functionality break with localization in Terminal.Gui using single-file
  14. if (Equals (Thread.CurrentThread.CurrentUICulture, CultureInfo.InvariantCulture))
  15. {
  16. Debug.Assert (Application.SupportedCultures.Count == 0);
  17. }
  18. else
  19. {
  20. Debug.Assert (Application.SupportedCultures.Count == 4);
  21. Debug.Assert (Equals (CultureInfo.CurrentCulture, Thread.CurrentThread.CurrentUICulture));
  22. }
  23. #endregion
  24. ExampleWindow app = new ();
  25. Application.Run (app);
  26. // Dispose the app object before shutdown
  27. app.Dispose ();
  28. // Before the application exits, reset Terminal.Gui for clean shutdown
  29. Application.Shutdown ();
  30. // To see this output on the screen it must be done after shutdown,
  31. // which restores the previous screen.
  32. Console.WriteLine ($@"Username: {ExampleWindow.UserName}");
  33. }
  34. }
  35. // Defines a top-level window with border and title
  36. public class ExampleWindow : Window
  37. {
  38. public static string? UserName;
  39. public ExampleWindow ()
  40. {
  41. Title = $"Example App ({Application.QuitKey} to quit)";
  42. // Create input components and labels
  43. var usernameLabel = new Label { Text = "Username:" };
  44. var userNameText = new TextField
  45. {
  46. // Position text field adjacent to the label
  47. X = Pos.Right (usernameLabel) + 1,
  48. // Fill remaining horizontal space
  49. Width = Dim.Fill ()
  50. };
  51. var passwordLabel = new Label
  52. {
  53. Text = "Password:", X = Pos.Left (usernameLabel), Y = Pos.Bottom (usernameLabel) + 1
  54. };
  55. var passwordText = new TextField
  56. {
  57. Secret = true,
  58. // align with the text box above
  59. X = Pos.Left (userNameText),
  60. Y = Pos.Top (passwordLabel),
  61. Width = Dim.Fill ()
  62. };
  63. // Create login button
  64. var btnLogin = new Button
  65. {
  66. Text = "Login",
  67. Y = Pos.Bottom (passwordLabel) + 1,
  68. // center the login button horizontally
  69. X = Pos.Center (),
  70. IsDefault = true
  71. };
  72. // When login button is clicked display a message popup
  73. btnLogin.Accept += (s, e) =>
  74. {
  75. if (userNameText.Text == "admin" && passwordText.Text == "password")
  76. {
  77. MessageBox.Query ("Logging In", "Login Successful", "Ok");
  78. UserName = userNameText.Text;
  79. Application.RequestStop ();
  80. }
  81. else
  82. {
  83. MessageBox.ErrorQuery ("Logging In", "Incorrect username or password", "Ok");
  84. }
  85. };
  86. // Add the views to the Window
  87. Add (usernameLabel, userNameText, passwordLabel, passwordText, btnLogin);
  88. }
  89. }