Program.cs 3.7 KB

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