fixed issues with multiple instances of view models and the config file not being present

This commit is contained in:
Conor 2020-10-29 20:47:52 +00:00
parent 48d9879645
commit 984f96b7fd
8 changed files with 43 additions and 42 deletions

Binary file not shown.

View File

@ -9,6 +9,7 @@ using RGBController2.ViewModels.Dialogs;
using System.ComponentModel; using System.ComponentModel;
using System.Windows; using System.Windows;
using System.Xml; using System.Xml;
using System.IO;
namespace RGBController2.ViewModels namespace RGBController2.ViewModels
{ {
@ -18,6 +19,7 @@ namespace RGBController2.ViewModels
public ICommand NewTabCommand { get; } public ICommand NewTabCommand { get; }
public static ICollection<Tab> Tabs { get; set; } public static ICollection<Tab> Tabs { get; set; }
private string configLocation = "config.xml";
public MainViewModel() public MainViewModel()
{ {
NewTabCommand = new ActionCommand(p => NewTab()); NewTabCommand = new ActionCommand(p => NewTab());
@ -28,7 +30,8 @@ namespace RGBController2.ViewModels
Tabs = _tabs; Tabs = _tabs;
// Check here that the config file exists // Check here that the config file exists
if (File.Exists(configLocation))
{
// Load the config file // Load the config file
using (XmlReader reader = XmlReader.Create("config.xml")) using (XmlReader reader = XmlReader.Create("config.xml"))
{ {
@ -68,6 +71,7 @@ namespace RGBController2.ViewModels
} }
} }
} }
}
private void Tabs_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) private void Tabs_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{ {
@ -121,10 +125,16 @@ namespace RGBController2.ViewModels
/// <param name="e"></param> /// <param name="e"></param>
public void OnWindowClosing(object sender, CancelEventArgs e) public void OnWindowClosing(object sender, CancelEventArgs e)
{ {
// ToDo - Save the config to disk // Check if the config file currently exists
// if it does then we will need to delete it beofre we can save a new config
if (File.Exists(configLocation))
{
File.Delete(configLocation);
}
// This may need to be saved somwhere better, for now we will just save it // This may need to be saved somwhere better, for now we will just save it
// with the executable // with the executable
XmlWriter xmlWriter = XmlWriter.Create("config.xml"); XmlWriter xmlWriter = XmlWriter.Create(configLocation);
xmlWriter.WriteStartDocument(); xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("tabs"); xmlWriter.WriteStartElement("tabs");
@ -154,8 +164,6 @@ namespace RGBController2.ViewModels
xmlWriter.WriteEndDocument(); xmlWriter.WriteEndDocument();
xmlWriter.Close(); xmlWriter.Close();
} }
} }

View File

@ -26,11 +26,6 @@ namespace RGBController2.Views.LightingModes
public StaticView() public StaticView()
{ {
InitializeComponent(); InitializeComponent();
// This is a bit hacky but ensure we are only creating one ArduinoTab object and using
// that in both the MainViewModel and here
var test = MainViewModel.Tabs.Last(); // FIX ME - this needs to be something else than .Last to find the correct viewmodel
DataContext = test.SelectedLightingMode;
} }
} }
} }

View File

@ -16,6 +16,8 @@ namespace RGBController2.Views
// Why? Something to do with MVVM & good practice? // Why? Something to do with MVVM & good practice?
var viewModel = new MainViewModel(); var viewModel = new MainViewModel();
DataContext = viewModel; DataContext = viewModel;
// We are using this to hook on the closing event so that we can save our config when the application is closed
Closing += viewModel.OnWindowClosing; Closing += viewModel.OnWindowClosing;
} }
} }

View File

@ -22,8 +22,6 @@ namespace RGBController2.Views.Tabs
public ArduinoTabView() public ArduinoTabView()
{ {
InitializeComponent(); InitializeComponent();
//DataContext = new ArduinoTab();
} }
} }
} }

View File

@ -22,8 +22,6 @@ namespace RGBController2.Views.Tabs
public CUEDeviceTabView() public CUEDeviceTabView()
{ {
InitializeComponent(); InitializeComponent();
//DataContext = new ArduinoTab();
} }
} }
} }