Program.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // This is a test 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) && Application.SupportedCultures.Count == 0)
  15. {
  16. // Only happens if the project has <InvariantGlobalization>true</InvariantGlobalization>
  17. Debug.Assert (Application.SupportedCultures.Count == 0);
  18. }
  19. #endregion
  20. ExampleWindow app = new ();
  21. Application.Run (app);
  22. // Dispose the app object before shutdown
  23. app.Dispose ();
  24. // Before the application exits, reset Terminal.Gui for clean shutdown
  25. Application.Shutdown ();
  26. // To see this output on the screen it must be done after shutdown,
  27. // which restores the previous screen.
  28. Console.WriteLine ($@"Username: {ExampleWindow.UserName}");
  29. }
  30. }
  31. // Defines a top-level window with border and title
  32. public class ExampleWindow : Window
  33. {
  34. public static string? UserName;
  35. public ExampleWindow ()
  36. {
  37. Title = $"Example App ({Application.QuitKey} to quit)";
  38. // Create input components and labels
  39. var usernameLabel = new Label { Text = "Username:" };
  40. var userNameText = new TextField
  41. {
  42. // Position text field adjacent to the label
  43. X = Pos.Right (usernameLabel) + 1,
  44. // Fill remaining horizontal space
  45. Width = Dim.Fill ()
  46. };
  47. var passwordLabel = new Label
  48. {
  49. Text = "Password:", X = Pos.Left (usernameLabel), Y = Pos.Bottom (usernameLabel) + 1
  50. };
  51. var passwordText = new TextField
  52. {
  53. Secret = true,
  54. // align with the text box above
  55. X = Pos.Left (userNameText),
  56. Y = Pos.Top (passwordLabel),
  57. Width = Dim.Fill ()
  58. };
  59. // Create login button
  60. var btnLogin = new Button
  61. {
  62. Text = "Login",
  63. Y = Pos.Bottom (passwordLabel) + 1,
  64. // center the login button horizontally
  65. X = Pos.Center (),
  66. IsDefault = true
  67. };
  68. // When login button is clicked display a message popup
  69. btnLogin.Accept += (s, e) =>
  70. {
  71. if (userNameText.Text == "admin" && passwordText.Text == "password")
  72. {
  73. MessageBox.Query ("Logging In", "Login Successful", "Ok");
  74. UserName = userNameText.Text;
  75. Application.RequestStop ();
  76. }
  77. else
  78. {
  79. MessageBox.ErrorQuery ("Logging In", "Incorrect username or password", "Ok");
  80. }
  81. };
  82. // Add the views to the Window
  83. Add (usernameLabel, userNameText, passwordLabel, passwordText, btnLogin);
  84. }
  85. }