我创建了一个 .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是为了
Remove references to the Microsoft.Windows.SDK.Contracts package. Instead, specify the version of the Windows APIs that you want to access via the TargetFramework property of the project. For example:
<TargetFramework>net5.0-windows10.0.19041</TargetFramework>
请注意,完成此操作后,无需手动添加对 C#/WinRT ( Microsoft.Windows.CsWinRT
) 的引用,它应该自动完成并在框架依赖项列表中显示为“Microsoft.Windows.SDK.NET.Ref
”。
关于c# - 使用 Windows.System 命名空间添加 CsWinRT nuget 中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66063733/