Xamarin.iOS – Intro to using a Xamarin Workbook

In this lesson

  • Output Trace Statements
  • Add Rich Text Documentation
  • Interact with the REPL


Tap on time to skip ahead


Hello fellow Xamarin nerds. In this lesson we’re going to cover the awesome new Xamarin Workbook functionality that comes with Xamarin’s new Inspector tool.


We’re going to kick things off by making sure our Xamarin Inspector app has been launched. I’m going to go up to File, New. Each Xamarin Workbook within the Xamarin Inspector tool needs to be associated with a particular platform. As of right now, we don’t have the forms platform but we do have Xamarin.Mac, Xamarin.iOS and Xamarin.Android. For this demonstration I’m going to select Xamarin.iOS and hit Create Workbook.


When we first create the Workbook you’ll see we have this loading indicator, that’s because Workbook needs to connect to our iOS simulator, as you can see here on the right. As with any new platform or tool or computer language, we like to kick things off and get ourselves oriented by writing a Hello World statement.


To do that, I’m going to type Console.WriteLine. As you see as I type I have this really nice Intellisense that will let me know what methods and properties I can access with a particular piece of functionality I’m interacting with. I’m going to say Hello World. Then I’m going to add a semicolon and hit return. We can see what our output for this line of code would be.


One of the things that makes the Workbook functionality of Xamarin Inspector pretty cool and interesting is that we can also annotate and comment our code with regular rich text. I can type in here, “This is an example of using Console to output a trace statement.” If I select my text I can format it. I can make this bold, I can also make console bold. If I want to link to something, I can do that here, etc. It’s really cool. This creates a lot of potential for a Xamarin Workbook for documentation or education or tutorial purposes. Hence, why I’m doing this lesson.


Writing out to the console is all well and good. What else can we do with our code? I’m going to create a list of colors. We’re going to do red, green, blue to kick things off. If I hit return, we’ll see the workbook REPL let’s us know what we instantiated and what’s inside.


Something else I can do is now that I’ve declared this color list variable, I can also say, “foreach (var c in colorsList)” and now when I hit return after hitting the opening curly brace, I can enter in a new line of code for this block of code. I can say, “Console.WriteLine(c)”. Now since I closed my curly braked, if I hit return- Oh, we have this nice compiler error. I didn’t finish my line correctly. And now since I fixed that I can now see the output of our various colors.


Now if I want to update my list here. If I want to add the color purple, if I hit return, we see I know have that. However, my output here has not updated but it has been greyed out to tell me that it is out of date. If I hit return here, now I can see the updated contents.


Also, if I remove this variable declaration, now when I hit return we see nothing gets outputted. Here I now get a runtime error. That colorsList does not exist. The scope works very similar to what you’re used to in a real coding environment. Now, if I update that- colorsList. Hit return here. Hit return there. And now we’re good to go.


While using primitives and what not in C# is cool and what not, in the real world when we’re making an application, we’re going to need to use more sophisticated features of the .net framework. With this in mind, the Workbooks functionality of Xamarin Inspector allows us to import different name space. In this particular instance, I’m going to import System.Net. And now I should have access to all the functionality to System.Net. I can say new WebClient. Create my instance. And here I can inspect WebClient and I can see my base address is empty. Let’s go ahead and populate that. I’m going to say base address equals http://google.com. Here again, we get a null output because this particular statement doesn’t return anything nor are we writing anything out to the console.


Now I’m going to say var contents = wc.DownloadString. We’re not going to pass a URI, we’re just going to say the root of the URL. Now when I hit return, we can see that the WebClient successfully hit google.com and here’s all the contents of that particular webpage.


As you can imagine, this is a great opportunity to maybe write documentation on how to use web service or how to use a particular library that you may have written. As you can see there’s a lot of potential with a Xamarin Workbook.