简体   繁体   English

使用 Windows.Devices.Bluetooth 命名空间是否需要通用 Windows 平台?

[英]Is Universal Windows Platform required to use the Windows.Devices.Bluetooth namespace?

I am evaluating the feasibility of using Bluetooth Low Energy API from windows for an existing WPF application.我正在评估在 Windows 中为现有 WPF 应用程序使用低功耗蓝牙 API 的可行性。

I noticed that UWP is mentioned all over the place in the documentation for the Windows.Devices.Bluetooth namespace.我注意到Windows.Devices.Bluetooth命名空间的文档中到处都提到了 UWP。 I tried creating a .NET console application but couldn't figure out how to bring this library in.我尝试创建一个 .NET 控制台应用程序,但不知道如何引入这个库。

If I create a UWP console application and fix the version to 1803 for both min and max, then I CAN use this namespace without hiccup.如果我创建了一个 UWP 控制台应用程序并将最小和最大版本的版本修复为 1803,那么我可以使用这个命名空间而不会出现问题。

I want to know if UWP is required to use this namespace or not.我想知道是否需要 UWP 才能使用此命名空间。 In the event that UWP is required, my plan B was to interface with low-energy bluetooth devices in some other way, but I can't seem to find libraries on NuGet that aren't UWP either.如果需要 UWP,我的计划 B 是以其他方式与低功耗蓝牙设备连接,但我似乎无法在 NuGet 上找到不是 UWP 的库。 Are there alternatives?有替代品吗? Our end users are on Windows 10 tablets, but our existing application is WPF.我们的最终用户使用的是 Windows 10 平板电脑,但我们现有的应用程序是 WPF。

[Updated on 2018/11/16] [2018/11/16 更新]

Thanks @Adam Braden - MSFT for improving my reply.感谢 @Adam Braden - MSFT 改进我的回复。 Yes, you could call Windows.Devices.Bluetooth APIs from your desktop app.是的,您可以从桌面应用程序调用Windows.Devices.Bluetooth API。

You need to add reference to C:\\Program Files (x86)\\Windows Kits\\10\\UnionMetadata\\Windows.winmd and C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\.NETCore\\v4.5\\System.Runtime.WindowsRuntime.dll .您需要添加对C:\\Program Files (x86)\\Windows Kits\\10\\UnionMetadata\\Windows.winmdC:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\.NETCore\\v4.5\\System.Runtime.WindowsRuntime.dll

Please see this blog Calling Windows 10 APIs From a Desktop Application for more information.有关详细信息,请参阅此博客从桌面应用程序调用 Windows 10 API

Please note that If you want to call UWP APIs from desktop app, you first need to confirm if it's supported in classic desktop app.请注意,如果您想从桌面应用程序调用 UWP API,首先需要确认经典桌面应用程序是否支持它。 See UWP APIs callable from a classic desktop app to learn how to determine if the API is available in desktop app.请参阅可从经典桌面应用调用的 UWP API ,了解如何确定该 API 在桌面应用中是否可用。

With .Net 5, Microsoft removed built-in support for the consumption of WinRT API's:在 .Net 5 中,Microsoft 删除了对使用 WinRT API 的内置支持:

Previously, CoreCLR could consume Windows metadata (WinMD) files to active and consume WinRT types.以前,CoreCLR 可以使用 Windows 元数据 (WinMD) 文件来激活和使用 WinRT 类型。 Starting in .NET 5, CoreCLR can no longer consume WinMD files directly.从 .NET 5 开始,CoreCLR 不能再直接使用 WinMD 文件。

If you attempt to reference an unsupported assembly, you'll get a FileNotFoundException.如果您尝试引用不受支持的程序集,您将收到 FileNotFoundException。 If you activate a WinRT class, you'll get a PlatformNotSupportedException.如果您激活 WinRT 类,您将收到 PlatformNotSupportedException。

However, there is a recommended solution and that is to change the target framework moniker (TFM).但是,有一个推荐的解决方案,那就是更改目标框架名称 (TFM)。 For example, in your project file (assuming you are targeting .Net 5):例如,在您的项目文件中(假设您的目标是 .Net 5):

<!-- Change from -->

<TargetFramework>net5</TargetFramework>

<!-- To -->    

<TargetFramework>net5.0-windows10.0.19041</TargetFramework>

This will provide access to the API's (although there are still some exceptions to specific API's).这将提供对 API 的访问(尽管对特定 API 仍有一些例外)。

At the time of writing this, the available TFM's are:在撰写本文时,可用的 TFM 是:

Replace the value of the TargetFramework element with one of the following strings:将 TargetFramework 元素的值替换为以下字符串之一:

  • net5.0-windows10.0.17763.0: Use this value if your app targets Windows 10, version 1809. net5.0-windows10.0.17763.0:如果您的应用面向 Windows 10 版本 1809,请使用此值。
  • net5.0-windows10.0.18362.0: Use this value if your app targets Windows 10, version 1903. net5.0-windows10.0.18362.0:如果您的应用面向 Windows 10 版本 1903,请使用此值。
  • net5.0-windows10.0.19041.0: Use this value if your app targets Windows 10, version 2004. net5.0-windows10.0.19041.0:如果您的应用面向 Windows 10 版本 2004,请使用此值。

For more information, you can see the Built-in support for WinRT is removed from .NET web page from Microsoft and for a detailed description on what options are available, see the Call Windows Runtime APIs in desktop apps web page.有关详细信息,您可以查看 Microsoft 的.NET网页中删除了对 WinRT内置支持,有关可用选项的详细说明,请参阅在桌面应用程序中调用 Windows 运行时 API网页。

I want to amend these helpful answers and point out something I found that's relatively new:我想修改这些有用的答案,并指出我发现的一些相对较新的内容:

TL;DR: You just need to pull in Microsoft.Windows.SDK.Contracts from NuGet to call Windows 10 runtime APIs. TL;DR:您只需要从 NuGet 中提取Microsoft.Windows.SDK.Contracts即可调用 Windows 10 运行时 API。

https://blogs.windows.com/windowsdeveloper/2019/04/30/calling-windows-10-apis-from-a-desktop-application-just-got-easier/ https://blogs.windows.com/windowsdeveloper/2019/04/30/calling-windows-10-apis-from-a-desktop-application-just-got-easier/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM