WF工作流设计器

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


再添加一个类,factory.cs


代码:

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();
    }

}

效果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵之章

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值