Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从C#控制台应用程序枚举UWP MIDI API设备?_C#_Uwp_Midi - Fatal编程技术网

从C#控制台应用程序枚举UWP MIDI API设备?

从C#控制台应用程序枚举UWP MIDI API设备?,c#,uwp,midi,C#,Uwp,Midi,我想使用Windows10UWPMIDIAPI获取Windows10中已安装MIDI设备的列表。 显示使用C#获取MIDI设备及其ID列表的示例代码: 我尝试在Visual Studio Community 2015“Hello World”示例程序中插入ListMidiDevices的代码。我将代码块放在了Console.WriteLine(“helloworld!”)的位置 在“hello world”控制台示例中。我在适当的地方添加了上面的“使用”语句 using System; usin

我想使用Windows10UWPMIDIAPI获取Windows10中已安装MIDI设备的列表。 显示使用C#获取MIDI设备及其ID列表的示例代码:

我尝试在Visual Studio Community 2015“Hello World”示例程序中插入
ListMidiDevices
的代码。我将代码块放在了
Console.WriteLine(“helloworld!”)的位置
在“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,并安装了
ConsoleAppUnversal.vsix
。然后我可以创建一个新项目--控制台应用程序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();
        }
    }
}

使用系统;
使用Windows.Devices.Midi;
使用Windows.Devices.Enumeration;
//此示例代码显示了如何实现
//控制台UWP应用程序。您可以用自己的自定义代码替换Main中的所有代码。
//还应在中的AppExecutionAlias扩展中更改别名值
//将Package.appxmanifest设置为您定义的值。要手动编辑此文件,请单击鼠标右键
//在解决方案资源管理器中打开它,然后选择“查看代码”,或者使用XML编辑器打开它。
命名空间控制台AppSharpUniversal
{
班级计划
{
静态void Main(字符串[]参数)
{
如果(args.Length==0)
{
Console.WriteLine(“启动-无参数”);
//枚举输入设备
var deviceList=DeviceInformation.findalsync(
midinport.GetDeviceSelector());
foreach(deviceList中的var deviceInfo)
{
Console.WriteLine(deviceInfo.Id);
Console.WriteLine(deviceInfo.Name);
Console.WriteLine(--------------”;
}
控制台写入线(“完成-无参数”);
}
其他的
{
for(int i=0;i
现在唯一剩下的错误是“foreach语句不能对
IAsyncOperation
类型的变量进行操作,因为
IAsyncOperation
不包含
GetEnumerator
的公共实例定义。”


有没有其他方法可以不使用异步方法访问设备信息

您必须确保您的项目目标至少为C#7.1(我认为该模板在VS 2019中有现成的版本),并使用:

您的方法签名将更改为:

public static async Task Main(string[] args)
{
   ...
}
然后您需要等待
FindAllAsync
方法:

var deviceList = await DeviceInformation.FindAllAsync(
                         MidiInPort.GetDeviceSelector());
注意:您可以通过在文本编辑器中打开
csproj
文件并将以下内容添加到
中来更改C版本:

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

最新版本

您可以使用
async
helper方法,让
Main
等待它完成吗?否则,您应该能够手动连接,但只需使用
wait
就更容易了。此外,如果MIDI API需要显示任何类型的同意用户界面(我不知道它们是否需要),那么控制台应用程序将无法工作,因为提示需要一个
CoreWindow
(至少在今天)。
var deviceList = await DeviceInformation.FindAllAsync(
                         MidiInPort.GetDeviceSelector());
<LangVersion>7.1</LangVersion>
<LangVersion>latest</LangVersion>