- Preparing Your Machine
- Creating a New Project
- Designing with Blend
- Adding Code
- Working with the Phone
- Where Are We?
Creating a New Project
To begin creating your first Windows Phone application you will want to start in one of two tools: Visual Studio or Expression Blend. Visual Studio is where most developers start their projects, so we will begin there, but we will also discuss how you can use both applications for different parts of the development process.
Visual Studio
As noted earlier, when you install the Windows Phone SDK 7.1 you get a version of Visual Studio 2010 Express that is used to create Windows Phone applications only. When you launch Visual Studio 2010 Express you will see the main window of the application, as shown in Figure 2.1.
Figure 2.1 Microsoft Visual Studio 2010 Express for Windows Phone
Click the New Project icon on the Start page and you will be prompted to start a new project. Visual Studio 2010 Express only supports creating applications for Window Phone. In the New Project dialog (see Figure 2.2) you will notice that only Silverlight and XNA project templates are shown. For our first project we will start with a new Windows Phone Application template and name it "HelloWorldPhone".
Figure 2.2 New Project dialog
When you click the OK button to create the project, Visual Studio will prompt you with a dialog where you can pick what version of the phone to target (version 7.0 or 7.1), as seen in Figure 2.3.
Figure 2.3 Picking the phone version to target
Once Visual Studio creates the new project, you can take a quick tour of the user interface (as shown in Figure 2.4). By default, Visual Studio shows two main panes for creating your application. The first pane (labeled #1 in the figure) is the main editor surface for your application. In this pane, every edited file will appear separated with tabs as shown. By default, the MainPage.xaml file is shown when you create a new Windows Phone application; this is the main design document for your new application. The second pane (#2 in the figure) is the Solution Explorer pane and it displays the contents of the new project.
Figure 2.4 The Visual Studio user interface
Another common pane that you will use is the toolbar, and it is collapsed when you first use Visual Studio 2010 Express for Windows Phone. On the left side of the main window you will see a Toolbox tab that you can click to display the Toolbox, as shown in Figure 2.5.
Figure 2.5 Enabling the toolbar
You may also want to click the pin icon to keep the toolbar shown at all times (as highlighted in Figure 2.5).
Before we look at how to create the application into something that is actually useful, let's see the application working in the device. You will notice that in the toolbar (not the Toolbox) of Visual Studio there is a bar for debugging. On that toolbar is a drop-down box for specifying what to do to debug your application. This drop down should already display the words "Windows Phone Emulator" as that is the default when the tools are installed (as shown in Figure 2.6).
Figure 2.6 Using the emulator
At this point, if you press the F5 key (or click the triangular play button on the debugging toolbar), Visual Studio will build the application and start the emulator with our new application, as shown in Figure 2.7.
Figure 2.7 The emulator
This emulator will be the primary way you will debug your applications while developing applications for Windows Phone. Our application does not do anything, so you can go back to Visual Studio and click the square stop button on the debugging toolbar (or press Shift-F5) to end your debugging session. You should note that the emulator does not shut down. It is meant to stay running between debugging sessions.
XAML
In Silverlight, development is really split between the design and the code. The design is accomplished using a markup language called XAML (rhymes with camel). XAML (or eXtensible Application Markup Language) is an XML-based language for representing the look and feel of your applications. Since XAML is XML-based, the design consists of a hierarchy of elements that describe the design. At its most basic level, XAML can be used to represent the objects that describe the look and feel of an application.1 These objects are represented by XML elements, like so:
<Rectangle /> <!-- or --> <TextBox />
You can modify these XML elements by setting attributes to change the objects:
<Rectangle Fill="Blue" /> <!-- or --> <TextBox Text="Hello World" />
Containers in XAML use XML nesting to imply ownership (a parent-child relationship):
<Grid> <Rectangle Fill="Blue" /> <TextBox Text="Hello World" /> </Grid>
Using this simple XML-based syntax you can create complex, compelling designs for your phone applications. With this knowledge in hand, we can make subtle changes to the XAML supplied to us from the template. We could modify the XAML directly, but instead we will start by using the Visual Studio designer for the phone. In the main editor pane of Visual Studio the MainPage.xaml file is split between the designer and the text editor for the XAML. The left pane of the MainPage.xaml file is not just a preview but a fully usable editor. For example, if you click on the area containing the words "page name" on the design surface, it will select that element in the XAML, as shown in Figure 2.8.
Figure 2.8 Using the Visual Studio XAML design surface
Once you have that element selected in the designer, the properties for the element are shown in the Properties window (which shows up below the Solution Explorer). If the window is not visible, you can enable it in the View menu by selecting "Properties window" or by pressing the F4 key. This window is shown in Figure 2.9.
Figure 2.9 Location of the Properties window
The Properties window consists of a number of small parts containing a variety of information, as shown in Figure 2.10.
Figure 2.10 Contents of the Properties window
The section near the top (#1 in Figure 2.10) shows the type of object you have selected (in this example, a TextBlock) and the name of the object, if any (here, "PageTitle"). This should help you ensure that you have selected the right object to edit its properties. The next section down (#2) contains a Search bar where you can search for properties by name, as well as buttons for sorting and grouping the properties. The third section (#3) is a list of the properties that you can edit.
From the Properties window you can change the properties of the selected item. For example, to change the text that is in the TextBlock, you can simply type in a new value for the Text property. If you enter "hello world" in the Text property and press Return, the designer will change to display the new value. Changing this property actually changes the XAML in the MainPage.xaml file. The design surface is simply reacting to the change in the XAML. If you look at the XAML the change has been affected there as well, as shown in Figure 2.11.
Figure 2.11 The changed property
You can edit the XAML directly as well if you prefer. If you click on the TextBlock above the PageTitle (the one labeled "ApplicationTitle"), you can edit the Text attribute directly. Try changing it to "MY FIRST WP7 APP" to see how it affects the designer and the Properties window:
... <TextBlock x:Name="ApplicationTitle" Text="MY FIRST WP7 APP" Style="{StaticResource PhoneTextNormalStyle}" /> ...
Depending on their comfort level, some developers will find it easier to use the Properties window while others will be more at ease editing the XAML directly. There is no wrong way to do this.
Although the Visual Studio XAML designer can create interesting designs, the real powerhouse tool for designers and developers is Blend. Let's use it to edit our design into something useful for our users.