我想允许我的 UWP 应用程序启动关闭。
尝试调用像 Powershell 这样的 shell 命令
Stop-Computer
不起作用。
因此,我尝试使用本机 C# 代码。但是,这样做会导致下面提到的错误:
ShutdownManager.BeginShutdown(Windows.System.ShutdownKind.Shutdown, TimeSpan.FromSeconds(120));
MainPage.xaml.cs
using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using System; using Windows.System; // The Blank Page item template is documented at https://learn.microsoft.com/en-us/previous-versions/windows/apps/hh768232(v=win.10)?redirectedfrom=MSDN#project-templates-at-a-glance. // Delete the extra namespaces when the application is complete. namespace Shutdown_Roulette { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } private void Button_click(object sender, RoutedEventArgs e) { ShutdownManager.BeginShutdown(Windows.System.ShutdownKind.Shutdown, TimeSpan.FromSeconds(120)); } } }
Package.appxmanifest
<?xml version="1.0" encoding="utf-8"?> <Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:iot="http://schemas.microsoft.com/appx/manifest/iot/windows10" IgnorableNamespaces="uap mp iot"> <Identity Name="7eb73f1e-b159-4fd0-aab9-4158e57ba08a" Publisher="CN=rokeb" Version="1.0.0.0" /> <mp:PhoneIdentity PhoneProductId="7eb73f1e-b159-4fd0-aab9-4158e57ba08a" PhonePublisherId="00000000-0000-0000-0000-000000000000"/> <Properties> <DisplayName>Shutdown Roulette</DisplayName> <PublisherDisplayName>Master Roke Julian Lockhart Beedell</PublisherDisplayName> <Logo>Assets\StoreLogo.png</Logo> </Properties> <Dependencies> <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" /> </Dependencies> <Resources> <Resource Language="x-generate"/> </Resources> <Applications> <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="Shutdown_Roulette.App"> <uap:VisualElements DisplayName="Shutdown Roulette" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="Shutdown Roulette" BackgroundColor="transparent"> <uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png" ShortName="Shutdown Roulette"> </uap:DefaultTile > <uap:SplashScreen Image="Assets\SplashScreen.png" /> </uap:VisualElements> </Application> </Applications> <Capabilities> <iot:Capability Name="systemManagement"/></Capabilities> </Package>
请注意,我想在 Windows 10 上使用此命令,而不是 Windows 10 IoT;我只是尝试利用 IoT 的命令,因为它们是调用此命令的唯一替代方法(我已经能够想到),而不依赖于 PowerShell、Python 或 Windows 命令处理器。
根据ShowdownManager的描述,它需要IOT SDK的支持,这意味着它是一个平台限制的API(仅在IOT设备上有效)。
在 Windows 10 桌面设备中,UWP 没有关闭设备的权限。