//TODO:Test it
        private XElement SerializeState(WorkflowScrollViewer scrollViewer)
        {
            using (var stream = new MemoryStream())
            {
                XmlSerializer serializer = new XmlSerializer(typeof(WorkflowScrollViewer));
                serializer.Serialize(stream, scrollViewer);
                stream.Position = 0;

                // using (XmlReader reader = XmlReader.Create(stream))
                // {
                return(XElement.Load(stream));
                // }
            }
        }
        /// <summary>
        /// Caches the current changes to the specified workflow.
        /// </summary>
        /// <param name="workflow">The workflow.</param>
        public void CacheWorkflow(WorkflowScrollViewer scrollViewer)
        {
            if (scrollViewer == null)
            {
                return;
            }
            var doc = new XDocument();

            using (XmlWriter writer = doc.CreateWriter())
            {
                XmlSerializer serializer = new XmlSerializer(typeof(WorkflowScrollViewer));
                serializer.Serialize(writer, scrollViewer);
            }
            scrollViewer.ActiveWorkflow.SetAppState(APP_NAME, doc.Root, Guid.Empty);
        }
        /// <summary>
        /// Loads a workflow instance
        /// </summary>
        /// <param name="fileName">path to .flo file. If null, create new temporary Workflow.</param>
        public void LoadWorkflow(string fileName = null)
        {
            TabItem tabItem         = new TabItem();
            Brush   unselectedBrush = new SolidColorBrush(ColorBackground);

            tabItem.Background = unselectedBrush;
            string headerName = "";

            logger.Info("Creating new workflow object");

            var workflow = new Workflow();

            if (fileName != null)
            {
                try
                {
                    workflow.TryLoad(fileName);
                    var elem = workflow.GetAppState(APP_NAME, Guid.Empty);
                    using (var reader = elem.CreateReader())
                    {
                        reader.ReadStartElement();
                        XmlSerializer serializer = new XmlSerializer(typeof(WorkflowScrollViewer));
                        tabItem.Content = serializer.Deserialize(reader);
                        headerName      = ((WorkflowScrollViewer)tabItem.Content).XmlTabHeader;

                        void OnSelectionChanged(object obj, SelectionChangedEventArgs e)
                        {
                            if (e.AddedItems.Contains(tabItem))
                            {
                                TabControl.SelectionChanged -= OnSelectionChanged;

                                // Update Xml provided values pertaining to layout based on when
                                // the application idles after unpacking all of the controls
                                Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle,
                                                                           new Action(() =>
                                {
                                    // Run first configuration pass
                                    ((WorkflowScrollViewer)tabItem.Content).OnConfigureLayout();
                                    // Schedule rendering lines after the first pass layout adjustment is finished
                                    Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle,
                                                                               new Action(() =>
                                    {
                                        ((WorkflowScrollViewer)tabItem.Content).OnConfigureLayout();
                                    }));
                                }));
                            }
                        }
                        TabControl.SelectionChanged += OnSelectionChanged;
                    }
                    ((WorkflowScrollViewer)tabItem.Content).ActiveWorkflow = workflow;
                }
                catch (Exception e)
                {
                    var msg = $"Could not load workflow: {e.Message}";
                    if (e.InnerException != null)
                    {
                        msg += " " + e.InnerException.Message;
                    }
                    Logger.Log(Severity.ERROR, LogCategory.APP, msg);
                    MessageBox.Show(msg, "", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else
            {
                workflow.SetAsNew();
                workflow.Title = $"Workflow {TabControl.Items.Count + 1}";
                headerName     = workflow.Title;
                var content = new WorkflowScrollViewer();
                content.ActiveWorkflow = workflow;
                tabItem.Content        = content;
            }

            TabControl.SelectionChanged += (obj, e) =>
            {
                if (e.AddedItems.Contains(tabItem))
                {
                    tabItem.Background = Brushes.White;
                }
                else
                {
                    tabItem.Background = unselectedBrush;
                }
            };

            tabItem.Header = new TabHeader(headerName, tabItem, OnTabClosed);


            TabControl.Items.Add(tabItem);
            TabControl.SelectedItem = tabItem;
            UpdateLayout();
        }