The range of Tasks available in the Microsoft.Phone.Tasks namespace call into various system features from your application. In some circumstances these may not work as expected. The most obvious example is the Emulator where not all of the system is implemented. The behaviour will differ depending on the specific task. For example EmailComposeTask on the emulator will display a message that no accounts are setup and of course you cant create these yourself because this functionality is hidden in the emulator. The other situation which can cause tasks to fail is when your device is docked with the Zune software. As a developer there is a tool to get around this when debugging but you must write your software to be aware of this to avoid upsetting users. In Windows Mobile we would have used SystemState.CradlePresent to detect when the device was docked, there isn’t an equivalent property for Windows Phone. Instead you can check the network type. Because we know that the hardware configurations for Windows Phone 7 are quite specific we know that devices will have phone, wifi and USB cable connections. You can detect the network type using the Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType property. There are a lot of possible values in the NetworkInterfaceType enumeration but only a few are relevant:-
Ethernet – Used for Zune cable connection
MobileBroadbandGsm – For phone connections
Wireless80211 – For WiFi
So by checking for the value Ethernet we can tell if the device is currently docked. The only caveat is that this property isn’t set immediately when you dock and there can be a few seconds before the connection is established.
Case Study – PhotoChooserTask
The PhotoChooserTask works on the emulator with a selection of default images. On the phone it will fail if the phone is docked. The Completed event will be raised and a TaskResult of Cancel will be returned – as if the user had cancelled. This simple block of code can be used to wrap your call and handle this situation (for a change a little VB):-
If Not Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType = Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Ethernet Then
Dim pct As New Microsoft.Phone.Tasks.PhotoChooserTask
AddHandler pct.Completed, AddressOf pct_completed
MessageBox.Show("To view photos, disconnect your phone from the computer", "My Application", MessageBoxButton.OK)
|Task Name||Emulator||Phone (Docked)|
|CameraCaptureTask||Yes (simulated data)||No (Completed returns TaskResult.Cancel)|
|EmailComposeTask||Shows error message (no account)||Yes|
|PhotoChooserTask||Yes (Sample data)||No (Completed returns TaskResult.Cancel)|
The marketplace tasks perform no action and these ones don’t have a Completed event so you don’t get any feedback. You can provide similar logic to the above to warn the user that they can’t use Marketplace functionality when docked, you could also hide any marketplace links from your UI based on the network state.