Beispiel #1
0
        /// <summary>
        /// Creates a new InMemoryViewModel by reading the persisted XML file from disk
        /// </summary>
        private void HydrateViewModelFromXml()
        {
            //Ask the user where they want to open the file from, and open it
            try
            {
                openFileService.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
                openFileService.FileName         = String.Empty;
                openFileService.Filter           = "Xml files (*.xml)|*.xml";
                bool?result = openFileService.ShowDialog(null);

                if (result.HasValue && result.Value)
                {
                    //open to XML
                    PesistentVM pesistentVM =
                        ViewModelPersistence.HydratePersistedViewModel(openFileService.FileName);
                    //check we got something, and recreate the full weight InMemoryViewModel from
                    //the lighter weight XML read PesistentVM
                    if (pesistentVM != null)
                    {
                        CurrentVM = new InMemoryViewModel();

                        //Start out with PropertiesViewModel shown
                        PropertiesViewModel propertiesViewModel =
                            new PropertiesViewModel();
                        propertiesViewModel.IsCloseable = false;
                        CurrentVM.PropertiesVM          = propertiesViewModel;
                        //and now read in other data
                        CurrentVM.ViewModelName        = pesistentVM.VMName;
                        CurrentVM.CurrentViewModelType = pesistentVM.VMType;
                        CurrentVM.ViewModelNamespace   = pesistentVM.VMNamespace;
                        //and add in the individual properties
                        foreach (var prop in pesistentVM.VMProperties)
                        {
                            CurrentVM.PropertiesVM.PropertyVMs.Add(new
                                                                   SinglePropertyViewModel
                            {
                                PropertyType   = prop.PropertyType,
                                PropName       = prop.PropName,
                                UseDataWrapper = prop.UseDataWrapper
                            });
                        }

                        HasContent = true;
                    }
                    else
                    {
                        messageBoxService.ShowError(String.Format("Could not open the ViewModel {0}",
                                                                  openFileService.FileName));
                    }
                }
            }
            catch (Exception ex)
            {
                messageBoxService.ShowError("An error occurred trying to Opening the ViewModel\r\n" +
                                            ex.Message);
            }
        }
        /// <summary>
        /// Executes the GenerateVMCommand
        /// </summary>
        private void ExecuteGenerateVMCommand()
        {
            ClearCodeWorkSpaces();
            Tuple <Boolean, String> info = SaveOrGenerateOperation("C# files (*.cs)|*.cs",
                                                                   SaveOrGenerate.Generate);

            if (info != null && info.First)
            {
                Workspaces.Insert(1, new GeneratedCodeViewModel
                {
                    IsCloseable = false,
                    DisplayName = ".cs",
                    FileName    = ViewModelPersistence.StripFileName(info.Second) + ".cs"
                });
                Workspaces.Insert(2, new GeneratedCodeViewModel
                {
                    IsCloseable = false,
                    DisplayName = ".g.cs",
                    FileName    = ViewModelPersistence.StripFileName(info.Second) + ".g.cs"
                });
            }
        }
        /// <summary>
        /// Saves the current ViewModel as an XML file or generates C# code for the
        /// current ViewModel
        /// </summary>
        /// <param name="filter">The save file filter to use</param>
        /// <param name="operation">The SaveOrGenerate operation to use</param>
        private Tuple <Boolean, String> SaveOrGenerateOperation(String filter, SaveOrGenerate operation)
        {
            if (!IsValid)
            {
                messageBoxService.ShowError("The current ViewModel is InValid\r\nPlease fix it then retry");
                return(null);
            }
            else
            {
                //Ask the user where they want to save the file, and save it
                try
                {
                    saveFileService.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
                    saveFileService.FileName         = String.Empty;
                    saveFileService.Filter           = filter;
                    saveFileService.OverwritePrompt  = true;
                    bool?result = saveFileService.ShowDialog(null);

                    if (result.HasValue && result.Value)
                    {
                        //Create a Persistence ViewModel based on the current In Memory ViewModel
                        PesistentVM pesistentVM = new PesistentVM();
                        pesistentVM.VMName      = ViewModelName;
                        pesistentVM.VMType      = CurrentViewModelType;
                        pesistentVM.VMNamespace = ViewModelNamespace;

                        foreach (SinglePropertyViewModel propVM in propertiesViewModel.PropertyVMs)
                        {
                            pesistentVM.VMProperties.Add(new
                                                         PesistentVMSingleProperty
                            {
                                PropertyType        = propVM.PropertyType,
                                PropName            = propVM.PropName,
                                UseDataWrapper      = propVM.UseDataWrapper,
                                ParentViewModelName = ViewModelName
                            });
                        }


                        bool success = false;

                        FileInfo file = new FileInfo(saveFileService.FileName);

                        //decide what file needs saving/generating
                        switch (operation)
                        {
                        case SaveOrGenerate.Save:
                            //save to XML
                            success = ViewModelPersistence.PersistViewModel(
                                saveFileService.FileName, pesistentVM);
                            ShowSaveOrGenMessage(success, file.Name, SaveOrGenerate.Save);
                            return(TupleHelper.New(success, saveFileService.FileName));

                        case SaveOrGenerate.Generate:
                            //generate code
                            success = ViewModelPersistence.CreateViewModelCode(
                                saveFileService.FileName, pesistentVM);
                            ShowSaveOrGenMessage(success, file.Name, SaveOrGenerate.Generate);
                            return(TupleHelper.New(success, saveFileService.FileName));
                        }
                    }
                }
                catch (Exception ex)
                {
                    messageBoxService.ShowError(
                        String.Format("An error occurred trying to {0} the ViewModel\r\n{1}",
                                      operation.ToString(), ex.Message));
                    return(null);
                }
                return(null);
            }
        }