Program.cs 3.7 KB

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