我的目标是在Windows Store Apps中接收MIDI消息.Microsoft提供了一个称为Microsoft.WindowsPreview.MidiRT的API(作为nuget包).我设法获得了一个Midi端口,但是没有出现MessageReceived事件,尽管我在MIDI键盘上按了键...
我的目标是在Windows Store Apps中接收MIDI消息.
Microsoft提供了一个称为Microsoft.WindowsPreview.MidiRT的API(作为nuget包).
我设法获得了一个Midi端口,但是没有出现MessageReceived事件,尽管我在MIDI键盘上按了键,并且其他MIDI程序显示PC收到了这些消息.
这是我的代码:
public sealed partial class MainPage : Page
{
private MidiInPort port;
public MainPage()
{
this.InitializeComponent();
DeviceWatcher watcher = DeviceInformation.CreateWatcher();
watcher.Updated += watcher_Updated;
watcher.Start();
}
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
port.Dispose();
}
async void watcher_Updated(DeviceWatcher sender, DeviceInformationUpdate args)
{
DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(MidiInPort.GetDeviceSelector());
foreach (var item in deviceCollection)
{
Debug.WriteLine(item.Name);
if (port == null)
{
port = await MidiInPort.FromIdAsync(item.Id);
port.MessageReceived += port_MessageReceived;
}
}
}
void port_MessageReceived(MidiInPort sender, MidiMessageReceivedEventArgs args)
{
Debug.WriteLine(args.Message.Type);
}
}
有任何想法吗?
解决方法:
可能相关:您的设备观察者代码未遵循正常模式.这是您需要做的:
DeviceWatcher midiWatcher;
void MonitorMidiChanges()
{
if (midiWatcher != null)
return;
var selector = MidiInPort.GetDeviceSelector();
midiWatcher = DeviceInformation.CreateWatcher(selector);
midiWatcher.Added += (s, a) => Debug.WriteLine("Midi Port named '{0}' with Id {1} was added", a.Name, a.Id);
midiWatcher.Updated += (s, a) => Debug.WriteLine("Midi Port with Id {1} was updated", a.Id);
midiWatcher.Removed += (s, a) => Debug.WriteLine("Midi Port with Id {1} was removed", a.Id);
midiWatcher.EnumerationCompleted += (s, a) => Debug.WriteLine("Initial enumeration complete; watching for changes...");
midiWatcher.Start();
}
沃梦达教程
本文标题为:c#-在Windows Store应用中使用MIDI(Win 8.1)
基础教程推荐
猜你喜欢
- C# FileStream复制大文件功能 2023-01-22
- C#中struct与class的区别详解 2023-06-15
- c# – 如何在Windows安装程序中为WinForms应用程序获取用户输入? 2023-09-20
- C#多线程之线程同步 2023-05-26
- c# 三种方法调用WebService接口 2023-03-04
- 利用AOP实现SqlSugar自动事务 2022-11-19
- C#如何快速释放内存的大数组详解 2023-03-29
- C#使用GUID(全局统一标识符) 2023-05-31
- C# 枚举类型的声明和使用 2023-04-22
- c# 数据标注与数据校验 2023-03-14