2
0

Program.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.Configuration;
  6. using Terminal.Gui.App;
  7. using Terminal.Gui.ViewBase;
  8. using Terminal.Gui.Views;
  9. namespace SelfContained;
  10. public static class Program
  11. {
  12. [RequiresUnreferencedCode ("Calls Terminal.Gui.Application.Run<T>(Func<Exception, Boolean>, IDriver)")]
  13. private static void Main (string [] args)
  14. {
  15. ConfigurationManager.Enable (ConfigLocations.All);
  16. Application.Init ();
  17. #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
  18. if (Equals (Thread.CurrentThread.CurrentUICulture, CultureInfo.InvariantCulture) && Application.SupportedCultures?.Count == 0)
  19. {
  20. // Only happens if the project has <InvariantGlobalization>true</InvariantGlobalization>
  21. Debug.Assert (Application.SupportedCultures.Count == 0);
  22. }
  23. else
  24. {
  25. Debug.Assert (Application.SupportedCultures?.Count > 0);
  26. Debug.Assert (Equals (CultureInfo.CurrentCulture, Thread.CurrentThread.CurrentUICulture));
  27. }
  28. #endregion
  29. ExampleWindow app = new ();
  30. Application.Run (app);
  31. // Dispose the app object before shutdown
  32. app.Dispose ();
  33. // Before the application exits, reset Terminal.Gui for clean shutdown
  34. Application.Shutdown ();
  35. // To see this output on the screen it must be done after shutdown,
  36. // which restores the previous screen.
  37. Console.WriteLine ($@"Username: {ExampleWindow.UserName}");
  38. }
  39. }
  40. // Defines a top-level window with border and title
  41. public class ExampleWindow : Window
  42. {
  43. public static string? UserName;
  44. public ExampleWindow ()
  45. {
  46. Title = $"Example App ({Application.QuitKey} to quit)";
  47. // Create input components and labels
  48. var usernameLabel = new Label { Text = "Username:" };
  49. var userNameText = new TextField
  50. {
  51. // Position text field adjacent to the label
  52. X = Pos.Right (usernameLabel) + 1,
  53. // Fill remaining horizontal space
  54. Width = Dim.Fill ()
  55. };
  56. var passwordLabel = new Label
  57. {
  58. Text = "Password:", X = Pos.Left (usernameLabel), Y = Pos.Bottom (usernameLabel) + 1
  59. };
  60. var passwordText = new TextField
  61. {
  62. Secret = true,
  63. // align with the text box above
  64. X = Pos.Left (userNameText),
  65. Y = Pos.Top (passwordLabel),
  66. Width = Dim.Fill ()
  67. };
  68. // Create login button
  69. var btnLogin = new Button
  70. {
  71. Text = "Login",
  72. Y = Pos.Bottom (passwordLabel) + 1,
  73. // center the login button horizontally
  74. X = Pos.Center (),
  75. IsDefault = true
  76. };
  77. // When login button is clicked display a message popup
  78. btnLogin.Accepting += (s, e) =>
  79. {
  80. if (userNameText.Text == "admin" && passwordText.Text == "password")
  81. {
  82. MessageBox.Query ("Logging In", "Login Successful", "Ok");
  83. UserName = userNameText.Text;
  84. Application.RequestStop ();
  85. }
  86. else
  87. {
  88. MessageBox.ErrorQuery ("Logging In", "Incorrect username or password", "Ok");
  89. }
  90. // When Accepting is handled, set e.Handled to true to prevent further processing.
  91. e.Handled = true;
  92. };
  93. // Add the views to the Window
  94. Add (usernameLabel, userNameText, passwordLabel, passwordText, btnLogin);
  95. }
  96. }