gpt4 book ai didi

.net - IAsyncOperation 在 Windows 服务中等待 : "Type is defined in an assembly that is not referenced..."

转载 作者:行者123 更新时间:2023-12-03 20:22:30 25 4
gpt4 key购买 nike

我有一个 Windows 服务(使用此 tutorial 创建)。

我正在尝试运行 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)

    最佳答案

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

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

  • 首先,我们需要添加对包含所有支持的 API 定义的元数据文件的引用:它被称为 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 package而不是以上两个引用。更多信息,请参阅 Calling Windows 10 APIs From a Desktop Application .

    So I wonder if this is even possible in a Windows Service? (.NET Framework version 4.6.1)



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

    关于.net - IAsyncOperation 在 Windows 服务中等待 : "Type is defined in an assembly that is not referenced...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41943297/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com