I'm writing code in a desktop application that discovers and connects to devices with Wi-Fi Direct for Windows 10. I've followed the instructions on how to call Windows Runtime API from non-WinRT environment, everything is fine on VS 2013 except the following lines and I move them in a console application targeting .NET Framework 4.5:
using System;
using Windows.Devices.Enumeration;
using Windows.Devices.WiFiDirect;
static void Main()
{
DoSomethingAsync().Wait();
}
static async void DoSomethingAsync()
{
var selector = WiFiDirectDevice.GetDeviceSelector(WiFiDirectDeviceSelectorType.DeviceInterface);
foreach (var info in await DeviceInformation.FindAllAsync(selector)) // <-- error
{
...
}
}
And the error is still
'await' requires that the type '
Windows.Foundation.IAsyncOperation<Windows.Devices.Enumeration.DeviceInformationCollection>' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?
with following library references: image
I'm sure selector could be retrieved correctly (something like
System.Devices.InterfaceClassGuid:="{439B20AF-8955-405B-99F0-A62AF0C68D43}"
AND System.Devices.InterfaceEnabled:=System.StructuredQueryType.Boolean#True
), then I assume the hack is still valid for desktop applications on Windows 10.
How do I fix this error?
Thank you very much, @Yuval Itzchakov, @vidalsasoon
It turns out I was mislead by the path where System.Runtime.WindowsRuntime.dll locates. In C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5 the library is actually of .NET Framework 4.0, in which no such extension methods(GetWaiter, AsTask...) are defined. However, it looks like the folder is the default location when you're referencing .NET Framework assemblies in VS.
Then I'm referencing
System.Runtime.WindowsRuntime.dll, of C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Runtime.WindowsRuntime.dll
Windows, of C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd
and now it's working.
Try adding "AsTask()" to the IAsyncOperation and work around the issue that Yuval mentioned.
static void Main()
{
var selector = WiFiDirectDevice.GetDeviceSelector();
var findAllDevicesTask = DeviceInformation.FindAllAsync().AsTask();
Task.WaitAll(findAllDevicesTask);
for (var info in findAllDevicesTask.Result)
{
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With