【问题标题】:WF 4 different IDs on the same activitiesWF 4 个不同的 ID 在相同的活动上
【发布时间】:2015-04-16 14:04:34
【问题描述】:

由于我的应用程序中有一个奇怪的行为,我不得不在调用 WorkflowInvoker.Invoke 之前重新加载设计器。

wd.Flush();
SaveXamlFile(currentXamlPath, wd.Text);

我只是刷新内容,然后将 wd.Text 写入文件。

//cleanup the previous designer
if (wd != null)
{
    wd.ModelChanged -= new EventHandler(Designer_ModelChanged);
}

//designer
wd = new WorkflowDesigner();
designerArea.Child = wd.View;
this.DebuggerService = this.wd.DebugManagerView;

//property grid
propertiesArea.Child = wd.PropertyInspectorView;

//event handler
wd.ModelChanged += new EventHandler(Designer_ModelChanged);

//error service
wd.Context.Services.Publish<IValidationErrorService>(errorService);


wd.Context.Items.Subscribe<Selection>(OnItemSelected);

然后我重新创建 WorkflowDesigner 的新实例并加载之前保存的文件。

wd.Load(currentXamlPath);

我调用 WorkflowInvoker.Invoke 并在派生自 CodeActivity 的自定义活动中使用它的名称: 好的,到现在为止还好,我那里有一个 1.2 的 ID。

我想通过它的 ModelItem 更新这个 Activity 的一些字段,以便立即在 GUI 中显示它们。

IEnumerable<ModelItem> activityCollection = currentWorkflow.Find(currentWorkflow.Root, typeof(Activity));

但问题来了:

我在那里找不到我的活动 ID。现在已从 1.2 转换为 2。为什么会这样?

我试图从我的 Activity Execute 方法发送一个 this 引用并通过 ref 搜索它,但我得到的只是空值。

ModelItem temp = activityCollection.FirstOrDefault((m) => (m.GetCurrentValue() == a));

我确定我在这里遗漏了一些东西,但我无法弄清楚它是什么。

【问题讨论】:

    标签: workflow workflow-foundation-4 workflow-foundation workflow-activity


    【解决方案1】:

    我找到了解决方法:

    在我的自定义活动中,我添加了一个 Guid 属性并覆盖了 CacheMetadata:

    public Guid unique { get; set; }
    
    protected override void CacheMetadata(CodeActivityMetadata metadata)
    {
        if (unique.ToString() == "00000000-0000-0000-0000-000000000000")
            unique = Guid.NewGuid();
    }
    

    当我在设计器上拖动活动时,会生成唯一 ID。我确保这部分代码只被调用一次。 这是为什么?

    因为这样的电话之后,

    IEnumerable<ModelItem> activityCollection = currentWorkflow.Find(currentWorkflow.Root, typeof(Activity));
    

    活动集合中的每个模型都包含该属性( Guid 类型的唯一性)以及在 CacheMetadata 中进行的第一个分配的值。我无法解释这种行为,我只是考虑到了它。

    谁再次调用 CacheMetadata ?像这样:

    Activity root = ActivityXamlServices.Load(currentXamlPath);
    WorkflowInspectionServices.CacheMetadata(root);
    

    因此,Guid 发生了变化,它的实用性消失了。

    这样,我可以为我的自定义活动获取 ModelItem 并更新它的一些属性,这些属性会立即显示在 GUI 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多