簡體   English   中英

Workflow Foundation設計人員的本機活動子活動

[英]Workflow Foundation Native activity child activities on designer

我創建了如下所示的本機活動:

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的主體,在其中可以拖放演示說明中顯示的其他活動(類似於Sequence活動)?

您需要創建一個活動設計器項目,以與自定義活動一起使用,該項目具有一個放置區(一個WorkflowItemPresenter控件),用於放置活動以填充自定義活動的body屬性。 然后,您可以設置管道以將您的設計師鏈接到活動。 下面詳細說明了這些步驟。

創建一個新的活動設計器項目

在您的解決方案中,添加一個名為<Your Custom Activity Library>.Design的新Activity Designer項目。 該程序集必須命名為<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>

XAML中的ModelItem代表您的活動,您可以讓設計者使用它來設置活動中的任何屬性。 在上面的示例中,我正在設置WorkflowItemPresenter綁定到您活動的Body屬性。

在活動中注冊設計師

接下來,添加一個稱為RegisterMetadata的類(可以是任何名稱), IRegisterMetadata實現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加載自定義活動設計器的方式是,查找名為<Your Custom Activity Library>.Design.dll ,然后查找實現IRegisterMetadata接口的公共類。

現在,您應該能夠將自定義活動拖放到工作流程中,並且它將具有一個放置區,可用於指定“ Body活動。

您可以喜歡您的設計師,並公開友好的控件,以允許用戶設置您的自定義活動。 您還可以為.NET Framework中提供的現成活動創建自己的設計器。

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM