从 C# 控制台应用程序枚举 UWP MIDI API 设备?
Enumerate UWP MIDI API devices from C# console application?
我想使用 Windows 10 UWP MIDI API 获取 Windows 10 中安装的 MIDI 设备列表。
This article 显示使用 C# 获取 MIDI 设备及其 ID 列表的示例代码:
using Windows.Devices.Midi;
using Windows.Devices.Enumeration;
...
private async void ListMidiDevices()
{
// Enumerate Input devices
var deviceList = DeviceInformation.FindAllAsync(
MidiInPort.GetDeviceSelector());
foreach (var deviceInfo in deviceList)
{
Console.WriteLine(deviceInfo.Id);
Console.WriteLine(deviceInfo.Name);
Console.WriteLine("----------");
}
// Output devices are enumerated the same way, but
// using MidiOutPort.GetDeviceSelector()
}
我尝试在 Visual Studio Community 2015 "Hello World" 示例程序中插入 ListMidiDevices
的代码。我将代码块放在 Console.WriteLine("Hello World!");
的位置
在 "hello world" 控制台示例中。我在上面的适当位置添加了 "using" 语句。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Midi;
using Windows.Devices.Enumeration;
namespace ConsoleApplicationHelloWorld
{
class Program
{
static void Main(string[] args)
{
// Enumerate Input devices
var deviceList = await DeviceInformation.FindAllAsync(
MidiInPort.GetDeviceSelector());
foreach (var deviceInfo in deviceList)
{
System.Diagnostics.Debug.WriteLine(deviceInfo.Id);
System.Diagnostics.Debug.WriteLine(deviceInfo.Name);
System.Diagnostics.Debug.WriteLine("----------");
}
// Output devices are enumerated the same way, but
// using MidiOutPort.GetDeviceSelector() }
}
}
编辑 - VS 未构建 UWP 类型。我升级到 VS Community 2019,并安装了 ConsoleAppUniversal.vsix
。然后我可以创建一个新项目 - Console App C# Universal:
using System;
using Windows.Devices.Midi;
using Windows.Devices.Enumeration;
// This example code shows how you could implement the required main function for a
// Console UWP Application. You can replace all the code inside Main with your own custom code.
// You should also change the Alias value in the AppExecutionAlias Extension in the
// Package.appxmanifest to a value that you define. To edit this file manually, right-click
// it in Solution Explorer and select View Code, or open it with the XML Editor.
namespace ConsoleAppCsharpUniversal
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("starting - no args");
// Enumerate Input devices
var deviceList = DeviceInformation.FindAllAsync(
MidiInPort.GetDeviceSelector());
foreach (var deviceInfo in deviceList)
{
Console.WriteLine(deviceInfo.Id);
Console.WriteLine(deviceInfo.Name);
Console.WriteLine("----------");
}
Console.WriteLine("finish - no args");
}
else
{
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine($"arg[{i}] = {args[i]}");
}
}
Console.WriteLine("Press a key to continue: ");
Console.ReadLine();
}
}
}
现在唯一剩下的错误是"foreach statement cannot operate on variables of type IAsyncOperation<DeviceInformationCollection>
because IAsyncOperation<DeviceInformationCollection>
does not contain a public instance definition for GetEnumerator
"
是否有另一种不使用异步方法访问设备信息的方法?
您必须确保您的项目至少以 C# 7.1 为目标(我认为该模板在 VS 2019 中确实具有开箱即用的功能)并使用 async Main method feature:
您的方法签名将更改为:
public static async Task Main(string[] args)
{
...
}
然后你需要等待FindAllAsync
方法:
var deviceList = await DeviceInformation.FindAllAsync(
MidiInPort.GetDeviceSelector());
注意: 您可以通过在文本编辑器中打开 csproj
文件并将以下内容添加到 <PropertyGroup>
中来更改 C# 版本:
<LangVersion>7.1</LangVersion>
甚至(如果您想要最新功能):
<LangVersion>latest</LangVersion>
我想使用 Windows 10 UWP MIDI API 获取 Windows 10 中安装的 MIDI 设备列表。 This article 显示使用 C# 获取 MIDI 设备及其 ID 列表的示例代码:
using Windows.Devices.Midi;
using Windows.Devices.Enumeration;
...
private async void ListMidiDevices()
{
// Enumerate Input devices
var deviceList = DeviceInformation.FindAllAsync(
MidiInPort.GetDeviceSelector());
foreach (var deviceInfo in deviceList)
{
Console.WriteLine(deviceInfo.Id);
Console.WriteLine(deviceInfo.Name);
Console.WriteLine("----------");
}
// Output devices are enumerated the same way, but
// using MidiOutPort.GetDeviceSelector()
}
我尝试在 Visual Studio Community 2015 "Hello World" 示例程序中插入 ListMidiDevices
的代码。我将代码块放在 Console.WriteLine("Hello World!");
的位置
在 "hello world" 控制台示例中。我在上面的适当位置添加了 "using" 语句。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Midi;
using Windows.Devices.Enumeration;
namespace ConsoleApplicationHelloWorld
{
class Program
{
static void Main(string[] args)
{
// Enumerate Input devices
var deviceList = await DeviceInformation.FindAllAsync(
MidiInPort.GetDeviceSelector());
foreach (var deviceInfo in deviceList)
{
System.Diagnostics.Debug.WriteLine(deviceInfo.Id);
System.Diagnostics.Debug.WriteLine(deviceInfo.Name);
System.Diagnostics.Debug.WriteLine("----------");
}
// Output devices are enumerated the same way, but
// using MidiOutPort.GetDeviceSelector() }
}
}
编辑 - VS 未构建 UWP 类型。我升级到 VS Community 2019,并安装了 ConsoleAppUniversal.vsix
。然后我可以创建一个新项目 - Console App C# Universal:
using System;
using Windows.Devices.Midi;
using Windows.Devices.Enumeration;
// This example code shows how you could implement the required main function for a
// Console UWP Application. You can replace all the code inside Main with your own custom code.
// You should also change the Alias value in the AppExecutionAlias Extension in the
// Package.appxmanifest to a value that you define. To edit this file manually, right-click
// it in Solution Explorer and select View Code, or open it with the XML Editor.
namespace ConsoleAppCsharpUniversal
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("starting - no args");
// Enumerate Input devices
var deviceList = DeviceInformation.FindAllAsync(
MidiInPort.GetDeviceSelector());
foreach (var deviceInfo in deviceList)
{
Console.WriteLine(deviceInfo.Id);
Console.WriteLine(deviceInfo.Name);
Console.WriteLine("----------");
}
Console.WriteLine("finish - no args");
}
else
{
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine($"arg[{i}] = {args[i]}");
}
}
Console.WriteLine("Press a key to continue: ");
Console.ReadLine();
}
}
}
现在唯一剩下的错误是"foreach statement cannot operate on variables of type IAsyncOperation<DeviceInformationCollection>
because IAsyncOperation<DeviceInformationCollection>
does not contain a public instance definition for GetEnumerator
"
是否有另一种不使用异步方法访问设备信息的方法?
您必须确保您的项目至少以 C# 7.1 为目标(我认为该模板在 VS 2019 中确实具有开箱即用的功能)并使用 async Main method feature:
您的方法签名将更改为:
public static async Task Main(string[] args)
{
...
}
然后你需要等待FindAllAsync
方法:
var deviceList = await DeviceInformation.FindAllAsync(
MidiInPort.GetDeviceSelector());
注意: 您可以通过在文本编辑器中打开 csproj
文件并将以下内容添加到 <PropertyGroup>
中来更改 C# 版本:
<LangVersion>7.1</LangVersion>
甚至(如果您想要最新功能):
<LangVersion>latest</LangVersion>