5. Understanding Idle Detection
The Windows Phone operating system
automatically detects when a user has stopped using the phone and locks
it. The phone will go to a lock screen, which can either display a pass
code to open the lock screen or just instruct the user to slide up the
wallpaper screen to get back to the phone.
Sometimes when you’re building certain types of
applications, you want your application to continue to run regardless of
whether the phone has input. There are two types of idle detection
modes: application and user.
The easier of these to understand is the user idle detection mode. On the PhoneApplicationService
class is a property called UserIdleDetectionMode
;
it is enabled by default. This means that if no touch events are
detected, it will let the phone go to the lock screen. You can change
this behavior (to enable your application to continue to run as the main
running application even if the user isn’t interacting with your
application) by disabling this detection mode, like so:
// Allow application to stay in the foreground
// even if the user isn't interacting with the application
PhoneApplicationService.Current.UserIdleDetectionMode =
IdleDetectionMode.Disabled;
Disabling the user idle
detection mode is useful when you are doing things that engage the user
but do not require input (for example, showing a video, displaying a
clock, and so on).
In contrast, the application idle detection mode
is not about preventing the lock screen but determining what the
application should do when the lock screen is enabled. By default, the
application idle detection mode is enabled, which means that when the
lock screen appears the application is paused (as though it was being
tombstoned). By disabling the application idle detection mode, you allow
your application to continue to run under the lock screen. To disable
the application idle detection mode, you also use the PhoneApplicationService
class:
// Allow your application to continue to run
// when the lock screen is shown
PhoneApplicationService.Current.ApplicationIdleDetectionMode =
IdleDetectionMode.Disabled;
6. The Tilt Effect
In many places on the phone, a subtle but
effective feedback mechanism is employed when selecting items in a list
or other controls. This is called the tilt effect. In Figure 5, you can see that the second item in the list is not being interacted with via touch; however, in Figure 6
you can see that the second item is subtly tilted to give the user
feedback that she is touching the item. In fact, it’s hard to see here
in static images, but the tilt actually interacts with where the user is
touching the item.
FIGURE 5 Untilted
FIGURE 6 Tilted
Unfortunately, this effect
is not built into the framework, but it is available in the Windows
Phone Toolkit. When you include the Toolkit’s XML namespace declaration
in your XAML file, you can specify it at any level to enable or disable
this behavior. The namespace required is the same one that is used to
include Windows Phone Toolkit controls . Typically you would include it at the page level to support the tilt effect in all controls, like so:
<phone:PhoneApplicationPage ...
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;
assembly=Microsoft.Phone.Controls.Toolkit"
toolkit:TiltEffect.IsTiltEnabled="True">
The TiltEffect.IsTiltEnabled
property can be applied to any individual control as well if you want to
apply the effect to specific controls instead of at the page or
container levels. The other attached property is TiltEffect.SuppressTilt
. This attached property enables you to turn off the tilt effect on particular controls where the TiltEffect.IsTiltEnabled
attached property is enabled at a higher level. For example:
<Grid x:Name="ContentPanel"
Grid.Row="1"
Margin="12,0,12,0"
toolkit:TiltEffect.IsTiltEnabled="False">
<ListBox FontSize="28"
Name="listBox1"
toolkit:TiltEffect.SuppressTilt="True"
ItemsSource="{Binding}" />
</Grid>
Because these are attached properties, you can set them in code as well:
using Microsoft.Phone.Controls;
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
listBox1.SetValue(TiltEffect.SuppressTiltProperty, false);
}
}