VS2010自定义活动设计器未使用

时间:2013-02-28 21:19:03

标签: visual-studio-2010 workflow-foundation-4 workflow-activity

我有一个定义了自定义活动的解决方案,我让设计师继续使用它(见下文)。我有3个项目,一个用于自定义活动,一个用于自定义活动的设计器,第三个用于实际运行我的自定义活动(一个简单的控制台应用程序)。

当我创建使用自定义活动的复合活动时,VS中的设计器不使用我的自定义设计器。我确保设计器程序集以* .Design.dll名称命名,并将其复制到控制台应用程序的bin目录中,但无法弄清楚为什么它没有选择我的自定义设计器。有任何想法吗?以下是所有必要部分的代码。

我的自定义活动

namespace WorkflowSampler.Activities
{
    using System.Activities;
    using System.Diagnostics;

    public class SampleVarChanger : NativeActivity
    {
        private InArgument<int> expressionToEvaluate;

        private Variable<int> counter;

        public Variable<int> Counter
        {
            get
            {
                return this.counter ?? (this.counter = new Variable<int>());
            }

            set
            {
                this.counter = value;
            }
        }

        public InArgument<int> ExpressionToEvaluate
        {
            get
            {
                return this.expressionToEvaluate ?? (this.expressionToEvaluate = new InArgument<int>());
            }

            set
            {
                this.expressionToEvaluate = value;
            }
        }

        protected override void CacheMetadata(NativeActivityMetadata metadata)
        {
            metadata.AddImplementationVariable(this.Counter);

            var exprToEvalRuntimeArg = new RuntimeArgument("ExpressionToEvaluate", typeof(int), ArgumentDirection.In);
            metadata.Bind(this.ExpressionToEvaluate, exprToEvalRuntimeArg);
            metadata.AddArgument(exprToEvalRuntimeArg);
        }

        protected override void Execute(NativeActivityContext context)
        {
            Debug.WriteLine(string.Format("Counter Variable Value: {0}", this.Counter.Get(context)));
        }
    }
}

我的自定义活动的设计器(XAML部件)

<sap:ActivityDesigner x:Class="WorkflowSampler.Activities.Design.SampleVarChangerDesigner"
    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"
                      xmlns:sapc="clr-namespace:System.Activities.Presentation.Converters;assembly=System.Activities.Presentation">
    <sap:ActivityDesigner.Resources>
        <ResourceDictionary>
            <sapc:ArgumentToExpressionConverter x:Key="ArgumentToExpressionConverter" />
        </ResourceDictionary>
    </sap:ActivityDesigner.Resources>
    <Grid Margin="10, 0, 0, 0">
        <Grid.RowDefinitions>
            <RowDefinition Height="25" />
            <RowDefinition Height="25" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="200" />
        </Grid.ColumnDefinitions>

        <TextBlock Text="Variable Name:" VerticalAlignment="Center" Grid.Row="0" Grid.Column="0" />
        <TextBox Name="VariableName" VerticalContentAlignment="Center" Grid.Row="0" Grid.Column="1" TextChanged="OnVariableNameChanged" />

        <TextBlock Text="Expression Using Variable:" VerticalAlignment="Center" Grid.Row="1" Grid.Column="0" />
        <sapv:ExpressionTextBox Name="ExpressionUsingVariable" VerticalContentAlignment="Center" Expression="{Binding Path=ModelItem.ExpressionToEvaluate, Converter={StaticResource ResourceKey=ArgumentToExpressionConverter}, ConverterParameter=In, Mode=TwoWay}" OwnerActivity="ModelItem" Grid.Row="1" Grid.Column="1" />
    </Grid>
</sap:ActivityDesigner>

我的自定义活动的设计器(部件背后的代码)

namespace WorkflowSampler.Activities.Design
{
    using System.Activities;
    // Interaction logic for SampleVarChangerDesigner.xaml
    public partial class SampleVarChangerDesigner
    {
        public SampleVarChangerDesigner()
        {
            InitializeComponent();
        }
        private void OnVariableNameChanged(object sender, TextChangedEventArgs e)
        {
            this.ModelItem.Properties["Counter"].SetValue(new Variable<int>(this.VariableName.Text));
        }
    }
}

VS Designer支持所需的IRegisterMetadata接口

namespace WorkflowSampler.Activities.Design
{
    using System.Activities.Presentation.Metadata;
    using System.ComponentModel;
    /// <summary>
    /// Registers the activity designer metadata.
    /// </summary>
    public class RegisterMetadata : IRegisterMetadata
    {
        public void Register()
        {
            AttributeTableBuilder builder = new AttributeTableBuilder();
            builder.AddCustomAttributes(typeof(SampleVarChanger), new DesignerAttribute(typeof(SampleVarChangerDesigner)));
            MetadataStore.AddAttributeTable(builder.CreateTable());
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我想通了......我需要让设计项目将其DLL复制到活动项目中,所以我所做的是创建一个MSBuild post build事件,将设计库复制到activity的debug文件夹。现在VS拿起它。