WF工作流设计器

先创建一个WinForm应用程序吧,再放SplitContainer,再工具箱WPF项下elementHost分别放三个,如下

image.png

再添加一个类,factory.cs

image.png

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities.Presentation;
using System.Activities.Presentation.Hosting;
using System.Windows;
using System.Windows.Controls;
using System.Activities.Presentation.Toolbox;
using System.Activities.Statements;
 
namespace WindowsFormsApp3
{
    public class factory
    {
        private WorkflowDesigner wd;
 
        private string DefaultXamlActivity
        {
            get
            {
                return @"<Activity x:Class='EmptyActivity' mva:VisualBasic.Settings='Assembly references and imported namespaces serialized as XML namespaces' xmlns='http://schemas.microsoft.com/netfx/2009/xaml/activities' xmlns:mva='clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' />";
            }
        }
 
        public void RegisterMetadata()
        {
            System.Activities.Core.Presentation.DesignerMetadata designers = new System.Activities.Core.Presentation.DesignerMetadata();
            designers.Register();
        }
 
        public UIElement LoadDesigner(string xaml)
        {
            this.wd = new WorkflowDesigner();
            this.wd.Context.Items.GetValue<ReadOnlyState>().IsReadOnly = false;
 
            this.wd.ModelChanged += delegate (object source, EventArgs args)
            {
                this.wd.Flush();
            };
 
            this.wd.Text = string.IsNullOrEmpty(xaml) ? this.DefaultXamlActivity : xaml;
            this.wd.Load();
 
            Grid.SetColumn(this.wd.View, 1);
            UIElement element = this.wd.View;
            element.IsEnabled = true;
            element.IsHitTestVisible = true;
            return element;
 
        }
 
        public UIElement LoadPropertyInspector()
        {
            Grid.SetColumn(wd.PropertyInspectorView, 2);
            return wd.PropertyInspectorView;
        }
 
        public UIElement LoadToolBox()
        {
            ToolboxControl tc = GetToolboxControl();
            Grid.SetColumn(tc, 0);
            return tc;
        }
 
        private ToolboxControl GetToolboxControl()
        {
            ToolboxControl ctrl = new ToolboxControl();
 
            #region MS Built-in Activity
            ToolboxCategory categoryFlowchart = new ToolboxCategory("Flowchart")
{
new ToolboxItemWrapper(typeof(Flowchart)),
new ToolboxItemWrapper(typeof(FlowDecision))
};
 
            ToolboxCategory categoryCollection = new ToolboxCategory("Collection")
{
new ToolboxItemWrapper(typeof(AddToCollection<>)),
new ToolboxItemWrapper(typeof(ClearCollection<>)),
new ToolboxItemWrapper(typeof(ExistsInCollection<>)),
new ToolboxItemWrapper(typeof(RemoveFromCollection<>)),
};
 
            ToolboxCategory categoryPrimitives = new ToolboxCategory("Primitives")
{
new ToolboxItemWrapper(typeof(Assign)),
new ToolboxItemWrapper(typeof(Assign<>)),
new ToolboxItemWrapper(typeof(Delay)),
new ToolboxItemWrapper(typeof(InvokeMethod)),
new ToolboxItemWrapper(typeof(WriteLine)),
};
 
            ToolboxCategory categoryControlFlow = new ToolboxCategory("ControlFlow")
{
new ToolboxItemWrapper(typeof(DoWhile)),
new ToolboxItemWrapper(typeof(ForEach<>)),
new ToolboxItemWrapper(typeof(If)),
new ToolboxItemWrapper(typeof(Parallel)),
new ToolboxItemWrapper(typeof(ParallelForEach<>)),
new ToolboxItemWrapper(typeof(Pick)),
new ToolboxItemWrapper(typeof(PickBranch)),
new ToolboxItemWrapper(typeof(Sequence)),
new ToolboxItemWrapper(typeof(Switch<>)),
new ToolboxItemWrapper(typeof(While)),
};
 
            ToolboxCategory categoryTransaction = new ToolboxCategory("Transaction")
{
new ToolboxItemWrapper(typeof(CancellationScope)),
new ToolboxItemWrapper(typeof(CompensableActivity)),
new ToolboxItemWrapper(typeof(Compensate)),
new ToolboxItemWrapper(typeof(Confirm)),
new ToolboxItemWrapper(typeof(TransactionScope)),
};
 
            ToolboxCategory categoryErrorHandling = new ToolboxCategory("Error Handling")
{
new ToolboxItemWrapper(typeof(Rethrow)),
new ToolboxItemWrapper(typeof(Throw)),
new ToolboxItemWrapper(typeof(TryCatch)),
};
            #endregion
 
            ctrl.Categories.Add(categoryPrimitives);
            ctrl.Categories.Add(categoryControlFlow);
            ctrl.Categories.Add(categoryErrorHandling);
            ctrl.Categories.Add(categoryFlowchart);
            ctrl.Categories.Add(categoryTransaction);
            ctrl.Categories.Add(categoryCollection);
 
            return ctrl;
        }
    }
}

窗口引用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities.Presentation;
using System.Activities.Presentation.Hosting;
using System.Windows;
using System.Windows.Controls;
using System.Activities.Presentation.Toolbox;
using System.Activities.Statements;
using System.Windows.Forms;
 
namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            factory.RegisterMetadata();
            elementHost1.Child = factory.LoadToolBox();
            elementHost2.Child = factory.LoadDesigner("");
            elementHost3.Child = factory.LoadPropertyInspector();
 
 
 
        }
 
        private factory factory = new factory();
    }
 
}

效果:

image.png


本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2024年4月    »
1234567
891011121314
15161718192021
22232425262728
2930
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864