Search code examples
c#windows-phone-8timelockscreen

Stopping Screen Timingout - windows phone 8.1


I am Creating a Windows Phone 8.1 that display the time, Quite basic but one thing i don't want it to do is allow the phone to look. Now sadly ApplicationIdleDetectionMode does not working anymore unless you do windows phone 8.1 Silverlight application. So i have used.

var displayRequest = new Windows.System.Display.DisplayRequest();
displayRequest.RequestActive(); 

now this works on its own and stops the phone going into the lock screen.

But my application does not work it goes it times out.

public MainPage()
    {
        var displayRequest = new Windows.System.Display.DisplayRequest();
        displayRequest.RequestActive(); 

        this.InitializeComponent();

        this.NavigationCacheMode = NavigationCacheMode.Required;



        this.NavigationCacheMode = NavigationCacheMode.Required;
        DateTime datetime = DateTime.Now;
        string time = String.Format("{0:T}", datetime);
        timeTXTblock.Text = time;

        DispatcherTimer dt = new DispatcherTimer();
        dt.Interval = TimeSpan.FromMilliseconds(1000);

        dt.Tick += (s, e) => { updateTime(); };

        dt.Start();
    }

    public void updateTime()
    {
        DateTime datetime = DateTime.Now;
        string time = String.Format("{0:T}", datetime);
        timeTXTblock.Text = time;
        return;
    }

I feel it might have to do with the tick on why it might be causing it to fail. Any idea why this might be and any solution?


Solution

  • Your DisplayRequest is going out of scope as it is only active in the constructor. Define it within the class:

    DisplayRequest displayRequest;
    
    public MainPage()
        {
            displayRequest = new Windows.System.Display.DisplayRequest();
            displayRequest.RequestActive(); 
    ...