0

我有一个定义了自定义活动的解决方案,并且我让一个设计师来配合它(见下文)。我有 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());
        }
    }
}
4

1 回答 1

2

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

于 2013-02-28T21:39:13.433 回答