private void DeviceRemoved(object sender, INTV.Shared.Interop.DeviceManagement.DeviceChangeEventArgs e) { if (e.Type == INTV.Core.Model.Device.ConnectionType.Serial) { var removedDevice = AvailableSerialPorts.FirstOrDefault(p => p.PortName == e.Name); if (DeviceChange.IsDeviceChangeFromSystem(e.State)) { if ((removedDevice != null) && AvailableSerialPorts.Remove(removedDevice)) { INTV.Shared.ComponentModel.CommandManager.InvalidateRequerySuggested(); } } else { // Removal may also indicate that the device is not valid for use by the party responsible for showing the dialog. // In such a case, disable the item. if ((removedDevice != null) && !DisabledSerialPorts.Contains(e.Name)) { removedDevice.IsSelectable = false; DisabledSerialPorts.Add(e.Name); INTV.Shared.ComponentModel.CommandManager.InvalidateRequerySuggested(); } } } }
/// <summary> /// Initializes a new instances of SerialPortSelectorViewModel. /// </summary> /// <param name="prompt">The prompt to display in the port selection area.</param> /// <param name="availablePorts">The available ports.</param> /// <param name="disabledPorts">Non-selectable ports.</param> /// <param name="selectedSerialPort">The currently selected port.</param> /// <param name="baudRates">Available baud rates to choose from.</param> /// <param name="defaultBaudRate">The default baud rate.</param> /// <param name="checkPortAvailability">If <c>true</c>, check the port to see if it is already in use before adding it to the selection list.</param> /// <param name="inclusionFilter">If <c>null</c> or it returns <c>true</c>, ports from all available ports are included.</param> public SerialPortSelectorViewModel(string prompt, IEnumerable <string> availablePorts, IEnumerable <string> disabledPorts, string selectedSerialPort, IEnumerable <int> baudRates, int defaultBaudRate, bool checkPortAvailability, Predicate <IConnection> inclusionFilter) { InclusionFilter = inclusionFilter; if (!string.IsNullOrWhiteSpace(prompt)) { Prompt = prompt; } else { Prompt = Resources.Strings.SelectSerialPortDialog_Message; } if (baudRates == null) { BaudRates = new ObservableCollection <BaudRateViewModel>(new[] { new BaudRateViewModel(defaultBaudRate) }); } else { BaudRates = new ObservableCollection <BaudRateViewModel>(baudRates.Select(r => new BaudRateViewModel(r))); } var ports = availablePorts; if ((ports == null) || !ports.Any()) { ports = inclusionFilter == null ? SerialPortConnection.AvailablePorts : SerialPortConnection.GetAvailablePorts(inclusionFilter); } if (ports != null) { AvailableSerialPorts = new ObservableCollection <SerialPortViewModel>(ports.OrderBy(p => p).Select(p => new SerialPortViewModel(p))); } else { AvailableSerialPorts = new ObservableCollection <SerialPortViewModel>(); } if (disabledPorts == null) { DisabledSerialPorts = new ObservableCollection <string>(); } else { DisabledSerialPorts = new ObservableCollection <string>(disabledPorts); } if (checkPortAvailability) { var portsInUse = INTV.Shared.Model.Device.SerialPortConnection.PortsInUse; foreach (var portInUse in portsInUse.Where(p => (InclusionFilter == null) || InclusionFilter(Connection.CreatePseudoConnection(p, ConnectionType.Serial)))) { DisabledSerialPorts.Add(portInUse); } } foreach (var disabledPort in DisabledSerialPorts.Where(p => (InclusionFilter == null) || InclusionFilter(Connection.CreatePseudoConnection(p, ConnectionType.Serial))).OrderBy(p => p).Reverse()) { var viewModel = AvailableSerialPorts.FirstOrDefault(p => p.PortName == disabledPort); if (viewModel == null) { viewModel = new SerialPortViewModel(disabledPort, false); } else { viewModel.IsSelectable = false; AvailableSerialPorts.Remove(viewModel); } if (AvailableSerialPorts.Count == 0) { AvailableSerialPorts.Add(viewModel); } else { AvailableSerialPorts.Insert(0, viewModel); } } _selectedSerialPort = selectedSerialPort; _selectedSerialPortViewModel = AvailableSerialPorts.FirstOrDefault(p => p.PortName == _selectedSerialPort); DefaultBaudRate = defaultBaudRate; _selectedBaudRate = defaultBaudRate; _selectedBaudRateViewModel = BaudRates.FirstOrDefault(b => b.BaudRate == defaultBaudRate); INTV.Shared.Interop.DeviceManagement.DeviceChange.DeviceAdded += DeviceAdded; INTV.Shared.Interop.DeviceManagement.DeviceChange.DeviceRemoved += DeviceRemoved; }