【问题标题】:Enumerate UWP MIDI API devices from C# console application?从 C# 控制台应用程序枚举 UWP MIDI API 设备?
【发布时间】:2020-01-04 19:59:41
【问题描述】:

我想使用 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 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 语句无法对 IAsyncOperation&lt;DeviceInformationCollection&gt; 类型的变量进行操作,因为 IAsyncOperation&lt;DeviceInformationCollection&gt; 不包含 GetEnumerator 的公共实例定义”

是否有其他方法可以在不使用异步方法的情况下访问设备信息?

【问题讨论】:

  • 您可以使用async 辅助方法并让Main 等待它完成吗?否则,您应该能够手动连接,但使用await 会更容易。此外,如果 MIDI API 需要显示任何类型的同意 UI(我不知道他们是否这样做),那么控制台应用程序将无法工作,因为提示需要 CoreWindow (至少今天)。

标签: c# uwp midi


【解决方案1】:

您必须确保您的项目至少针对 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 文件并将以下内容添加到 &lt;PropertyGroup&gt; 中来更改 C# 版本:

<LangVersion>7.1</LangVersion>

甚至(如果您想要最新的功能):

<LangVersion>latest</LangVersion>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 2021-09-18
    • 1970-01-01
    • 2015-02-12
    相关资源
    最近更新 更多