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.Windows;
using System.Xml;
using System.IO;
namespace RGBController2.ViewModels
{
@ -18,6 +19,7 @@ namespace RGBController2.ViewModels
public ICommand NewTabCommand { get; }
public static ICollection<Tab> Tabs { get; set; }
private string configLocation = "config.xml";
public MainViewModel()
{
NewTabCommand = new ActionCommand(p => NewTab());
@ -28,42 +30,44 @@ namespace RGBController2.ViewModels
Tabs = _tabs;
// Check here that the config file exists
// Load the config file
using (XmlReader reader = XmlReader.Create("config.xml"))
if (File.Exists(configLocation))
{
string name = "";
string type = "";
while (reader.Read())
// Load the config file
using (XmlReader reader = XmlReader.Create("config.xml"))
{
if (reader.IsStartElement())
string name = "";
string type = "";
while (reader.Read())
{
//return only when you have START tag
switch (reader.Name.ToString())
if (reader.IsStartElement())
{
case "name":
name = reader.ReadString();
break;
case "type":
type = reader.ReadString();
break;
//return only when you have START tag
switch (reader.Name.ToString())
{
case "name":
name = reader.ReadString();
break;
case "type":
type = reader.ReadString();
break;
}
}
}
// Create the tab here
if (name != "" && type != "")
{
switch(type)
// Create the tab here
if (name != "" && type != "")
{
case "arduino":
_tabs.Add(new ArduinoTab(name));
break;
case "cue":
_tabs.Add(new CueDeviceTab(name));
break;
}
switch (type)
{
case "arduino":
_tabs.Add(new ArduinoTab(name));
break;
case "cue":
_tabs.Add(new CueDeviceTab(name));
break;
}
name = "";
type = "";
name = "";
type = "";
}
}
}
}
@ -121,10 +125,16 @@ namespace RGBController2.ViewModels
/// <param name="e"></param>
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
// with the executable
XmlWriter xmlWriter = XmlWriter.Create("config.xml");
XmlWriter xmlWriter = XmlWriter.Create(configLocation);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("tabs");
@ -154,8 +164,6 @@ namespace RGBController2.ViewModels
xmlWriter.WriteEndDocument();
xmlWriter.Close();
}
}

View File

@ -26,11 +26,6 @@ namespace RGBController2.Views.LightingModes
public StaticView()
{
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?
var viewModel = new MainViewModel();
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;
}
}

View File

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

View File

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