代码之家  ›  专栏  ›  技术社区  ›  Airsource Ltd

如何在工作流基础上设置自定义活动的通用属性绑定

  •  0
  • Airsource Ltd  · 技术社区  · 6 年前

    我有一个名为“参数”的自定义类和一个名为“参数”的泛型集合属性的自定义活动,我想为参数属性的泛型列表中的一个参数设置绑定,我已经尝试了ActuviyBand,但是它不起作用,有没有办法用工作流基础来实现呢?请参见以下代码:

    public class Parameter:DependencyObject //A Class
    {
    
        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for Name.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NameProperty =
        DependencyProperty.Register("Name", typeof(string), typeof(Parameter));
    
        public string Value
        {
            get { return (string)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Value", typeof(string), typeof(Parameter));
        }
    
        //A Custom Activity with generic property
    
        public partial class Activity1 : System.Workflow.ComponentModel.Activity
        {
            public Activity1()
            {
                InitializeComponent();
            }
    
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            return base.Execute(executionContext);
        }
    
        public List<Parameter> Parameters
        {
            get { return (List<Parameter>)GetValue(ParametersProperty); }
            set { SetValue(ParametersProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for Parameters.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ParametersProperty =
        DependencyProperty.Register("Parameters", typeof(List<Parameter>), typeof(Activity1));
    

    //设计师代码

       #region Designer generated code
    
        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        [System.Diagnostics.DebuggerNonUserCode]
        private void InitializeComponent()
        {
            this.CanModifyActivities = true;
            this.activity12 = new ActivityLibrary1.Activity1();
            this.activity11 = new ActivityLibrary1.Activity1();
    
            List<Parameter> paras1 = new List<Parameter>();
            paras1.Add(new Parameter() { Name = "a", Value = "a" });
            paras1.Add(new Parameter() { Name = "b", Value = "b" });
    
            List<Parameter> paras2 = new List<Parameter>();
            paras2.Add(new Parameter() { Name = "c", Value = "c" });
            paras2.Add(new Parameter() { Name = "d", Value = "d" });
            // 
            // activity12
            // 
            this.activity12.Name = "activity12";
            this.activity12.Parameters = paras1;
            // 
            // activity11
            // 
            this.activity11.Name = "activity11";
            this.activity11.Parameters = paras2;
    
    
            ActivityBind bind = new ActivityBind();
            bind.Name = activity11.Name;
            bind.Path = "Parameters[0].Value";
    
            activity12.Parameters[0].SetBinding(Parameter.ValueProperty, bind);
            // 
            // Workflow1
            // 
            this.Activities.Add(this.activity11);
            this.Activities.Add(this.activity12);
            this.Name = "Workflow1";
            this.CanModifyActivities = false;
    
        }
    
        #endregion
    
        private ActivityLibrary1.Activity1 activity12;
        private ActivityLibrary1.Activity1 activity11;
    
    2 回复  |  直到 5 年前
        1
  •  2
  •   Chris Stavropoulos    15 年前

    我已经在这个领域做了一些额外的工作作为我以前的答案,虽然技术上的可用性并不理想。所以我想出了更好的解决办法。

    基本上,在绑定属性时,当您单击属性框中的省略号时,内置设计器“猜测”要加载哪个控件。当您将它指向一个列表时,它会加载一些奇特的编辑器,允许您手动添加列表项,这对于任何类型的动态数据场景基本上都是无用的。

    我挖了点东西,绊倒在柱子上: http://blogs.microsoft.co.il/blogs/bursteg/archive/2006/10/29/DynamicWorkflowBindingParameters.aspx

    这并不完全是我所需要的,但它确实为我提供了编辑器属性的线索。

    答案的“我需要知道什么”部分在这里,只需将此属性添加到属性声明中(实际属性,而不是DependencyProperty支持字段):

    [Editor(typeof(BindUITypeEditor), typeof(UITypeEditor))]
    

    这并不是工作流框架本身的一个缺点,但是必须深入研究这个问题才能找到解决方案,这有点烦人。似乎只要你使用windows工作流,问题就在那里,而不是答案。

    不管怎样,我希望这能帮助你或其他人,如果你遇到这个问题。

        2
  •  1
  •   Chris Stavropoulos    15 年前

    我只是遇到了同样的问题,我解决了它,只是消除了收集的需要。

    public class Parameters {
      private Dictionary<string, string> _parameters;
    
      public void Add(string name, string value) {
        _parameters.Add(name, value);
      }
    }
    

    然后可以将其绑定到依赖项属性而不是列表。