Introduction To Xamarin Workbooks

Xamarin Workbooks is a unique product that allows you to write documentation, with runnable code examples and provides a way to visualize the user interfaces. You can create these workbooks for Xamarin Android, Xamarin iOS, WPF and Console. As of the date of this post, Xamarin.Android and Xamarin.iOS don’t have great (or any?) support in the Windows version, hence you will want to use your Mac, unless you plan to just use WPF or Console.

You will need to download Xamarin Workbooks separately. This example will go through and create a Hello World app for Xamarin.iOS on Xamarin Forms. Hopefully in the future, Workbooks will be expanded to have better Windows and Xamarin Forms support. XAML support would definitely be awesome too.

Creating Workbook

When you open Workbooks, you choose which type of Workbook you want.

createworkbookmac

On Windows you see a few different options.

createworkbook

It will automatically open the emulator or simulator for you. For example, if you select Xamarin.iOS, you will see the Simulator load with the workbook.

openworkbookmac

Walk-through

When the workbook opens you will notice a place your can write and a few small buttons. This is called a Cell. The one open by default allows you to write code, however you can add more cells for code or documentation as shown below.

addcell

Press File > Add Package, will allow you to add Nuget packages to your workbook.

workbookaddpackage

When you have added a package, it will show the imports at the top of the workbook. I then manually added in the appropriate usings for Xamarin Forms.

xamarinformsusing

Xamarin Forms App

Now, lets create the Xamarin Forms application class and I placed a simple HelloWorld label inside a ContentPage. Then ensure your initialize Forms. Lastly you need to implement a slight hack by assigning a ViewController to the RootViewController. Thanks to Craig Dunn for his blog post for this tip.

xamarinformshelloworld

Under the hood

When you save the Workbook you will get a <name>.Workbook, which is actually a folder/package that contains different .workbook files inside. Inside it is Markup language. From the example above it created an index.workbook and this is what it generated.

---
uti: com.xamarin.workbook
platforms:
- iOS
packages:
- id: Xamarin.Forms
 version: 2.3.3.168
---

```csharp
#r "Xamarin.Forms.Platform.iOS"
#r "Xamarin.Forms.Core"
#r "Xamarin.Forms.Xaml"
#r "Xamarin.Forms.Platform"
```

```csharp
using Xamarin.Forms; 
using Xamarin.Forms.Platform.iOS;
```

```csharp
public class App: Application
{
    public App()
    {
        MainPage = new ContentPage() 
        { Content = new Label() { Text="Hello World!" },
          Padding=20 };
    }
}
```

```csharp
Forms.Init();
var app = new App();
KeyWindow.RootViewController = app.MainPage.CreateViewController();
```
Microsoft MVP | Xamarin Certified Developer |
Exrin Creator | Xamarin Forms Developer | Melbourne, Australia

Related Posts

Leave A Comment?