6

我有一个 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);

但是,我从以下错误中得到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)

4

3 回答 3

5

根据您的代码,您似乎想在 Windows 服务应用程序中使用 UWP API,并且根据您的错误,我认为这里的问题是因为您在项目中缺少引用

要从经典的 .NET Framework 应用程序调用 UWP API,我们应该添加以下两个引用:

  • 首先,我们需要添加对包含所有支持的 API 定义的元数据文件的引用:它称为Windows.md,我们可以在计算机上的以下路径中找到它:C:\Program Files (x86)\Windows套件\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

所以我想知道这是否可能在 Windows 服务中实现?(.NET 框架版本 4.6.1)

但是,即使您修复了这些编译器错误,您的代码仍然无法工作。这是因为Launcher.LaunchUriForResultsAsync标准桌面应用程序和 Windows 服务不支持 API。所以不允许在 Windows 服务中使用方法Launcher.LaunchUriForResultsAsync,甚至方法在 Windows 服务中也行不通。Launcher.LaunchUriAsync

于 2017-02-01T11:24:16.803 回答
0

Windows 10 SDKhttps://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk下载。然后添加Windows.winmdSystem.Runtime.WindowsRuntime.dll作为您的参考。这对我有用。

于 2018-10-19T19:00:53.833 回答
0

我必须添加Windows 10 SDK到 Visual Studio 安装。

于 2020-06-05T12:26:29.787 回答