In this tutorial series, we are going to learn C# using the Raspberry Pi 2 and WinIoT. The goal will be to learn about the C# language by building projects with it. So, think about it as learning through building. As we progress through the tutorial, the projects will grow in complexity and we'll start to create some pretty amazing things.


Why C#?

C# is a very popular language, and for good reason. It allows for complex programs to be simply and clearly stated. This allows you, as the developer, to spend more time concentrating on the problem that you wish to solve and less time writing/reading code.


Why Raspberry Pi 2?

The Raspberry Pi 2 is a low cost, powerful board. The low cost makes it very approachable for hobbyists and the power that it has makes applicable to a wide range of projects.


Why WinIoT?

Windows is the operating system that most people are familiar with because they use it every day. This familiarity lowers the barrier to entry and allows for new developers to get up and running quickly.



Getting the initial set up figured out can be the most difficult and frustrating part of getting started with a new board. Fortunately, getting set up with WinIoT on the RPi2 is pretty easy. There is a great tutorial of how to get started here:


Once you have set up your PC and your Rpi2, you can follow the example to blink an LED. Another option would be to write a classic Hello, World application:


Getting Started with Visual Studio

Visual Studio is a very, very powerful program. Unfortunately, with all of that power, sometimes it can become overwhelming. Don't despair though, after the initial shock wears off, it is really pretty easy to use.


When you first open Visual Studio, it will present you with a start page in the center of the screen. In the top left, there will be an option to create a “New Project...” Clicking on that will bring up the New Project window. In this window, you want to select the “Blank App (Universal Windows)” option. Then give it a name and click OK. Now if the “Blank App (Universal Windows)” option is not present, you can find it under Templates -> Visual C# -> Windows -> Universal, or by using the search box to search for “universal c#”.


Now, once you are in Visual Studio, let's take a quick tour:


The first thing to point out is the Solution Explorer on the right hand side. That shows you the directory structure of the project that you are currently working on. Creating a new project automatically populates some basic information for you with common things that every project needs. You can go ahead and click around in there and see what there is to see. Don't worry if it is a little bit too much at this point. Most of the stuff in there is going to be the same for every project that we do, so we can safely ignore it and just use the defaults.


Double clicking on any file in the Solution Explorer will bring it up in the main view. (In the above screen shot, I have MainPage.xaml up in the main view.) There are two files to take a closer look at: MainPage.xaml and MainPage.xaml.cs. To get to MainPage.xaml.cs, just click the triangle on the left of MainPage.xaml in the Solution Explorer to expand/collapse the child elements. MainPage.xaml.cs is a child of MainPage.xaml.


When the MainPage.xaml is displayed, by default the screen will be split into a design view and a XAML view. The XAML view shows the source code, which looks a lot like XML. The design view will give you an idea of what it will look like when it is displayed on the screen.


XAML is the language that is used to describe what the UI will look like. It's a concise way to tell the computer what goes where, and how it should look. Everything that can be done in XAML can also be done in C#, but it is not nearly as neat and clean. The same XAML code will take many more lines of code in C#. XAML also helps separate how the information is displayed from the business logic. This turns out to be extremely helpful in reusing the same code for various different applications.


The MainPage.xaml.cs file has C# code in it. This should look a lot more familiar to you if you have seen other programming languages. C# is where all of the logic of the program will be written.


Next up on the tour is the Output window, which is located at the bottom. This window has a bunch of tabs at the bottom of the window. These tabs hold a lot of important information. For example, any compilation errors and warnings will be displayed in the “Error List”.


The last thing to discuss in the whirlwind tour is the tool bar at the top. Now, there is definitely a lot going on there, so let's just hit the high points. The most important one is the green triangle that looks like the play symbol. This starts up the application. (If you are into keyboard shortcuts, the keyboard shortcut is F5.) This will kick off the compilation process and upon successful compilation, it will start up the application in debug mode.

Now once the application starts up, the tool bars and windows will switch around. At first this can be a bit weird, but over time you will start to enjoy it because you have very different needs when you are writing and debugging code. This changing of the view really helps get the tools that you need at your fingertips for the job at hand.


When the application is running, a pause sign and a stop sign will appear on the tool bar. The stop sign will kill the application and get you back into writing code mode. The pause sign will stop the application and show you where in the code it currently is. This can be extremely helpful if something is running much longer than you expect it to and you want to get an idea of what is going on.


Ok, so that's the whirlwind tour. There is a lot more going on in there, but that should be enough to get going.


Hello, World

Now, to create our first application, take and place the code below between the <Grid ...> and </Grid> tags.

This piece of code displays the words “Hello, World!” in the middle of the screen.


To run the application, just click on the green play icon, and it should pop up a window with the words “Hello, World!” on it:


Nice! Now, to run it on the Raspberry Pi 2, click on the drop down next to the play button that says “x86” and switch that to “ARM”. That drop down specifies the processor type. Your PC has an x86 processor in it, whereas the RPi2 has an ARM processor in it. The good news is that other than this drop down, all of the differences are abstracted away from us as the user! So, we can go on programming without worrying about the underlying processor type. Next, click on the expansion triangle to the right of the play icon (just past the word “Device”), and click on the “Remote Machine” option. This will bring up a “Remote Connections” window. In there will be a section title “Auto Detected” and within that section should be your RPi2 (mine is called “minwinpc”). Click on that and then hit “Select”. Now when you hit play, the program will be running on the RPi2!



The full source code for this tutorial is also on GitHub: