【问题标题】:Add variables in designer for NativeActivity<T> in wf4在 wf4 中的 NativeActivity<T> 设计器中添加变量
【发布时间】:2015-06-03 08:17:58
【问题描述】:

我有一个扩展 NativeActivity 的类

我想允许从设计器向活动范围添加变量。如何修改我的活动代码,以便设计师让我添加变量?

我的代码

public class MyActivity : NativeActivity<bool>
{
    public InArgument<string> SomeInArg { get; set; }

    public InArgument<object> RefInArg { get; set; }

    public WorkflowDataContext Data { get; set; }

    public Collection<Variable> Variables { get; set; }

    protected override void Execute(NativeActivityContext context)
    {
        Result.Set(context, true);
    }
}

谢谢。

【问题讨论】:

    标签: c# workflow-foundation-4 workflow-foundation


    【解决方案1】:

    令人惊讶的是,添加一个返回空集合的 getter - 解决了问题。我怀疑设计者试图以某种方式访问​​ variables 集合,当返回 null 时 - 它没有提供在设计器中设置变量的选项。

        public Collection<Variable> Variables { get; } = new Collection<Variable>();
    

    但现在尝试访问变量会导致

    Activity '1.1: MyActivity' cannot access this variable 
    because it is declared at the scope of activity '1.1: MyActivity'.  
    An activity can only access its own implementation variables.
    

    【讨论】:

    • 也许您应该在构造函数中初始化您的集合,以避免每次获得属性时出现“新”。
    【解决方案2】:

    尝试:

    Collection<Variable> variables;
    public Collection<Variable> Variables
        {
            get
            {
                if (this.variables == null)
                {
                    this.variables = new Collection<Variable>();
    
                }
                return this.variables;
            }
        }
    

    这只会在避免“访问”问题时初始化您的收藏。

    【讨论】:

    • 是的,感谢您之前的评论,我做到了。请参阅我自己的答案的更新。它可以工作并让我通过设计器添加变量,但在尝试读取它们时给我一个新错误(不是因为集合为空)。见:stackoverflow.com/questions/30616844/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 2013-01-13
    相关资源
    最近更新 更多