/CBR/CBR/Views/File/DeviceConfigView.xaml.cs

# · C# · 56 lines · 45 code · 7 blank · 4 comment · 4 complexity · 9a5aa4e41ea6393845301ea158e3b16b MD5 · raw file

  1. using System.Linq;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using CBR.Core.Helpers;
  5. using CBR.Core.Services;
  6. using CBR.ViewModels;
  7. using System.IO;
  8. using System.Collections.Generic;
  9. using CBR.Core.Models;
  10. namespace CBR.Views
  11. {
  12. /// <summary>
  13. /// Interaction logic for DeviceConfigView.xaml
  14. /// </summary>
  15. public partial class DeviceConfigView : UserControl
  16. {
  17. public DeviceConfigView()
  18. {
  19. InitializeComponent();
  20. if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
  21. {
  22. //WorkspaceService.Instance.Settings.DeviceInfoList.Clear();
  23. if (WorkspaceService.Instance.Settings.DeviceInfoList.Count <= 0)
  24. {
  25. if (MessageBox.Show("Your device list is empty, Do you want to load the default one ?", "Warning", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
  26. WorkspaceService.Instance.Settings.DeviceInfoList = (List<DeviceInfo>)XmlHelper.Deserialize(Path.Combine(DirectoryHelper.ApplicationPath, "devices.xml"),
  27. WorkspaceService.Instance.Settings.DeviceInfoList.GetType());
  28. }
  29. this.DataContext = new DeviceConfigViewModel(WorkspaceService.Instance.Settings.DeviceInfoList);
  30. }
  31. }
  32. private void btnSave_Click(object sender, RoutedEventArgs e)
  33. {
  34. DeviceConfigViewModel dc = this.DataContext as DeviceConfigViewModel;
  35. WorkspaceService.Instance.Settings.DeviceInfoList = dc.Data.ToList<Core.Models.DeviceInfo>();
  36. XmlHelper.Serialize(Path.Combine(DirectoryHelper.ApplicationPath, "devices.xml"), WorkspaceService.Instance.Settings.DeviceInfoList);
  37. }
  38. private void btnAddDevice_Click(object sender, RoutedEventArgs e)
  39. {
  40. DeviceConfigViewModel dc = this.DataContext as DeviceConfigViewModel;
  41. dc.Data.Add(new Core.Models.DeviceInfo(this.tbModel.Text, this.tbManufacturer.Text));
  42. }
  43. private void btnDeleteDevice_Click(object sender, RoutedEventArgs e)
  44. {
  45. DeviceConfigViewModel dc = this.DataContext as DeviceConfigViewModel;
  46. dc.Data.Remove(this.lbDevices.SelectedItem as DeviceInfo);
  47. }
  48. }
  49. }