系统中会有很多数据需要执行流转,以及触发或者存储发送, 这里引入自定义的全局变量来完成这种需求,首先对全局变量数据对象进行设计。
/// <summary>
/// 全局变量
/// </summary>
public class GlobalVariableModel
{
public VariableTypeEnum VariableType { set; get; }
public string VariableName { set; get; }
public object VariableValue { set; get; }
public string Remark { set; get; }
}
public enum VariableTypeEnum
{
String = 0,
Int = 1,
Float = 2,
Decimal = 3,
Short = 4,
Bool = 5
}
包含数据类型、变量名称、变量值、备注。全局变量直接挂载在方案下面。一个方案的变量包含多个,因此是个List集合。
变量会贯穿每个节点 都可以使用,从节点触发 到节点执行后,以及数据结果关联都会用得到。
维护的界面采用的是代码动态绘制panel 文本框 下拉框按钮等,自定义变量等等。代码如下
private VariableControlModel AddTriggerControl()
{
FlowLayoutPanel triggerPanel = new FlowLayoutPanel();
triggerPanel.BorderStyle = BorderStyle.FixedSingle;
triggerPanel.FlowDirection = FlowDirection.LeftToRight;
triggerPanel.Width = FlowPanelVariable.Width - 10;
triggerPanel.Height = 42;
Label variableLabel = new Label();
variableLabel.Text = "变量";
variableLabel.Height = triggerPanel.Height;
variableLabel.AutoSize = false;
variableLabel.Width = 50;
variableLabel.TextAlign = ContentAlignment.MiddleRight;
TextBox textBoxName = new TextBox();
textBoxName.Width = 80;
//类型
Label variableTypeLabel = new Label();
variableTypeLabel.Text = "类型";
variableTypeLabel.Height = triggerPanel.Height;
variableTypeLabel.AutoSize = false;
variableTypeLabel.Width = 50;
variableTypeLabel.TextAlign = ContentAlignment.MiddleRight;
ComboBox comboBox = new ComboBox();
comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox.ValueMember = "Value";
comboBox.DisplayMember = "Name";
comboBox.Items.AddRange(typeList.ToArray());// (typeList.MapToList<VariableTypeModel>().ToArray());
//comboBox.SelectedItem = item.Key;
Label variableValueLabel = new Label();
variableValueLabel.Text = "值";
variableValueLabel.Height = triggerPanel.Height;
variableValueLabel.TextAlign = ContentAlignment.MiddleRight;
variableValueLabel.AutoSize = false;
variableValueLabel.Width = 30;
TextBox textBox = new TextBox();
textBox.Width = 100;
Label variableRemarkLabel = new Label();
variableRemarkLabel.Text = "备注";
variableRemarkLabel.Height = triggerPanel.Height;
variableRemarkLabel.TextAlign = ContentAlignment.MiddleRight;
variableRemarkLabel.AutoSize = false;
variableRemarkLabel.Width = 50;
TextBox textBoxRemark = new TextBox();
textBoxRemark.Width = 250;
Button button = new Button();
button.Text = "移除";
button.Width = 50;
button.Height = 32;
button.Click += Button_Click; ;
//textBox.Text = item.Value;
var model = new VariableControlModel()
{
FlowLayoutPanel = triggerPanel,
VariableType = comboBox,
VariableValueBox = textBox,
VariableNameBox = textBoxName,
VariableRemarkBox = textBoxRemark,
};
VariableControlModels.Add(model);
triggerPanel.Controls.Add(variableLabel);
triggerPanel.Controls.Add(textBoxName);
triggerPanel.Controls.Add(variableTypeLabel);
triggerPanel.Controls.Add(comboBox);
triggerPanel.Controls.Add(variableValueLabel);
triggerPanel.Controls.Add(textBox);
triggerPanel.Controls.Add(variableRemarkLabel);
triggerPanel.Controls.Add(textBoxRemark);
triggerPanel.Controls.Add(button);
FlowPanelVariable.Controls.Add(triggerPanel);
return model;
}
容器采用的是FlowPanel ,可以自动流式布局 控件添加到panel会自动往后追加,比较方便。
好了以上就是全局变量的设计思路。