是否可以从非通用应用程序使用 Wi-Fi Direct? [英] Is it possible to use Wi-Fi Direct from a non Universal application?

查看:27
本文介绍了是否可以从非通用应用程序使用 Wi-Fi Direct?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试编写一个简单的基于命令行的应用程序(使用 Visual Studio 2015 和 Windows 10 中的 C# 和 .NET)以在 Microsoft 的通用示例,但手动添加对必要的 *.dll 和 *.winmd 程序集的引用,而不是创建一个 UniversalWindowsPlatform 项目.(System.Runtime.WindowsRuntime 来自 Refference Assemblies 和 Windows 来自 Windows Kits10Union MetadataWindows.winmd)

I've been trying to code a simple command-line based application (using C# and .NET from Visual Studio 2015 and Windows 10) to start a Wi-Fi Direct advertiser following Microsoft's Universal Samples, but manually adding references to the necessary *.dll and *.winmd assemblies instead of creating a UniversalWindowsPlatform project. (System.Runtime.WindowsRuntime from Refference Assemblies and Windows from Windows Kits10Union MetadataWindows.winmd)

这是相关代码:

public void StartAdvertisement(WiFiDirectAdvertisementListenStateDiscoverability discoverability,
        bool listenToConnections)
    {
        if (mPublisher == null)
            mPublisher = new WiFiDirectAdvertisementPublisher();

        if (listenToConnections)
        {
            mListener = new WiFiDirectConnectionListener();
            mListener.ConnectionRequested += OnConnectionRequested;
        }

        mPublisher.StatusChanged += OnStatusChanged;
        mPublisher.Advertisement.IsAutonomousGroupOwnerEnabled = true;
        mPublisher.Advertisement.ListenStateDiscoverability = discoverability;
        mPublisher.Start();
    }

async void OnConnectionRequested(WiFiDirectConnectionListener sender,
        WiFiDirectConnectionRequestedEventArgs connectionEventArgs)
    {
    // Connection code
    }

广告商启动正常(可以从其他设备找到,并创建必要的网络接口),但是当其他设备尝试连接时不会调用 OnConnectionRequested 方法.我已经看到,要使用 Wi-Fi Direct,通用 Windows 应用程序必须在其清单中添加 接近能力,但对于通用应用程序,没有清单.

The advertiser starts OK (it can be found from other devices, and it creates the necessary network interface), but the OnConnectionRequested method doesn't get called when other devices attempt to connect. I've seen that for using Wi-Fi Direct, an Universal Windows Application must add to its manifest the proximity capability, but for a generic application, there is no manifest.

我能否仅通过引用必要的程序集从非通用 Windows 应用程序中使用 Windows 10 WiFi Direct API?

Can I use the Windows 10 WiFi Direct API from a non-universal Windows application only by referencing the necessary assemblies?

推荐答案

所以,我终于可以使用来自非通用 Windows 的 WinRT API(包括 Wi-Fi Direct 的 API,即使没有声明使用邻近功能的清单)应用程序,但在 Windows 10 中比在 8 或 8.1 中有点棘手.

So, I finally could use the WinRT APIs (including the Wi-Fi Direct ones even without a manifest declaring the proximity capability use) from a non-Universal Windows application, but in Windows 10 is a little bit trickier than in 8 or 8.1.

编辑项目的 *.csproj 以在组内添加以下行...

Once you edit the *.csproj of your project to add the following line inside the group...

<TargetPlatformVersion>10.0.10240.0</TargetPlatformVersion>

您将在引用管理器中看到一个名为 Windows 的新部分,其中包含 winmd 库.它们都没有用,您可能只需要在两个库中添加浏览:

you'll see a new section inside the Reference Manager called Windows, with winmd libraries. None of them will be useful, all you probably need is inside two libraries that you'll have to add browsing:

C:Program Files (x86)Windows Kits10Union MetadataWindows.winmd  
C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETCorev4.5System.Runtime.WindowsRuntime.dll

通过这两个参考,您将避免类似的问题

With those two references you'll avoid problems like

'类型 XXXX 定义在未引用的程序集中'

'The type XXXX is defined in an assembly that is not referenced'

'命名空间 XXXX 在两个不同的程序集中定义'.

'The namespace XXXX is defined in two different assemblies'.

但我们还没有完成!特别是在 Wi-Fi Direct 中,一旦广告商在做广告,并且其他一些计算机尝试连接,如果您有 WiFiDirectConnectionListener 的实例,则应调用以下方法

But we're not done yet! Particularly in Wi-Fi Direct, once the advertiser is, you know, advertising, and some other computer attempts to connect, if you have an instance of WiFiDirectConnectionListener, the following method should be called

async void OnConnectionRequested(WiFiDirectConnectionListener sender, WiFiDirectConnectionRequestedEventArgs connectionEventArgs)

但是你得到一个System.BadImageFormatException.这是因为 System.Runtime.WindowsRuntime.dll 的实际版本与清单中声明的​​版本有所不同,因此无法加载.

but instead you get a System.BadImageFormatException. That's because the System.Runtime.WindowsRuntime.dll actual version differs somehow from the one declared in its manifest, so it can't be loaded.

在 Visual Studio 中打开属性工具,选择 System.Runtime.WindowsRuntime 引用并更改以下属性:将本地复制为 false,将特定版本复制为 true.

Open the properties tool in Visual Studio, select the System.Runtime.WindowsRuntime reference and change the following properties: Copy Local to false and Specific Version to true.

现在应该可以了!

这篇关于是否可以从非通用应用程序使用 Wi-Fi Direct?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆