【问题标题】:Workflow Foundation Native activity child activities on designer设计器上的工作流基础本机活动子活动
【发布时间】:2013-06-06 19:01:34
【问题描述】:

我创建了如下所示的 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
        }
    }

这是取自工作示例的类:

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

但是,当我打开 XAML 文件时,我无法看到范围内的子活动,如上面链接中的图片所示。我只能看到作用域的名称。

我创建了自己的 NativeActivity 版本,但我遇到了同样的问题。是否有一些我必须遵循的程序才能让我看到 NativeActivity 的主体,我可以在其中拖放其他活动(类似于序列活动),如演示说明中所示?

【问题讨论】:

    标签: workflow workflow-foundation-4 workflow-foundation workflow-foundation-4.5


    【解决方案1】:

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

    创建一个新的活动设计器项目

    在您的解决方案中,添加一个名为 &lt;Your Custom Activity Library&gt;.Design 的新活动设计器项目。程序集必须命名为 &lt;Your Custom Activity Library&gt;.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>
    

    XAML 中的 ModelItem 代表您的活动,您可以让设计人员使用它设置活动中的任何属性。在上面的示例中,我将 WorkflowItemPresenter 设置为绑定到您的活动的 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();
            }
        }
    

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

    您现在应该能够将自定义活动拖放到工作流中,并且它将有一个放置区,允许您指定 Body 活动。

    您可以对您的设计器产生兴趣,并公开友好的控件以允许用户设置您的自定义活动。您还可以为 .NET Framework 中提供的开箱即用活动创建自己的设计器。

    希望对您有所帮助。

    【讨论】:

    • 关于注释“程序集必须命名为 MyCustomActivities.Design.dll”的注释 确实,您必须根据 MSDN 上的这篇文章将其命名为 YOUR_ACTIVTY_ASSEMBLY.Design.dll:blogs.msdn.com/b/appfabric/archive/2011/08/14/…
    • 感谢@SimonTheCat,我更新了答案以反映它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多