Disabling the lock screen

You have almost created the final version of the Space Aim 3D game that can be submitted to the store. However, let's run the application, navigate to the Game screen, and do not click on anything on the mobile phone for some time. As you could see, the lock screen is engaged and the game is stopped. It is not a convenient situation, because if the player just steers the game with an accelerometer (without touching the screen), after some time will need to unlock the phone to continue playing.

Implementation

To fix the problem of engaging the lock screen while playing, you can temporarily disable the user idle detection mode. However, it is important to enable it again as soon as possible, because then the system can limit consumption of the battery power while the player is not using the mobile phone.

In the exemplary game, you will disable the mechanism when the player opens the Game screen and enable it again while navigating back to the Menu page.

GameViewModel.cs

Let's start by modifying the view model for the Game screen. Here, you add a private field which is an instance of the PhoneApplicationService class from the Microsoft.Phone.Shell namespace. You assign to it a value of the static Current property, thus it represents settings regarding your application:

private PhoneApplicationService m_phoneApplicationService = PhoneApplicationService.Current;

When the user navigates to the Game screen, you will disable the user idle detection mechanism. To do so, you can simply assign a suitable value of the enumeration type to the UserIdleDetectionMode property, as shown as follows:

public void OnNavigatedTo(NavigationEventArgs e)
{
  this.m_phoneApplicationService.UserIdleDetectionMode = 
    IdleDetectionMode.Disabled;
}

Whenever the user navigates back to the Menu screen or the application state is changed to Dormant, you should enable the user idle detection mode again:

public void OnNavigatedFrom(NavigationEventArgs e)
{
  this.m_phoneApplicationService.UserIdleDetectionMode = IdleDetectionMode.Enabled;
}

GamePage.xaml.cs

At the end, you need to override both the OnNavigatedTo and OnNavigatedFrom methods inside the code-behind file and call suitable methods (OnNavigatedTo and OnNavigatedFrom from the view model class) inside them. Thus, you are able to move the logic related to enabling and disabling the user idle detection mode from the view to the view model.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.197.213