Workflow Foundation本地活动设计者的子活动

Adm*_*vić 1 workflow workflow-foundation workflow-foundation-4 workflow-foundation-4.5

我创建了如下所示的Native Activity:

public sealed class ConsoleColorScope : NativeActivity
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ConsoleColorScope"/> class.
        /// </summary>
        public ConsoleColorScope()
        {
            this.Color = ConsoleColor.Gray;
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///   Gets or sets Body.
        /// </summary>
        [DefaultValue(null)]
        public Activity Body { get; set; }

        /// <summary>
        ///   Gets or sets Color.
        /// </summary>
        public ConsoleColor Color { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        protected override void Execute(NativeActivityContext context)
        {
            context.Properties.Add(ConsoleColorProperty.Name, new ConsoleColorProperty(this.Color));

            if (this.Body != null)
            {
                context.ScheduleActivity(this.Body);
            }
        }

        #endregion

        /// <summary>
        /// The console color property.
        /// </summary>
        private class ConsoleColorProperty : IExecutionProperty
        {
            #region Constants and Fields

            /// <summary>
            ///   The name.
            /// </summary>
            public const string Name = "ConsoleColorProperty";

            /// <summary>
            ///   The color.
            /// </summary>
            private readonly ConsoleColor color;

            /// <summary>
            ///   The original.
            /// </summary>
            private ConsoleColor original;

            #endregion

            #region Constructors and Destructors

            /// <summary>
            /// Initializes a new instance of the <see cref="ConsoleColorProperty"/> class.
            /// </summary>
            /// <param name="color">
            /// The color.
            /// </param>
            public ConsoleColorProperty(ConsoleColor color)
            {
                this.color = color;
            }

            #endregion

            #region Explicit Interface Methods

            /// <summary>
            /// Cleanup the workflow thread.
            /// </summary>
            void IExecutionProperty.CleanupWorkflowThread()
            {
                Console.ForegroundColor = this.original;
            }

            /// <summary>
            /// Setup the workflow thread.
            /// </summary>
            void IExecutionProperty.SetupWorkflowThread()
            {
                this.original = Console.ForegroundColor;
                Console.ForegroundColor = this.color;
            }

            #endregion
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是从工作样本中获取的类:

http://code.msdn.microsoft.com/windowsdesktop/Windows-Workflow-c5649c23#content

但是,当我打开XAML文件时,我无法在范围内看到子活动,因为它显示在上面链接的图片上.我只能看到范围的名称.

我已经创建了自己的NativeActivity版本,但我遇到了同样的问题.是否有一些我必须遵循的程序,这将允许我看到NativeActivity的主体,我可以在其中拖放其他活动(类似于序列活动),因为它在演示描述中显示?

aja*_*987 7

您需要创建一个活动设计器项目以与您的自定义活动一起使用,该活动具有一个放置区域(WorkflowItemPresenter控件),用于放置活动以填充您的自定义活动的body属性.然后,您可以设置管道以将设计器链接到活动.以下详细说明了这些步骤.

创建一个新的Activity Designer项目

在您的解决方案中,添加一个名为like的新Activity Designer项目<Your Custom Activity Library>.Design.必须对程序集进行命名<Your Custom Activity Library>.Design.dll,以便Visual Studio将您的活动设计器用于自定义活动.在设计器的XAML中,您将使用它WorkflowItemPresenter来显示一个放置区域,该放置区域接受您的自定义活动的用户可以使用的活动.

<sap:ActivityDesigner x:Class="Your_Custom_Activity_Library.Design.ConsoleColorScopeDesigner"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"
    xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation">
    <Grid>
        <sap:WorkflowItemPresenter HintText="Drop an activity here!" 
                                   Item="{Binding Path=ModelItem.Body, Mode=TwoWay}"
                                   MinWidth="300"
                                   MinHeight="150" />
    </Grid>
</sap:ActivityDesigner>
Run Code Online (Sandbox Code Playgroud)

XAML中的ModelItem表示您的活动,您可以让设计人员使用它在您的活动中设置任何属性.在上面的示例中,我将设置WorkflowItemPresenter绑定到您的activity的Body属性.

通过您的活动注册设计师

接下来,添加一个RegisterMetadata实现该IRegisterMetadata接口的名为(可以是任何名称)的类.它的实现非常简单:

public class RegisterMetadata : IRegisterMetadata
    {
        /// <summary>
        /// Registers all activity designer metadata.
        /// </summary>
        public static void RegisterAll()
        {
            // Attribute table builder for the metadata store.
            AttributeTableBuilder builder = new AttributeTableBuilder();

            // Register the designer for the custom activities.
            builder.AddCustomAttributes(typeof(ConsoleColorScope), new DesignerAttribute(typeof(ConsoleColorScopeDesigner)));

            // Add the attributes to the metadata store.
            MetadataStore.AddAttributeTable(builder.CreateTable());
        }

        /// <summary>
        /// Registers this instance.
        /// </summary>
        public void Register()
        {
            RegisterMetadata.RegisterAll();
        }
    }
Run Code Online (Sandbox Code Playgroud)

Visual Studio加载自定义活动设计器的方式是查找名为的程序集<Your Custom Activity Library>.Design.dll,然后查找实现该IRegisterMetadata接口的公共类.

您现在应该能够将自定义活动拖放到工作流程中,并且它将具有允许您指定Body活动的放置区域.

您可以对设计师有所了解,并公开友好的控件以允许用户设置您的自定义活动.您还可以为.NET Framework中提供的开箱即用活动创建自己的设计器.

希望有所帮助.