PMA*_*RSH 6 .net windows-services async-await windows-runtime windows-10-desktop
我有一个 Windows 服务(使用本教程创建)。
我正在尝试运行IAsyncOperation
:
var uri= new Uri(@"uri/to/second/app");
var options = new LauncherOptions
{
TargetApplicationPackageFamilyName = "second-app-guid"
};
var results = await Launcher.LaunchUriForResultsAsync(uri, options);
Run Code Online (Sandbox Code Playgroud)
但是,我从以下错误中得到以下错误await
:
The type 'IAsyncOperationWithProgress<,>' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.
The type 'IAsyncOperation<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.
The type 'IAsyncActionWithProgress<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.
The type 'IAsyncAction' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.
我查过这个错误,很多人说这是由于 VS 安装错误,但是执行全新安装(同时检查 ISO 的校验和)未能解决问题。
另外,我在一个空白的通用 Windows 应用程序中尝试了这个,但没有出现这个错误。所以我想知道这在 Windows 服务中是否可行?(.NET 框架版本 4.6.1)
根据您的代码,您似乎想在 Windows 服务应用程序中使用 UWP API,并且从您的错误来看,我认为这里的问题是因为您在项目中缺少引用。
\n\n要从经典 .NET Framework 应用程序调用 UWP API,我们应该添加以下两个引用:
\n\n首先,我们需要添加对包含所有支持的 API 定义的元数据文件的引用:it\xe2\x80\x99s,名为Windows.md,我们可以在计算机上的以下路径中找到它:C:\\ Program Files (x86)\\Windows Kits\\10\\UnionMetadata。
第二个是System.Runtime.WindowsRuntime.dll,我们可以在以下路径中找到它:C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\.NETCore\\v4.5。如果没有这个 DLL,我们就无法await
在 WinRT 类型上使用,这个以及 .NET 和 WinRT 之间的其他互操作都是由这个 DLL 提供的。
此外,我们还可以使用UWPDesktop NuGet 包来代替上述两个参考。有关详细信息,请参阅从桌面应用程序调用 Windows 10 API。
\n\n\n\n\n所以我想知道这在 Windows 服务中是否可能?(.NET Framework 版本 4.6.1)
\n
但是,即使您修复了这些编译器错误,您的代码仍然无法工作。这是因为Launcher.LaunchUriForResultsAsync
标准桌面应用程序和 Windows 服务不支持 API。所以不允许在Windows服务Launcher.LaunchUriForResultsAsync
中使用方法,即使方法在Windows服务中不起作用。Launcher.LaunchUriAsync