void DeviceManager_DeviceChanged(DeviceChangedEventArgs eventArgs) { Trace.WriteLine(String.Format("*** MainWindow.DeviceManager_DeviceChanged(): {0}: {1}, {2}({3})", eventArgs.Event, eventArgs.DeviceId, Thread.CurrentThread.Name, Thread.CurrentThread.GetHashCode())); String logStr = eventArgs.Event + ": " + eventArgs.DeviceId + "\r\n";; //LogTextBox.AppendText(logStr); //TODO: Fix Device Arrival/Removal messages coming in the wrong order! if (eventArgs.Event == DeviceChangeEvent.DeviceArrival) { DeviceManager.Refresh(); if (CurrentDevice.IsNull && _PreviousDevice != null && _PreviousDevice.DeviceInstance == eventArgs.DeviceId) { DeviceViewModelList.Add(_PreviousDevice); CurrentDevice = _PreviousDevice; _PreviousDevice = null; } else { var device = DeviceManager.DeviceClassMgrs.SelectMany(mgr => mgr.Devices).Single(dev => dev.DeviceInstanceId == eventArgs.DeviceId); DeviceViewModelList.Add(new DeviceViewModel(device)); } } else if (eventArgs.Event == DeviceChangeEvent.DeviceRemoval) { if (CurrentDevice.DeviceInstance == eventArgs.DeviceId) { _PreviousDevice = CurrentDevice; CurrentDevice = new DeviceViewModel(); } var devModel = DeviceViewModelList.SingleOrDefault(model => model.DeviceInstance == eventArgs.DeviceId); DeviceViewModelList.Remove(devModel); DeviceManager.Refresh(); } // else if (MainMenuItem_File_TrackUsbPort.Checked) // { // _CurrentDevice = FindDeviceByPort(LastPort); // CurrentApi = LoadDeviceApis(_CurrentDevice); // // if (_CurrentDevice != null) // { // logStr = String.Format("Track USB Port(Hub{0},Port{1}): AutoSelect \"{2}\".\r\n", _CurrentDevice.UsbHub.Index, _CurrentDevice.UsbPort, _CurrentDevice.ToString()); // LogTextBox.AppendText(logStr); // } // } // UpdateStatus(); }
private void InitDeviceList() { CurrentDevice = new DeviceViewModel(); foreach (var dev in HidDeviceClass.Instance.Devices) { DeviceViewModelList.Add(new DeviceViewModel(dev)); } foreach (var dev in SerialPortDeviceClass.Instance.Devices) { DeviceViewModelList.Add(new DeviceViewModel(dev)); } }