⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wxdwfstatemachinedesigner.cs

📁 博客园WxWinter写的WF工作流入门学习资料及示例代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
        }


        /// <summary>
        /// 改变显示比例
        /// </summary>
        /// <param name="zoomFactor"></param>
        public void ProcessZoom(int zoomFactor)
        {
            this.workflowView.Zoom = zoomFactor;
            this.workflowView.Update();
        }


        /// <summary>
        /// 删除选中结点
        /// </summary>
        public void DeleteSelected()
        {
            ISelectionService selectionService = (ISelectionService)this.GetService(typeof(ISelectionService));

            if (selectionService != null)
            {
                if (selectionService.PrimarySelection is Activity)
                {
                    Activity activity = (Activity)selectionService.PrimarySelection;

                    if (activity.Name != this.WorkflowName)
                    {
                        activity.Parent.Activities.Remove(activity);
                        this.workflowView.Update();
                    }
                }
            }
        }


        /// <summary>
        /// 从xoml文件中加载工作流
        /// </summary>
        public void LoadExistingWorkflow()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "xoml files (*.xoml)|*.xoml|All files (*.*)|*.*";
            openFileDialog.FilterIndex = 1;
            openFileDialog.RestoreDirectory = true;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                using (XmlReader xmlReader = XmlReader.Create(openFileDialog.FileName))
                {
                    WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
                  
                   this.workflow = (System.Workflow.Activities.StateMachineWorkflowActivity)serializer.Deserialize(xmlReader);

                    this.LoadWorkflow();

                    this.XomlFile = openFileDialog.FileName;
                    this.Text = "打开文件 [" + openFileDialog.FileName + "]";
                }
            }
        }


        /// <summary>
        /// 保存工作流
        /// </summary>
        /// <returns></returns>
        public bool Save()
        {
            return this.Save(true);
        }




        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                UnloadWorkflow();
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        new public object GetService(Type serviceType)
        {
            return (this.workflowView != null) ? ((IServiceProvider)this.workflowView).GetService(serviceType) : null;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            ShowDefaultWorkflow();
        }


        private void LoadWorkflow(string xoml)
        {
            SuspendLayout();

            DesignSurface designSurface = new DesignSurface();
            wxdWFLoader loader = new wxdWFLoader(activityToolItemFile);
            loader.Xoml = xoml;
            designSurface.BeginLoad(loader);
            IDesignerHost designerHost;
            designerHost = designSurface.GetService(typeof(IDesignerHost)) as IDesignerHost;
            if (designerHost != null && designerHost.RootComponent != null)
            {
                IRootDesigner rootDesigner = designerHost.GetDesigner(designerHost.RootComponent) as IRootDesigner;
                if (rootDesigner != null)
                {
                    UnloadWorkflow();

                    this.designSurface = designSurface;
                    this.loader = loader;
                    this.workflowView = rootDesigner.GetView(ViewTechnology.Default) as WorkflowView;
                    panel2.Controls.Add(this.workflowView);
                    this.workflowView.Dock = DockStyle.Fill;
                    this.workflowView.TabIndex = 1;
                    this.workflowView.TabStop = true;
                    this.workflowView.HScrollBar.TabStop = false;
                    this.workflowView.VScrollBar.TabStop = false;
                    this.workflowView.Focus();

                    ISelectionService selectionService = GetService(typeof(ISelectionService)) as ISelectionService;

                    if (selectionService != null)
                    {
                        selectionService.SelectionChanged += new EventHandler(OnSelectionChanged);
                    }
                }
            }

            ResumeLayout(true);
        }

        private void UnloadWorkflow()
        {
            IDesignerHost designerHost;
            designerHost = GetService(typeof(IDesignerHost)) as IDesignerHost;
            if (designerHost != null && designerHost.Container.Components.Count > 0)
                wxdWFLoader.DestroyObjectGraphFromDesignerHost(designerHost, designerHost.RootComponent as Activity);

            if (this.designSurface != null)
            {
                this.designSurface.Dispose();
                this.designSurface = null;
            }

            if (this.workflowView != null)
            {
                Controls.Remove(this.workflowView);
                this.workflowView.Dispose();
                this.workflowView = null;
            }
        }

        //显示默认的工作流
        private void ShowDefaultWorkflow()
        {
           
            this.workflow = new StateMachineWorkflowActivity();
            workflow.Name = "wxwinter";

            this.LoadWorkflow();
        }


        private void LoadWorkflow()
        {
            using (StringWriter stringWriter = new StringWriter())
            {
                using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter))
                {
                    WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
                    serializer.Serialize(xmlWriter, workflow);
                    this.Xoml = stringWriter.ToString();
                }
            }
        }



        private void OnSelectionChanged(object sender, EventArgs e)
        {
            ISelectionService selectionService = GetService(typeof(ISelectionService)) as ISelectionService;

            if (selectionService != null)
            {
                this.propertyGrid.SelectedObjects = new ArrayList(selectionService.GetSelectedComponents()).ToArray();
            }
        }

        private void SaveFile()
        {
            if (this.XomlFile.Length != 0)
            {
                this.SaveExistingWorkflow(this.XomlFile);
            }
            else
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "xoml files (*.xoml)|*.xoml|All files (*.*)|*.*";
                saveFileDialog.FilterIndex = 1;
                saveFileDialog.RestoreDirectory = true;

                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    this.SaveExistingWorkflow(saveFileDialog.FileName);
                    this.Text = "Designer Hosting Sample -- [" + saveFileDialog.FileName + "]";
                }
            }
        }

        internal void SaveExistingWorkflow(string filePath)
        {
            if (this.designSurface != null && this.loader != null)
            {
                this.XomlFile = filePath;
                this.loader.PerformFlush();
            }
        }

        public bool Save(bool showMessage)
        {
            Cursor cursor = this.Cursor;
            this.Cursor = Cursors.WaitCursor;

            bool saveOK = true;

            try
            {
              
                this.SaveFile();

                if (showMessage)
                {
                    MessageBox.Show(this, "文件已保存 \n" + Path.Combine(Path.GetDirectoryName(this.GetType().Assembly.Location), this.XomlFile), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                saveOK = false;
            }
            finally
            {
                this.Cursor = cursor;
            }

            return saveOK;
        }

    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -