You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 8.0 WPF项目如何添加Windows API访问权限?

.NET 8 WPF 引用 Windows.Devices.Bluetooth 报错 CS0234 的解决办法

核心原因

.NET 8 已弃用 Microsoft.Windows.SDK.Contracts NuGet包,这个包是.NET 5/6/7时期的过渡方案,.NET 8开始直接通过Windows SDK绑定支持WinRT API,所以之前的老方法不再适用。

具体解决步骤

1. 确保项目目标框架为Windows专属

打开项目的.csproj文件,确认配置如下:

<PropertyGroup>
    <TargetFramework>net8.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
    <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion> <!-- 最低要求Windows 10 1809版本 -->
</PropertyGroup>

必须使用net8.0-windows而非单纯的net8.0,只有Windows目标框架才包含WinRT API绑定。

2. 启用WinRT支持

如果已有上述配置仍报错,在.csproj中添加以下配置:

<PropertyGroup>
    <EnableWindowsRuntime>true</EnableWindowsRuntime>
</PropertyGroup>

3. 解决dotnet restore挂起问题

先清理NuGet本地缓存,再重新还原:

  • 进入项目目录,打开命令行执行:
dotnet nuget locals all --clear
  • 之后执行dotnet restore,或者在Visual Studio右键项目选择「还原NuGet包」。

4. 检查Windows SDK安装状态

若仍有问题,确认Visual Studio已安装对应版本的Windows SDK:

  • 打开Visual Studio安装器,选择「修改」,确保勾选了Windows 10 SDK(17763及以上版本)或Windows 11 SDK。

内容的提问来源于stack exchange,提问作者John John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 21:54:54