IdleDetectionMode error Windows phone 7
I have a checkbox. On it's checked event I want to turn off IdleDetectionMode and on unchecked event I want to turn on. This is the code :-
private void chkRunInBackground_Checked(object sender, RoutedEventArgs e)
{
PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled;
}
private void chkRunInBackground_Unchecked(object sender, RoutedEventArgs e)
{
PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Enabled;
开发者_运维技巧 }
The checked event runs fine but on unchecked event I get, IdleDetection mode cannot be started once it is disabled. Why is this restriction applied and what can I do to work around it?
From MSDN:
In the current release, application idle detection cannot be enabled in a single application instance once it has been disabled. Doing so throws an exception. In future releases this may be supported, so your application may choose to disable application idle detection when it is no longer needed and catch the expected exception.
The following code snippet shows an implementation of this.
// Custom function to turn off idle detection. This will throw an exception in the current release.
public void TryReenableApplicationIdleDetection()
{
bool didEnable = false;
try
{
Microsoft.Phone.Shell.PhoneApplicationService.Current.ApplicationIdleDetectionMode =
Microsoft.Phone.Shell.IdleDetectionMode.Enabled;
didEnable = true;
}
catch (InvalidOperationException ex)
{
// This exception is expected in the current release.
}
// possibly use the value of didEnable to decide what to do next
// if it is 'true' then your app will be deactivated
// if it is 'false' then your app will keep running
}
This is by design. As per MSDN:
In the current release, application idle detection cannot be enabled in a single application instance once it has been disabled. Doing so throws an exception.
Basically, the application defines it's characteristics that will determine the system behavior and "attitude" towards it. It is a bad practice to try and change those while the application is running.
精彩评论