Programming4us
         
 
 
Windows Phone

Writing Your First Phone Application - Adding Code (part 2)

11/25/2010 3:37:00 PM

Debugging with a Device

If you have a phone with which you want to do your development, you will want to be able to deploy and debug directly on the phone itself. First you need to connect your phone to your development machine. The communication with the phone is all routed through the Zune software. By connecting your device to your computer, you should also run Zune (if it fails to run automatically). While the device will continue to operate normally when connected to Zune, there are several functions which are disabled to allow for Zune to synchronize media (music, photos and videos) to the device. Consequently these functions are using your media on the phone. Once connected, the device looks like it would normally (as seen in Figure 4).

Figure 4. Connected Device (phone.jpg)


All the communication that you will do to your phone (e.g. debugging, deploying and registration) is all done while the Zune software is running. Once you’ve connected your phone to your computer and run the Zune software, you should be able to see the phone attached as shown in Figure 5.

Figure 5. Your Phone Connected to the Zune Software (ZuneWithPhone.bmp)


Once your device is connected, you can use it to sync your music, videos and photos to the phone. Though, before you can use a phone as a development device, you will need to register the phone for development. This lifts the requirements that applications that are signed by Microsoft. This allows you to deploy your applications directly to the phone so you can debug applications.

Before you can enable your phone as a developer phone, you will need to have an account at the Windows Phone developer portal[1]. Once you have done that, you can enable your phone to be used for development. To do this you will need the tool called “Windows Phone Developer Registration”. This tool is installed when you install the Windows Phone 7 SDK. When you run this application it will ask you for your Windows Live ID that you used to register with the developer portal as shown in Figure 6.

[1] http://developer.windowsphone.com

Figure 6. Registering your Device (DeveloperPhoneRegistration.bmp)


If your phone is successfully attached to your computer, the Status will tell you that it is ready to register your device for development. At this point, just hit the ‘Register’ button to register with the developer portal. Once it registers the phone, it changes the status to show you that the phone is ready (as shown in Figure 7).

Figure 7. Successfully Registered Developer Phone (DeveloperPhoneRegistrationComplete.bmp)


Once you’ve registered your device, you can deploy and debug your applications using Visual Studio. The key to using the device instead of the emulator is to change the deployment using the drop-down list of deployment options. There are only two:

  • Windows Phone 7 Emulator

  • Windows Phone 7 Device

The drop-down is located in the toolbar of Visual Studio as shown in Figure 8.

Figure 8. Changing the Deployment to use a Development Phone (DebuggingOnDevice.bmp)


Once you change the deployment target you can debug just like you did with the emulator. When you run the application, it will deploy your application to the device and run it so you can debug it in the same way as you did with the emulator. Figure 9 shows the application running on a device.

Figure 9. Running on a Device (DeviceDebugging.jpg)


Using Touch

Even though the touch interactions do, in fact, fire mouse events there are other events which allow you to design your application for touch. Since touch is so important to the way that applications on the phone work, this first application should give you a taste of that experience. To show touch working, let’s add an ellipse to the application that the user can move around by dragging it with their finger. To get started, you should open the MainPage.xaml file and add a new ellipse in the center of the page. To do this find the TextBlock called theStatus and place a new Ellipse element after it like so:

...
<Grid x:Name="ContentGrid"
Grid.Row="1">
<Rectangle Fill="#FF7E0505"
Margin="8"
RadiusY="24"
RadiusX="24" />
<TextBlock HorizontalAlignment="Center"
TextWrapping="Wrap"
Text="Status"
VerticalAlignment="Bottom"
FontSize="48"
FontWeight="Bold"
Name="theStatus" />
<Ellipse x:Name="theEllipse"
Fill="White"
Width="200"
Height="200">
</Ellipse>
</Grid>
...

We want to be able to move theEllipse as the user drags it. To allow us to do this we will need to use something called a Transform. In Silverlight, a Transform is used to change the way that an object is rendered without having to change properties of the Ellipse. While we could change the margins and/or alignments to move it around the screen, using a transform is much simpler. You should use a TranslateTransform to allow this movement. A TranslateTransform provides an X and Y property which specify where to draw the element (as a delta between where it originally exists and where you want it). You can specify this transform by setting the RenderTransform property with a TranslateTransform (naming it in the process):

...
<Ellipse x:Name="theEllipse"
Fill="White"
Width="200"
Height="200">
<Ellipse.RenderTransform>
<TranslateTransform x:Name="theMover" />
</Ellipse.RenderTransform>
</Ellipse>
...

Now that we have a way to move our Ellipse around the page, let’s look at dealing with touch. In Silverlight, there are two specific types of touch interactions that are meant to allow the user to change on-screen objects. These are when the user drags their finger on the screen and when they use a pinch move to resize objects. These types of interactions are called Manipulations. Silverlight has three events to allow you to use this touch information:

  • ManipulationStarted

  • ManipulationDelta

  • ManipulationCompleted

These events let you get information about the manipulation as it happens. For example, lets handle the ManipulationDelta event to get information about when the user drags on the screen. This event is called as the manipulation happens and included information about the difference between the start of the manipulation and the current state (e.g. how far as the user dragged their finger):

...
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();

theStatus.Text = "Hello from Code";

theStatus.MouseLeftButtonUp +=
new MouseButtonEventHandler(theStatus_MouseLeftButtonUp);

theEllipse.ManipulationDelta +=
new EventHandler<ManipulationDeltaEventArgs>(
theEllipse_ManipulationDelta);
}

void theEllipse_ManipulationDelta(object sender,
ManipulationDeltaEventArgs e)
{
// As a manipulation is executed (drag or resize), this is called
theMover.X = e.CumulativeManipulation.Translation.X;
theMover.Y = e.CumulativeManipulation.Translation.Y;
}

...
}
...

The event is fired while the user either pinches or drags within theEllipse.. In this case the code is only concerned with the dragging. In the event handler for ManipulationDelta, the ManipulationDeltaEventArgs object contains information about the extent of the manipulation. In the CumulativeManipulation property of the event args, there is a property called Translation which contains the extent of the drag operation (the complete delta). We are just changing theMover’s properties to match the manipulation. This means we can now drag theEllipse around and see it change position under our dragging as seen in Figure 10.
Figure 10. Dragging the Ellipse (DraggingEllipse.bmp)


Other -----------------
- Developing Applications for Windows Phone 7 : Designing with Blend
- Developing Applications for Windows Phone 7 : Creating a New Project
- Developing Applications for Windows Phone 7 with Silverlight : Preparing Your Machine
- Windows Phone 7 : Picking Ringtones and Alerts
- Windows Phone 7 : Changing Themes and Wallpaper
- Windows Phone 7 : Customizing the Start Screen
- Windows Phone 7 : Connecting to a Wi-Fi Hotspot
- Windows Phone 7 : Setting Up Facebook
- Windows Phone 7 : Setting Up E-Mail and Your Calendar
- Windows Phone 7 : Taking a Quick Tour (part 2)
- Windows Phone 7 : Taking a Quick Tour (part 1)
 
 
Most View
- Managing Windows Server 2012 Storage and File Systems : Storage Management (part 14) - Managing volumes on dynamic disks - Configuring RAID 5
- Developing for Windows Phone and Xbox Live : Sprites and 2D Graphics - Rendering Text
- Windows Server 2008 : Install the Windows Server Backup Server Feature
- Windows Phone 7 : Changing Themes and Wallpaper
- Windows 7 : Setting Security Permissions on Files and Folders (part 3) - Assigning a User to Multiple Security Groups
- Programming Windows Phone 7 : Sensors and Services
- Installing Configuration Manager 2007 : ConfigMgr Service Manager
- SQL Server 2008 : Indexing for Performance - Putting It All Together (part 3) - Covering Your Queries
- Windows 7 : Setting Security Permissions on Files and Folders (part 4) - Assigning Standard Permissions
- Exchange Server 2010 : Planning for Unified Messaging (part 1) - Unified Messaging Servers
Top 10
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 3) - Configuring Recipient Filtering
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 2)
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 1)
- Implementing Edge Services for an Exchange Server 2007 Environment : Installing and Configuring the Edge Transport Server Components
- What's New in SharePoint 2013 (part 7) - BCS
- What's New in SharePoint 2013 (part 6) - SEARCH
- What's New in SharePoint 2013 (part 6) - WEB CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 5) - ENTERPRISE CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 4) - WORKFLOWS
- What's New in SharePoint 2013 (part 3) - REMOTE EVENTS