public PianoPage() { this.InitializeComponent(); // Setup our device watchers for input and output MIDI devices. // Let's us know if devices are connected/disconnected while we're running // (And hopefully catches these gracefully so that we don't crash!) inputDeviceWatcher = new MidiDeviceWatcher(MidiInPort.GetDeviceSelector(), midiInPortComboBox, Dispatcher); inputDeviceWatcher.StartWatcher(); outputDeviceWatcher = new MidiDeviceWatcher(MidiOutPort.GetDeviceSelector(), midiOutPortComboBox, Dispatcher); outputDeviceWatcher.StartWatcher(); // Helper class to take care of MIDI Control messages, set it up here with the sliders msgHelper = new ControlMessageHelper(KB, SliderPitch, SliderMod, SliderVolume, SliderPan, Dispatcher); // Register Suspending to clean up any connections we have Application.Current.Suspending += Current_Suspending; // Register event handlers for KeyTapped and KeyReleased // (These events only occur when user taps/clicks on keys on screen) KB.K_KeyTapped += KB_K_KeyTapped; KB.K_KeyReleased += KB_K_KeyReleased; // Wait until page has finished loading before doing some UI/layout changes Loaded += PianoPage_Loaded; }
// Called when app is suspending private void Current_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e) { // Clean up out watchers inputDeviceWatcher.StopWatcher(); inputDeviceWatcher = null; outputDeviceWatcher.StopWatcher(); outputDeviceWatcher = null; // Remove EventHandlers and try dispose of input & output ports try { midiInPort.MessageReceived -= MidiInPort_MessageReceived; midiInPort.Dispose(); midiInPort = null; } catch { } try { midiOutPort.Dispose(); midiOutPort = null; } catch { } }