From 9a273bf86442bfd16a0b69895fb1a2103caa64b5 Mon Sep 17 00:00:00 2001 From: Craig Dunn Date: Wed, 2 Nov 2016 10:53:30 -0400 Subject: [PATCH] add Console agent books --- Collections/Arrays.workbook | 8 ++- Csharp6/csharp6.workbook | 40 +++++------ Visualizers/Visualizers-Console.workbook | 85 ++++++++++++++++++++++++ Visualizers/Visualizers-Mac.workbook | 5 +- Visualizers/Visualizers-iOS.workbook | 6 +- 5 files changed, 115 insertions(+), 29 deletions(-) create mode 100644 Visualizers/Visualizers-Console.workbook diff --git a/Collections/Arrays.workbook b/Collections/Arrays.workbook index 12298c7..a6ea236 100644 --- a/Collections/Arrays.workbook +++ b/Collections/Arrays.workbook @@ -1,6 +1,8 @@ -```json -{"platform":"MacNet45","uti":"com.xamarin.Workbook"} -``` +--- +uti: com.xamarin.workbook +platforms: +- Console +--- # Collections: Arrays diff --git a/Csharp6/csharp6.workbook b/Csharp6/csharp6.workbook index ce2fc78..2bc6f6e 100644 --- a/Csharp6/csharp6.workbook +++ b/Csharp6/csharp6.workbook @@ -1,16 +1,23 @@ -```json -{"platform":"MacNet45","uti":"com.xamarin.Workbook"} -``` +--- +uti: com.xamarin.workbook +platforms: +- Console +--- # Using C# 6 Some examples from Xamarin's [intro to C# 6](https://developer.xamarin.com/guides/cross-platform/advanced/csharp_six/). * Null-conditional operator + * String Interpolation + * Expression-bodied Function Members + * Auto-property Initialization + * Index Initializers + * using static ## Null-conditional operator @@ -75,7 +82,6 @@ foreach (var s in values.Select (i => $"The value is {i,10:N2}.")) { } ``` - ## Expression-bodied Function Members The `ToString` override in the following class is an expression-bodied @@ -140,13 +146,12 @@ types with a simple object-initializer-like syntax: ```csharp var userInfo = new Dictionary { - ["Created"] = NSDate.Now, - ["Due"] = NSDate.Now.AddSeconds(60 * 60 * 24), + ["Created"] = DateTime.Now, + ["Due"] = DateTime.Now.AddSeconds(60 * 60 * 24), ["Task"] = "buy lettuce" }; ``` - ## using static Enumerations, and certain classes such as System.Math, are primarily @@ -161,8 +166,12 @@ C# 6 code can then reference the static members directly, avoiding repetition of the class name (eg. `Math.PI` becomes `PI`): ```csharp -using CoreLocation; -static public double MilesBetween(CLLocationCoordinate2D loc1, CLLocationCoordinate2D loc2) +public class Location +{ + public Location (double lat, double @long) {Latitude = lat; Longitude = @long;} + public double Latitude = 0; public double Longitude = 0; +} +static public double MilesBetween(Location loc1, Location loc2) { double rlat1 = PI * loc1.Latitude / 180; double rlat2 = PI * loc2.Latitude / 180; @@ -178,15 +187,6 @@ static public double MilesBetween(CLLocationCoordinate2D loc1, CLLocationCoordin } ``` - - -```csharp -MilesBetween (new CLLocationCoordinate2D(-12,22), new CLLocationCoordinate2D(-13,33)) -``` - ```csharp -// requires El Capitan -//var sf = await new CLGeocoder().GeocodeAddressAsync("San Francisco, CA"); -//var la = await new CLGeocoder().GeocodeAddressAsync("Los Angeles, CA"); -//MilesBetween (sf, la); -``` +MilesBetween (new Location(-12,22), new Location(-13,33)) +``` \ No newline at end of file diff --git a/Visualizers/Visualizers-Console.workbook b/Visualizers/Visualizers-Console.workbook new file mode 100644 index 0000000..c4395c4 --- /dev/null +++ b/Visualizers/Visualizers-Console.workbook @@ -0,0 +1,85 @@ +--- +uti: com.xamarin.workbook +platforms: +- Console +--- + +# Visualizers (Console) + +Xamarin Workbooks uses a different visualizers for inline code evaluation results: + +* String + +* Object + +* Enumerable + +* Exception + +* Html + +* Help + +By default, the results of a code-block will shown as a string representation of the *last-referenced object* in the block. The `Monkey` class below demonstrates this: when the `rupert` object is assigned, the **ToString** representation is printed after the code-block. + +```csharp +class Monkey { + public string Name; + public string Species; + public string Habitat; + public DateTime Birthday = DateTime.MinValue; + public override string ToString(){ + return $"{Name} the {Species} ({Habitat})"; //C# 6 + } +} +var rupert = new Monkey {Name="Rupert", Species = "Xamarin", Habitat="San Francisco"}; +``` + +Use the popup menu to the right of the result to switch to the **Object Members** view. Most code-block results in Workbooks will have both **ToString** and **Object Members** display options. + +DateTime values have a number of display options, including a calendar view: + +```csharp +rupert.Birthday = new DateTime(2011,05,11); +``` + +Enumerable collections will be expanded so that you can explore their contents. You can also change the view of each individual object in the collection: + +```csharp +var enumerable = new List {"alpha", "beta", "gamma", "delta"}; +``` + +Exceptions have a custom display: + +```csharp +new ArgumentNullException ("name"); +``` + +HTML can also be emitted from code-blocks and rendered in the Workbook. This simple example uses string interpolation to customize an HTML string for display using `AsHtml()`: + +```csharp +var greeting = "Hello, Workbooks"; // C# 6 string interpolation +$"

{greeting}

bold italic underline".AsHtml() +``` + +There’s also a `help` command, which just lists some handy tips. The help list is slightly different for each platform supported by Workbooks. + +```csharp +help +``` + +And finally, not really a visualization, but from the help above you can see that it’s possible to affect the culture of the Workbook. These code-blocks show the date rendered in English and then Spanish (after setting the `CurrentCulture`): + +```csharp +DateTime.Now.ToLongDateString() +``` + +```csharp +CurrentCulture = new System.Globalization.CultureInfo("es"); +DateTime.Now.ToLongDateString() +``` + +```csharp +// reset to English +CurrentCulture = new System.Globalization.CultureInfo("en"); +``` \ No newline at end of file diff --git a/Visualizers/Visualizers-Mac.workbook b/Visualizers/Visualizers-Mac.workbook index 37840f5..ea70f6f 100644 --- a/Visualizers/Visualizers-Mac.workbook +++ b/Visualizers/Visualizers-Mac.workbook @@ -43,7 +43,7 @@ class Monkey { var rupert = new Monkey {Name="Rupert", Species = "Xamarin", Habitat="San Francisco"}; ``` -Use the popup menu to the right of the result to switch to the **Object Members** view. Most code-block results in Workbooks will have both \*\*ToString \*\*and **Object Members** display options. +Use the popup menu to the right of the result to switch to the **Object Members** view. Most code-block results in Workbooks will have both **ToString** and **Object Members** display options. Color types are rendered with an example of the color: @@ -109,5 +109,4 @@ DateTime.Now.ToLongDateString() ```csharp // reset to English CurrentCulture = new System.Globalization.CultureInfo("en"); -``` - +``` \ No newline at end of file diff --git a/Visualizers/Visualizers-iOS.workbook b/Visualizers/Visualizers-iOS.workbook index df7af49..b3b3603 100644 --- a/Visualizers/Visualizers-iOS.workbook +++ b/Visualizers/Visualizers-iOS.workbook @@ -1,7 +1,7 @@ --- uti: com.xamarin.workbook -platform: iOS -packages: [] +platforms: +- iOS --- # Visualizers for iOS @@ -43,7 +43,7 @@ class Monkey { var rupert = new Monkey {Name="Rupert", Species = "Xamarin", Habitat="San Francisco"}; ``` -Use the popup menu to the right of the result to switch to the **Object Members** view. Most code-block results in Workbooks will have both **ToString **and **Object Members** display options. +Use the popup menu to the right of the result to switch to the **Object Members** view. Most code-block results in Workbooks will have both \*\*ToString \*\*and **Object Members** display options. Color types are rendered with an example of the color: