UWP:在全屏模式下如何防止屏幕锁定?

时间:2018-10-13 19:37:34

标签: uwp

我熟悉许多文档,其中讨论了如何在UWP应用程序处于活动状态时(例如this article)防止屏幕变黑。它非常适合我的应用程序,除非我使用ApplicationView.TryEnterFullScreenMode方法将应用程序置于全屏模式。当应用全屏显示时,在闲置了很多分钟后(取决于Windows设置),屏幕将变为空白。当应用程序处于全屏模式时,如何防止屏幕锁定/变黑?

2 个答案:

答案 0 :(得分:0)

我已经在全屏模式下测试了RequestActive,它也很好用。为了确保您已调用RequestActive方法,您可以定义布尔_isRequest来维持RequestActive()的调用。

private bool _isRequest;
public void ActivateDisplay()
{
    //create the request instance if needed
    if (_displayRequest == null)
        _displayRequest = new Windows.System.Display.DisplayRequest();

    //make request to put in active state
    _displayRequest.RequestActive();
    _isRequest = true;
    info.Text = _isRequest.ToString();
}

public void ReleaseDisplay()
{
    //must be same instance, so quit if it doesn't exist
    if (_displayRequest != null && _isRequest)
    {
        _displayRequest.RequestRelease();
        _isRequest = false;
        info.Text = _isRequest.ToString();
    }
    else return;
}

答案 1 :(得分:0)

经过进一步研究,这是我发现的东西:

如果应用程序已经调用了DisplayRequest.RequestActive,然后然后进入全屏模式(调用ApplicationView.TryEnterFullScreenMode),则它将无法正常工作。 (换句话说,该应用程序不会阻止睡眠的发生。)

如果应用程序已经调用DisplayRequest.RequestActive,然后进入全屏模式,然后尝试再次调用DisplayRequest.RequestActive,则它 still 无效。

但是,如果应用程序已经调用了DisplayRequest.RequestActive,并且当它想要进入全屏模式时,请先调用DisplayRequest.RequestRelease,然后再调用ApplicationView.TryEnterFullScreenMode,然后在进入后调用DisplayRequest.RequestActive全屏模式,就可以了。

退出全屏模式时也是如此。

因此,对Nico的代码所做的更改将起作用:

private bool _isRequest;
public void ActivateDisplay()
{
    //create the request instance if needed
    if (_displayRequest == null)
        _displayRequest = new Windows.System.Display.DisplayRequest();

    //make request to put in active state
    _displayRequest.RequestActive();
    _isRequest = true;
    info.Text = _isRequest.ToString();
}

public void ReleaseDisplay()
{
    //must be same instance, so quit if it doesn't exist
    if (_displayRequest != null && _isRequest)
    {
        _displayRequest.RequestRelease();
        _isRequest = false;
        info.Text = _isRequest.ToString();
    }
    else return;
}

public void EnterFullScreen()
{
    ApplicationView applicationView = ApplicationView.GetForCurrentView();
    applicationView.FullScreenSystemOverlayMode = FullScreenSystemOverlayMode.Minimal;
    bool wasRequest = _isRequest;
    if (wasRequest)
    {
        ReleaseDisplay();
    }
    applicationView.TryEnterFullScreenMode();
    if (wasRequest)
    {
        ActivateDisplay();
    }
}

public void ExitFullScreen()
{
    ApplicationView applicationView = ApplicationView.GetForCurrentView();
    applicationView.FullScreenSystemOverlayMode = FullScreenSystemOverlayMode.Standard;
    bool wasRequest = _isRequest;
    if (wasRequest)
    {
        ReleaseDisplay();
    }
    applicationView.ExitFullScreenMode();
    if (wasRequest)
    {
        ActivateDisplay();
    }
}