在设计师选择活动时,如何获取相关活动本身的参考?

时间:2011-11-15 21:45:44

标签: workflow-foundation workflow-foundation-4

我添加回调

_workflowDesigner.Context.Items.Subscribe<Selection>(SelectionChanged);

在重新托管的设计器中,并尝试在回调中查找相关活动。然后我假设我可以添加一些事件处理程序来为用户提供一种操作它的某些属性的方法。 (即添加事件处理程序

(target as WorkflowViewElement).PreviewKeyDown += new KeyEventHandler(DoSomeWorkBySomeKeyDown);

)但这种方式看起来不起作用,(选择选择)无法找到活动。

我还尝试在以下代码中添加事件处理程序,但它只能在WF中添加活动后立即生效。单击其他活动容器后,再次选择活动,事件处理程序将丢失,不再被触发。

public sealed class WrappedCodeActivity : IActivityTemplateFactory
{
    private CodeActivityAbc codeActivityAbc = new CodeActivityAbc() { DisplayName = "abc" };

    public WorkflowViewElement oneWorkflowViewElement;

    public Activity Create(DependencyObject target)
    {
        oneWorkflowViewElement = (WorkflowViewElement)target;

        (target as WorkflowViewElement).PreviewKeyDown += new KeyEventHandler(DoSomeWorkBySomeKeyDown);

        return codeActivityAbc;
    }
.  .  .

有人有任何想法吗?请认为你有任何想法。

1 个答案:

答案 0 :(得分:1)

_workflowDesigner.Context.Items.Subscribe<Selection>(selection =>
{
    foreach (var item in selection.SelectedObjects)
    {
        Console.WriteLine("Activity of type: " + item.ItemType + " selected.");

        if (item.ItemType == typeof(Sequence))
        {
            // Get Sequence object
            var seq = item.GetCurrentValue() as Sequence;

            // Add variable to Sequence
            seq.Variables.Add(new Variable<int>("NewIntegerVar", 10));
        }
    }
});