暂停 PC

Suspending a PC

我是 C# 的新手,我正在尝试编写自己的 UWP 应用程序以在用户输入间隔后暂停 PC。

我已经设法让我的计时器开始工作,但我很难理解如何调用该方法来暂停计算机。我尝试使用建议的代码 here

Application.SetSuspendState(PowerState.Suspend, true, true);

但是当我尝试构建项目时出现以下错误:

CS0117 'Application' does not contain a definition for 'SetSuspendState' SystemTimer C:\Users\xxxxx\Source\Workspaces\Workspace\SystemTimer\SystemTimer\MainPage.xaml.cs 52

我在 PowerState 上找到了一个 MSDN 条目,它建议我使用 System.Windows.Form 库。我尝试将它添加到命名空间,但收到一条错误消息,指出 Form 不是 System.Windows.

中的可用命名空间

我觉得我可能遗漏了一些基本的东西,但我不完全确定它是什么。

您是否尝试过使用 Interop Services 实现此功能?

[DllImport("Powrprof.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern bool SetSuspendState(bool hiberate, bool forceCritical, bool disableWakeEvent);

// Hibernate
SetSuspendState(true, true, true);
// Standby
SetSuspendState(false, true, true);

I feel I am probably missing something fundamental but I am not entirely what it is.

(UWP) 应用程序的世界是一个不同的世界。应用程序不应该干扰(即混乱)其底层系统。你基本上 运行 在沙盒环境中,你只能得到 API 提供的内容。关闭或暂停系统不属于此 API。

你有什么选择?如果您创建一个桌面应用程序,您可能会过得更好。然后您将能够使用所有可用的桌面 API,包括 System.Windows.Form