代码之家  ›  专栏  ›  技术社区  ›  Will

活动总是在重新承载的设计器中折叠

  •  1
  • Will  · 技术社区  · 14 年前

    我正试图重新设置设计师的职位,但每次我向设计师添加工作流时:

    _workflowDesigner = new WorkflowDesigner();
    // added to UI here
    Properties.Content = _workflowDesigner.PropertyInspectorView;
    _workflowDesigner.Load(myWorkflowInstance);
    

    哪里 myWorkflowInstance 是在引用的程序集中定义的工作流。我施了魔法 Register 要注册默认活动元数据,请执行以下操作:

    new DesignerMetadata().Register();
    

    我已经登记了我所有的风俗习惯:

    public static void Register(IEnumerable<Type> activityTypes)
    {            
        // activityTypes are all my custom NativeActivities
        // and all workflows (root of System.Activities.Activity)
        var builder = new AttributeTableBuilder();
        var attrGroups =
            from x in activityTypes
            from y in x.GetCustomAttributes(true).OfType<Attribute>()
            group y by x into g 
            select g;
    
        foreach (var typeGroup in attrGroups)
            builder.AddCustomAttributes(typeGroup.Key, typeGroup.ToArray());
        MetadataStore.AddAttributeTable(builder.CreateTable());
    }
    

    然而,当我在设计器中加载一个活动时,得到的是:

    Why r u collapsed?

    我错过了什么?


    我认为这与这些工作流被编译并只存在于活动的实现属性中的事实有关。

    2 回复  |  直到 14 年前
        1
  •  1
  •   Maurice    14 年前

    您的工作流实例是否包装在ActivityBuilder中?

    更新: 在进一步的研究中,我发现了一个使用WorkflowInspectionServices的可能解决方案。

    var activities = WorkflowInspectionServices.GetActivities(new DemoWorkflow());
    designer.Load(activities.First());
    
        2
  •  0
  •   user1228user1228    14 年前

    一点反射器和反射让我想到了这一点:

    var impl = (typeof(DemoWorkflow)
                .GetProperty("Implementation", 
                             BindingFlags.NonPublic | BindingFlags.Instance)
                .GetValue(new DemoWorkflow(), new object[0]) 
                as System.Func<System.Activities.Activity>)();
    
    _workflowDesigner.Load(new ActivityBuilder { Implementation = impl });
    

    是这样吗?真的吗?我知道是我写的,感到恶心。肮脏。

    我在reflector中注意到工作流的xaml实际上嵌入在资源流的程序集中: the stream in Reflector

    但所有的尝试都失败了。

    var target = typeof(DemoWorkflow);
    var name = string.Format("{0}.obj.Debug.{1}.g.xaml", 
                             target.Assembly.GetName().Name, 
                             target.Name);
    Activity derp = null;
    using (var stream = assy.Assembly.GetManifestResourceStream(name))
    {
        var reader = new XamlXmlReader(stream, new XamlSchemaContext());
        // throws an exception as the property Implementation is in the xaml;
        // it is protected and cannot be set, so deserialization blows up
        derp = ActivityXamlServices.Load(reader);
    }
    

    在这一点上,我看没有其他选择。