【发布时间】:2018-05-17 07:41:55
【问题描述】:
我希望我的应用在用户登录 Windows 时启动。目前,我可以通过打开任务管理器 > 启动 > 并为我的应用设置“启用”来做到这一点。不过,我想在我的应用程序中执行此操作,并关注this guide。但是,当请求应用程序在启动时运行时,它总是会抛出以下异常:
The group or resource is not in the correct state to perform the requested operation.
这对我来说没有意义,因为如果StartupTaskState 是Disabled,我的应用程序只会调用RequestEnableAsync(),如下面的代码所示:
private async Task<bool> SetLaunchOnLogin_UWP_Async(bool shouldLaunchOnLogin)
{
try
{
var startupTask = await StartupTask.GetAsync("MyProjectStartupId");
switch (startupTask.State)
{
case StartupTaskState.Disabled:
Debug.WriteLine("Startup is disabled. Will ask");
// The code reaches here, but always throws an exception
// when calling RequestEnableAsync():
var newState = await startupTask.RequestEnableAsync()
return newState == StartupTaskState.Enabled;
}
}
catch (Exception ex)
{
Debug.WriteLine(
"SpecificPlatformFunctions_UWP - SetLaunchOnLogin_UWP_Async ERROR: "
+ ex.ToString());
}
return false;
}
以及完整的错误输出:
Startup is disabled. Will ask
Exception thrown: 'System.Exception' in System.Private.CoreLib.ni.dll
SpecificPlatformFunctions_UWP - SetLaunchOnLogin_UWP_Async ERROR: System.Exception: The group or resource is not in the correct state to perform the requested operation. (Exception from HRESULT: 0x8007139F)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at MyProject.UWP.DependencyServices.SpecificPlatformFunctions_UWP.<SetLaunchOnLogin_UWP_Async>d__13.MoveNext()
当单击按钮时,上面的代码通过DependencyService 调用。
【问题讨论】:
标签: c# xamarin xamarin.forms uwp