2
0

Program.cs 3.8 KB

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