Пример #1
0
        /// <summary>
        /// WorkspaceViewModel's Save method does a two-part serialization. First, it serializes the Workspace,
        /// then adds a View property to serialized Workspace, and sets its value to the serialized ViewModel.
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="isBackup"></param>
        /// <param name="engine"></param>
        /// <exception cref="ArgumentNullException">Thrown when the file path is null.</exception>
        internal void Save(string filePath, bool isBackup = false, EngineController engine = null)
        {
            if (String.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath");
            }

            try
            {
                //set the name before serializing model.
                this.Model.setNameBasedOnFileName(filePath, isBackup);
                // Stage 1: Serialize the workspace.
                var json        = Model.ToJson(engine);
                var json_parsed = JObject.Parse(json);

                // Stage 2: Add the View.
                var jo = AddViewBlockToJSON(json_parsed);

                // Stage 3: Save
                File.WriteAllText(filePath, jo.ToString());

                // Handle Workspace or CustomNodeWorkspace related non-serialization internal logic
                // Only for actual save, update file path and recent file list
                if (!isBackup)
                {
                    Model.FileName = filePath;
                    Model.OnSaved();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message + " : " + ex.StackTrace);
                throw (ex);
            }
        }
Пример #2
0
        /// <summary>
        /// WorkspaceViewModel's Save method does a two-part serialization. First, it serializes the Workspace,
        /// then adds a View property to serialized Workspace, and sets its value to the serialized ViewModel.
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="engine"></param>
        /// <exception cref="ArgumentNullException">Thrown when the file path is null.</exception>
        internal void Save(string filePath, bool isBackup = false, EngineController engine = null)
        {
            if (String.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath");
            }

            try
            {
                // Stage 1: Serialize the workspace.
                string fileName = string.Empty;
                try
                {
                    fileName = Path.GetFileName(filePath);
                    string extension = Path.GetExtension(filePath);
                    if (extension == ".dyn" || extension == ".dyf")
                    {
                        fileName = Path.GetFileNameWithoutExtension(filePath);
                    }
                }
                catch (ArgumentException)
                {
                }

                if (fileName != string.Empty)
                {
                    Model.Name = fileName;
                }

                var json = Model.ToJson(engine);

                // Stage 2: Add the View.
                var jo    = JObject.Parse(json);
                var token = JToken.Parse(this.ToJson());
                jo.Add("View", token);

                // Stage 3: Save
                File.WriteAllText(filePath, jo.ToString());

                // Handle Workspace or CustomNodeWorkspace related non-serialization internal logic
                // Only for actual save, update file path and recent file list
                if (!isBackup)
                {
                    Model.FileName = filePath;
                    Model.OnSaved();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message + " : " + ex.StackTrace);
                throw (ex);
            }
        }