Question Having a lot of Trouble learning MVVM

macattack18

New member
Joined
Oct 14, 2015
Messages
1
Programming Experience
1-3
I am trying to learn MVVM. Right now I have some simple DataBinding working but I am struggling with some higher concepts. For Instance when my program launches it needs to read an xml file and check how many profiles are there. If there are 2 or more it will display a new window that will ask you to pick the profile you want. If there is only 1 it loads it.

My First question is what is the proper method for displaying a new window. Since it needs Code to determine if it should be displayed I feel it should go into the ViewModel but since it is making a new Window I don't know if it is okay to do that?

Second When the view model loads how do I start executing code? I have been working with someone else and he said I should use a Service. Why do I have to do it this way why not just make a method and call it from the constructor? I keep hearing the word injecting being used a lot. What exactly do they mean by injecting the service?

Lastly Can someone point me at a good MVVM example everything I looked at seems difficult to follow and various MVVM guides I look up differ enough that I am not sure which one to follow.


ViewModel
C#:
        public ConfigModel Model { get; set; }

        private IMyConfigModelService _Service;

        public ConfigViewModel()
        {
            _Service = new IConfigModelService();
            Model = _Service.LoadModel();
        }


        public ConfigViewModel(IMyConfigModelService service)
        {
            _Service = service;
            //Load the model from the service
        }

Service
C#:
public interface IMyConfigModelService
    {
        ConfigModel LoadModel();
    }

    public class IConfigModelService : IMyConfigModelService
    {
        public ConfigModel Model { get; private set; }


        public ConfigModel LoadModel()
        {
            Model = new ConfigModel();


            string workingPath = "C:\\Users\\amcateer\\Documents\\Automation";


            ReadConfigFile(workingPath);


            ReadXML(workingPath);


            return Model;
        }


        private void ReadConfigFile(string workingPath)
        {
            string configPath = workingPath + "\\FTConfiguration.properties";
            string[] ConfigFile = File.ReadAllLines(configPath);
            foreach (string s in ConfigFile)
            {
                string[] Property = s.Split('=');
                switch (Property[0])
                {
                    case "ENVIRONMENT":
                        {
                            int i = -1;
                            Int32.TryParse(Property[1], out i);
                            Model.EnviromentNum = i;
                            break;
                        }
                    case "ENVIRONMENT_NAME":
                        {
                            Model.EnviromentName = Property[1];
                            break;
                        }
                    case "BROWSER":
                        {
                            Model.Browser = Property[1];
                            break;
                        }
                    case "RECIPIENT":
                        {
                            string[] emails = Property[1].Split(',');
                            foreach (string email in emails)
                            {
                                if (email != "")
                                {
                                    Model.EmailRecipients.Add(email);
                                }
                            }
                            break;
                        }
                    case "NO_OF_DEVICES":
                        {
                            int i = -1;
                            Int32.TryParse(Property[1], out i);
                            Model.NumDevices = i;
                            break;
                        }
                    case "TEST_DRIVER_FILE":
                        {
                            Model.TestFile = Property[1];
                            break;
                        }
                    case "NEW_UI_EXECUTION":
                        {
                            Model.NewUIExecution = Property[1];
                            break;
                        }
                    case "LOCALE":
                        {
                            Model.Locale = Property[1];
                            break;
                        }
                    case "GRID_EXECUTION":
                        {
                            Model.GridExecution = Property[1];
                            break;
                        }
                    case "Security":
                        {
                            Model.Security = Property[1];
                            break;
                        }
                    case "ReRunFailed":
                        {
                            Model.ReRunFailed = Property[1];
                            break;
                        }
                    case "DOWNLOAD_CE":
                        {
                            Model.DownloadCE = Property[1];
                            break;
                        }
                    case "INTERMEDIATE_REPORT":
                        {
                            Model.IntermediateReport = Property[1];
                            break;
                        }
                }
            }
        }


        public void ReadXML(string workingPath)
        {
            string xmlPath = workingPath + "\\Configuration.xml";
            ArrayList allOptions = new ArrayList();
            string xmlContent = "";


            try
            {
                xmlContent = File.ReadAllText(xmlPath);
                XmlReader reader = XmlReader.Create(new StringReader(xmlContent));


                // parse the XML file
                while (reader.Read())
                {
                    switch (reader.Name)
                    {
                        case "ENVIRONMENT":
                            {
                                string optionsStr = (reader.ReadString()).Replace(" ", "");
                                Model.LstEnviromentNum.AddRange(optionsStr.Split(','));
                                break;
                            }
                        case "ENVIRONMENT_NAME":
                            {
                                string optionsStr = (reader.ReadString()).Replace(" ", "");
                                Model.LstEnviromentName.AddRange(optionsStr.Split(','));
                                break;
                            }
                        case "BROWSER":
                            {
                                string optionsStr = (reader.ReadString()).Replace(" ", "");
                                Model.LstBrowser.AddRange(optionsStr.Split(','));
                                break;
                            }
                        case "NO_OF_DEVICES":
                            {
                                string optionsStr = (reader.ReadString()).Replace(" ", "");
                                Model.LstNumDevices.AddRange(optionsStr.Split(','));
                                break;
                            }
                        case "TEST_DRIVER_FILE":
                            {
                                string optionsStr = (reader.ReadString()).Replace(" ", "");
                                Model.LstTestFile.AddRange(optionsStr.Split(','));
                                break;
                            }
                        case "LOCALE":
                            {
                                string optionsStr = (reader.ReadString()).Replace(" ", "");
                                Model.LstLocale.AddRange(optionsStr.Split(','));
                                break;
                            }
                    }
                }
            }
            catch (Exception ex)
            {


            }
        }
    }
 
Back
Top Bottom