DisplayRequest 类

定义

表示显示请求。

public ref class DisplayRequest sealed
/// [Windows.Foundation.Metadata.Activatable(65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.None)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.STA)]
class DisplayRequest final
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.None)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.STA)]
/// [Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
class DisplayRequest final
[Windows.Foundation.Metadata.Activatable(65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.None)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.STA)]
public sealed class DisplayRequest
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.None)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.STA)]
[Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
public sealed class DisplayRequest
function DisplayRequest()
Public NotInheritable Class DisplayRequest
继承
Object Platform::Object IInspectable DisplayRequest
属性

Windows 要求

设备系列
Windows 10 (在 10.0.10240.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v1.0 中引入)

示例

以下代码 (取自 显示电源状态示例) 演示如何激活、跟踪和释放显示请求。

/// <param name="sender"></param> 
/// <param name="e"></param> 
private void Activate_Click(object sender, RoutedEventArgs e)
{
    Error.Text = string.Empty;
    Button b = sender as Button;
    if (b != null)
    {
        try
        {
            if (g_DisplayRequest == null)
            {
                // This call creates an instance of the displayRequest object 
                g_DisplayRequest = new DisplayRequest();
            }
        }
        catch (Exception ex)
        {
            rootPage.NotifyUser("Error Creating Display Request: " + ex.Message, NotifyType.ErrorMessage);
        }

        if (g_DisplayRequest != null)
        {
            try
            {
                // This call activates a display-required request. If successful,  
                // the screen is guaranteed not to turn off automatically due to user inactivity. 
                g_DisplayRequest.RequestActive();
                drCount += 1;
                rootPage.NotifyUser("Display request activated (" + drCount + ")", NotifyType.StatusMessage);
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser("Error: " + ex.Message, NotifyType.ErrorMessage);
            }
        }
    }
}

/// <param name="sender"></param> 
/// <param name="e"></param> 
private void Release_Click(object sender, RoutedEventArgs e)
{
    Error.Text = string.Empty;
    Button b = sender as Button;
    if (b != null)
    {
        if (g_DisplayRequest != null)
        {
            try
            {
                // This call de-activates the display-required request. If successful, the screen 
                // might be turned off automatically due to a user inactivity, depending on the 
                // power policy settings of the system. The requestRelease method throws an exception  
                // if it is called before a successful requestActive call on this object. 
                g_DisplayRequest.RequestRelease();
                drCount -= 1;
                rootPage.NotifyUser("Display request released (" + drCount + ")", NotifyType.StatusMessage);
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser("Error: " + ex.Message, NotifyType.ErrorMessage);
            }
        }
    }
}
''' <param name="sender"></param> 
''' <param name="e"></param> 
Private Sub Activate_Click(sender As Object, e As RoutedEventArgs) 
    ErrorTestBlock.Text = String.Empty 
    Dim b As Button = TryCast(sender, Button) 
    If b IsNot Nothing Then 
        Try 
            If g_DisplayRequest Is Nothing Then 
                ' This call creates an instance of the displayRequest object 
                g_DisplayRequest = New DisplayRequest() 
            End If 
        Catch ex As Exception 
            rootPage.NotifyUser("Error Creating Display Request: " & ex.Message, NotifyType.ErrorMessage) 
        End Try 

        If g_DisplayRequest IsNot Nothing Then 
            Try 
                ' This call activates a display-required request. If successful,  
                ' the screen is guaranteed not to turn off automatically due to user inactivity. 
                g_DisplayRequest.RequestActive() 
                drCount += 1 
                rootPage.NotifyUser("Display request activated (" & drCount & ")", NotifyType.StatusMessage) 
            Catch ex As Exception 
                rootPage.NotifyUser("Error:" & ex.Message, NotifyType.ErrorMessage) 
            End Try 
        End If 
    End If 
End Sub

''' <param name="sender"></param> 
''' <param name="e"></param> 
Private Sub Release_Click(sender As Object, e As RoutedEventArgs) 
    ErrorTestBlock.Text = String.Empty 
    Dim b As Button = TryCast(sender, Button) 
    If b IsNot Nothing Then 
        If g_DisplayRequest IsNot Nothing Then 
            Try 
                ' This call de-activates the display-required request. If successful, the screen 
                ' might be turned off automatically due to a user inactivity, depending on the 
                ' power policy settings of the system. The requestRelease method throws an exception  
                ' if it is called before a successful requestActive call on this object. 
                g_DisplayRequest.RequestRelease() 
                drCount -= 1 
                rootPage.NotifyUser("Display request released (" & drCount & ")", NotifyType.StatusMessage) 
            Catch ex As Exception 
                rootPage.NotifyUser("Error: " & ex.Message, NotifyType.ErrorMessage) 
            End Try 
        End If 
    End If 
End Sub 

注解

为了节省电源并延长电池使用时间,如果系统在一定时间内未检测到任何用户活动,则会减少计算机的电源。 根据系统电源设置,显示器可能首先变暗,可能会显示屏幕保护程序,最终在系统进入低功耗睡眠状态时关闭显示器。

在没有用户输入的情况下显示视频或长时间运行的应用可以通过调用 DisplayRequest.RequestActive 来请求保持显示状态。 激活显示请求后,当应用可见时,设备的显示器将保持打开状态。 当用户将应用移出前台时,系统会停用应用的显示请求,并在应用返回到前台时重新激活这些请求。

显示请求是累积的 - 每个显示请求都必须通过对 DisplayRequest.RequestRelease 的单独调用来释放。 当应用不再需要显示时,应用应跟踪活动显示请求的数量,并确保每个请求都释放 (相应的 DisplayRequest.RequestRelease 调用) 。 有关详细信息,请参阅:

使用显示请求使显示器保持打开状态会消耗大量电量。 使用显示请求时,请遵循这些准则来获得最佳应用行为。

  • 仅在需要时才使用显示请求,即不需要用户输入但显示应保持打开状态时。 例如,在全屏演示期间或用户正在阅读电子书时。
  • 不再需要每个显示请求后立即释放该请求。

注意

此类不是敏捷类,这意味着需要考虑其线程模型和封送处理行为。 有关详细信息,请参阅线程处理和封送处理 (C++/CX) 在多线程环境中使用 Windows 运行时 对象 (.NET)

Windows Phone 8

此 API 仅在本机应用中受支持。

构造函数

DisplayRequest()

创建 DisplayRequest 类的实例。

方法

RequestActive()

激活显示请求。

RequestRelease()

停用显示请求。

适用于