So now that I have my device up and running, time to start coding. So I’ll be writing my first application.
Objectives
I could start coding a complicated app using the GPIO or talking with advanced devices using I2C. But I just wanted to see what was possible on the device first.
The objective of this first application are:
- Creating a MINIMAL basic UI based application.
- See if it could be deployed to the device.
- See my application run on the Raspberry Pi.
- Start a debugging session, place break points and watch variable values.
Getting Started
Creating a Universal Windows Project
In Visual Studio 2015, I clicked File | New Project….
Coding
Once the project was loaded, I opened up the main page MainPage.xaml. I created a basic layout, added a TextBlock, a TextBox and a Button. Here is the code:
XAML
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="51"/> <RowDefinition Height="51"/> <RowDefinition Height="51"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBlock x:Name="textBlock" Grid.Row="1" TextWrapping="Wrap" Text="Hello World!" HorizontalAlignment="Center"/> <TextBox x:Name="textBox" Grid.Row="2" TextWrapping="Wrap" Text="" HorizontalAlignment="Center" Margin="5" Width="150"/> <Button x:Name="Greet" Content="Button" Grid.Row="3" HorizontalAlignment="Center" Click="Greet_Click"/> </Grid>
I double clicked on the button to write an event handler. And typed in the following code:
C#
private void Greet_Click(object sender, RoutedEventArgs e) { string greet = "Hello World!"; if(!string.IsNullOrEmpty(textBox.Text)) { greet = string.Format("Hello {0}!", textBox.Text); } textBlock.Text = greet; }
Testing the application
To make sure the application was working as expected I started it from my local machine.
As expected, I got a new window with the following in it:
I typed in my name, clicked the button and got:
Fantastic! The app works! So I closed the window to stop the debug process before I continue.
Starting on the Raspberry Pi
Now that I have a working app, it’s time to try to run it on the device. To do this:
- Change set your debug target to Remote Machine
- Specify the machine address
- Change your build target to ARM
- Click Remote Machine to start the debugging session
Once this is done a number of things will happen:
- The application will be built.
- If this is the first time, Visual Studio will check some of the requirements on the remote device and install missing packages.
- Visual Studio will deploy the application to the remote device
- Visual Studio will start the remote debugger for the application on the device
- Visual Studio start the application.
This is the build output for this process:
Output Window (Build)
1>------ Build started: Project: HelloWorld, Configuration: Debug ARM ------ 1> HelloWorld -> C:\prog\IoT\HelloWorld\HelloWorld\bin\ARM\Debug\HelloWorld.exe 2>------ Deploy started: Project: HelloWorld, Configuration: Debug ARM ------ 2>Creating a new clean layout... 2>Copying files: Total 16 mb to layout... 2>Checking whether required frameworks are installed... 2>Framework: Microsoft.NET.CoreRuntime.1.0/ARM, app package version 1.0.23819.0 is not currently installed. 2>Framework: Microsoft.VCLibs.140.00.Debug/ARM, app package version 14.0.23918.0 is not currently installed. 2>Installing missing frameworks... 2>Registering the application to run from layout... 2>Deployment complete (0:01:52.439). Full package name: "ad63be4b-92ac-472f-8c68-f4537d5b5ce9_1.0.0.0_arm__7j55shkp2zhzg" ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ========== ========== Deploy: 1 succeeded, 0 failed, 0 skipped ==========
After that the application will start on the Raspberry Pi.
Debugging
So I first tried the application on the Raspberry Pi. I typed in my name, clicked “Button” and got the expected result.
Now going back to my development machine, I place a break point in the button click event handler.
On the device I type Test in the TextBox and click “Button”.
As expected, on development machine, Visual Studio breaks on the break point. I press F10 to step through and I am debugging.
When I look at the watch I can see all the variables value:
I press F5 to resume and the application continues to run as normal.
To stop the application, you need to stop the debugging session in Visual Studio.
Conclusion
It works! I can create a XML based application and run it on my Raspberry Pi.
One thing I have noticed is that the application on the Raspberry Pi is Modal. You cannot stop the application or get out of it. This will be useful in the future.