c# - 使用 Windows.System 命名空间添加 CsWinRT nuget 中断

标签 c# .net windows-runtime .net-5

我创建了一个 .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/

相关文章:

c# - 将 VirtualPathProvider 用于简单的静态文件

c# - 如何制作一个没有正方形的 Windows 应用程序?

random - 在 WinRT 中创建随机数有哪些加密安全选项?

c# - MouseEnter 事件的奇怪行为

.net - 获取计算机的上次唤醒时间

c# - 如何将虚拟化 ListView 放入 StackPanel 或 Flyout 中?

c# - 将 HttpClient 类与 WinRT 结合使用

c# - Dynamics CRM 如何获取所有实体的列表

javascript - 在 MVC razor View 中使用浏览器的后退按钮后清除表单元素

c# - 如何完全忽略正则表达式中的换行符和制表符?