浏览代码

fix: sample in readme so it compiles (#1162)

Dag König 4 年之前
父节点
当前提交
f1fa8256de
共有 1 个文件被更改,包括 63 次插入57 次删除
  1. 63 57
      README.md

+ 63 - 57
README.md

@@ -96,33 +96,32 @@ You can force the use of `System.Console` on Unix as well; see `Core.cs`.
 See the [`Terminal.Gui/` README](https://github.com/migueldeicaza/gui.cs/tree/master/Terminal.Gui) for an overview of how the library is structured. The [Conceptual Documentation](https://migueldeicaza.github.io/gui.cs/articles/index.html) provides insight into core concepts.
 See the [`Terminal.Gui/` README](https://github.com/migueldeicaza/gui.cs/tree/master/Terminal.Gui) for an overview of how the library is structured. The [Conceptual Documentation](https://migueldeicaza.github.io/gui.cs/articles/index.html) provides insight into core concepts.
 
 
 ### Sample Usage
 ### Sample Usage
-
+The code below is done with the new [Top-level statements](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9#top-level-statements) in C# 9.0.  
 ```csharp
 ```csharp
 using Terminal.Gui;
 using Terminal.Gui;
+using NStack;
 
 
-class Demo {
-	static void Main ()
-	{
-		Application.Init ();
-		var top = Application.Top;
-
-		// Creates the top-level window to show
-		var win = new Window ("MyApp") {
-			X = 0,
-			Y = 1, // Leave one row for the toplevel menu
-
-			// By using Dim.Fill(), it will automatically resize without manual intervention
-			Width = Dim.Fill (),
-			Height = Dim.Fill ()
-		};
-	
-		top.Add (win);
-
-		// Creates a menubar, the item "New" has a help menu.
-		var menu = new MenuBar (new MenuBarItem [] {
+Application.Init();
+var top = Application.Top;
+
+// Creates the top-level window to show
+var win = new Window("MyApp")
+{
+	X = 0,
+	Y = 1, // Leave one row for the toplevel menu
+
+	// By using Dim.Fill(), it will automatically resize without manual intervention
+	Width = Dim.Fill(),
+	Height = Dim.Fill()
+};
+
+top.Add(win);
+
+// Creates a menubar, the item "New" has a help menu.
+var menu = new MenuBar(new MenuBarItem[] {
 			new MenuBarItem ("_File", new MenuItem [] {
 			new MenuBarItem ("_File", new MenuItem [] {
-				new MenuItem ("_New", "Creates new file", NewFile),
-				new MenuItem ("_Close", "", () => Close ()),
+				new MenuItem ("_New", "Creates new file", null),
+				new MenuItem ("_Close", "",null),
 				new MenuItem ("_Quit", "", () => { if (Quit ()) top.Running = false; })
 				new MenuItem ("_Quit", "", () => { if (Quit ()) top.Running = false; })
 			}),
 			}),
 			new MenuBarItem ("_Edit", new MenuItem [] {
 			new MenuBarItem ("_Edit", new MenuItem [] {
@@ -131,41 +130,48 @@ class Demo {
 				new MenuItem ("_Paste", "", null)
 				new MenuItem ("_Paste", "", null)
 			})
 			})
 		});
 		});
-		top.Add (menu);
-
-		var login = new Label ("Login: ") { X = 3, Y = 2 };
-		var password = new Label ("Password: ") {
-			X = Pos.Left (login),
-			Y = Pos.Top (login) + 1
-		};
-		var loginText = new TextField ("") {
-			X = Pos.Right (password),
-			Y = Pos.Top (login),
-			Width = 40
-		};
-		var passText = new TextField ("") {
-			Secret = true,
-			X = Pos.Left (loginText),
-			Y = Pos.Top (password),
-			Width = Dim.Width (loginText)
-		};
-	
-		// Add some controls, 
-		win.Add (
-			// The ones with my favorite layout system, Computed
-  			login, password, loginText, passText,
-
-			// The ones laid out like an australopithecus, with Absolute positions:
-			new CheckBox (3, 6, "Remember me"),
-			new RadioGroup (3, 8, new [] { "_Personal", "_Company" }),
-			new Button (3, 14, "Ok"),
-			new Button (10, 14, "Cancel"),
-			new Label (3, 18, "Press F9 or ESC plus 9 to activate the menubar")
-		);
-
-		Application.Run ();
-	}
+top.Add(menu);
+
+static bool Quit()
+{
+	var n = MessageBox.Query(50, 7, "Quit Demo", "Are you sure you want to quit this demo?", "Yes", "No");
+	return n == 0;
 }
 }
+
+var login = new Label("Login: ") { X = 3, Y = 2 };
+var password = new Label("Password: ")
+{
+	X = Pos.Left(login),
+	Y = Pos.Top(login) + 1
+};
+var loginText = new TextField("")
+{
+	X = Pos.Right(password),
+	Y = Pos.Top(login),
+	Width = 40
+};
+var passText = new TextField("")
+{
+	Secret = true,
+	X = Pos.Left(loginText),
+	Y = Pos.Top(password),
+	Width = Dim.Width(loginText)
+};
+
+// Add some controls, 
+win.Add(
+	// The ones with my favorite layout system, Computed
+	login, password, loginText, passText,
+
+	// The ones laid out like an australopithecus, with Absolute positions:
+	new CheckBox(3, 6, "Remember me"),
+	new RadioGroup(3, 8, new ustring[] { "_Personal", "_Company" }, 0),
+	new Button(3, 14, "Ok"),
+	new Button(10, 14, "Cancel"),
+	new Label(3, 18, "Press F9 or ESC plus 9 to activate the menubar")
+);
+
+Application.Run();
 ```
 ```
 
 
 Alternatively, you can encapsulate the app behavior in a new `Window`-derived class, say `App.cs` containing the code above, and simplify your `Main` method to:
 Alternatively, you can encapsulate the app behavior in a new `Window`-derived class, say `App.cs` containing the code above, and simplify your `Main` method to: