1. The Clipboard API
Windows Phone supports a common clipboard much like Windows. Users can add items to the clipboard while working with TextBox
controls, and the copied text will remain in the clipboard for other
applications to use. You can also access the clipboard programmatically
with the Clipboard
class. The Clipboard
class has three simple static methods: SetText
, ContainsText
, and GetText
. You can set text to the clipboard programmatically by using the SetText
method, like so:
Clipboard.SetText("Hello World");
You can test to see whether there is data in the clipboard by calling the ContainsText
method as well:
if (Clipboard.ContainsText())
{
// ...
}
The last method (GetText
) exists on the Clipboard
class but is not available to Windows Phone applications. Calling GetText
will throw a security exception.
This means your application cannot read the clipboard; it can only test
to see whether it has text and then push text onto the clipboard. The
reasoning behind this is to prevent accidental leaking of user data to
unauthorized applications. For desktop Silverlight, user approval
typically is required to access the clipboard, but currently the
decision is to just disallow it instead of introducing another pop-up
confirmation that needs to be explained to the user. This is a
limitation of the phone SDK, and you will need to work around it if you
need to copy text from the clipboard.
2. Location APIs
Your application can determine where in the
world the phone is at any moment. This is the same technology your GPS
device uses to determine how to give you driving directions. The phones
are required to have Assisted Global Positioning System (A-GPS)
hardware. This is different from simple GPS location technology as the
“Assisted” part is very important. Typical GPS devices rely on being
able to locate three orbiting satellites to triangulate your location.
Sometimes these systems were hurt by the lag in “syncing” with the
three satellites and often structures (such as buildings or bridges)
made GPS unreliable. Assisted GPS improves this by using GPS to give
you high-precision geolocation when possible but can also fall back to
use other ways of determining your location, including cell-phone tower
triangulation and the Wi-Fi Positioning System.
Location Permission
To create a location-aware application, you
will use the A-GPS on the phone. Because using location without the
user’s consent would likely be a violation of privacy, your application
has to specify that it can use the location APIs in the SDK. To do
this, you need to add a new capability via the WMAppManifest.xml
file. If you open the manifest in the editor, you can check off the location capability in the Capabilities tab, as shown in Figure 1.
FIGURE 1 Requesting the location capability
When your application is
installed, the Marketplace specifically asks the user whether he wants
to allow your application to gather location information. Before your
application can use location information, it must use the GeoLocationWatcher
class (in the System.Device.Location
namespace) to check whether the user has granted permission:
// using System.Device.Location;
// Create the Watcher (and clean it up when done)
using (var watcher = new GeoCoordinateWatcher())
{
if (watcher.Permission == GeoPositionPermission.Granted)
{
// Find the location
}
}
The Permission
property of the GeoCoordinateWatcher
class will tell your application whether it is allowed to use location information. If permission is
denied and you attempt to use location information, the class will
throw an exception. In addition to checking for permission, you must
allow the user to disable this capability in your application.