如何解决使用 Windows.System 命名空间添加 CsWinRT nuget 中断
我创建了一个 .NET 5.0 项目,其中一个 dependencies 就在这个 API 上:
AnalyticsInfo.VersionInfo.DeviceFamily
安装 Microsoft.Windows.SDK.Contracts 后,我可以使用此 API。然后,我需要安装 Microsoft.Windows.CsWinRT 包来解决这个错误:
Error NETSDK1130 Referencing a Windows Metadata component directly when targeting
.NETCoreApp,Version=v5.0 is not supported.
Use the C#/WinRT projection tool (https://aka.ms/cswinrt) or a provided projection for this target.
安装后,我无法再访问 Windows.System.Profile
命名空间来调用 AnalyticsInfo
API:
Error CS0234 The type or namespace name 'System' does not exist in the namespace 'Windows' (are you missing an assembly reference?)
解决方法
在 .NET 5 中,删除了 .NET 中对 WinRT API 的内置支持(因为它特定于 Windows),因此我们不能再使用 Microsoft.Windows.SDK.Contracts
。
此处说明的解决方案 Built-in support for WinRT is removed from .NET 是
删除对 Microsoft.Windows.SDK.Contracts 包的引用。 相反,请指定您想要的 Windows API 版本 通过项目的 TargetFramework 属性访问。例如:
<TargetFramework>net5.0-windows10.0.19041</TargetFramework>
请注意,无需手动添加对 C#/WinRT (Microsoft.Windows.CsWinRT
) 的引用,它应该自动完成并在框架依赖项列表中显示为“Microsoft.Windows.SDK.NET.Ref
”。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。