首页 > 代码库 > [开源]FreeSCADA的通道数据与控件属性关联以及自动刷新机制研究

[开源]FreeSCADA的通道数据与控件属性关联以及自动刷新机制研究

1. 几个重要的类列表:

a) Designer工程下的CommonBindingDialog.cs:

技术分享

b) Designer工程下的NumericBindingPanel.cs(或者StringBindingPanel.cs):

技术分享

2. 通道数据与控件属性关联(以及自动刷新机制):

a) 通道数据与控件属性绑定:

CommonBindingDialog.cs中的事件响应方法(Create association按钮被按下):

private void CreateAssociationButton_Click(object sender, EventArgs e){    SavePanelStateAndClose();    if (propertyList.SelectedIndex >= 0 && bindingTypes.SelectedIndex >= 0)    {        BaseBindingPanelFactory factory = (BaseBindingPanelFactory)bindingTypes.SelectedItem; // factory在这里执行过后,就是NumericBindingPanel.cs里定义的NumericBindingPanelFactory类型了        bindingPanel = factory.CreateInstance(); // bindingPanel的具体类型是NumericBindingPanel还是StringBindingPanel,在这里得到了重新定义(不再是基类的BaseBindingPanel类型了)        bindingPanel.Initialize(element, propertyList.SelectedItem as PropertyWrapper, null);        bindingPanel.Parent = panel1;        bindingPanel.Dock = DockStyle.Fill;        CreateAssociationButton.Enabled = false;        bindingTypes.Enabled = false;    }}

CommonBindingDialog.cs中的事件响应方法(Save按钮被按下):

private void saveButton_Click(object sender, EventArgs e){    SavePanelStateAndClose();    if (activeBindings.Count > 0)    {        foreach (PropertyWrapper key in activeBindings.Keys)        {            DependencyObject depObj;            DependencyProperty depProp;            System.Windows.Data.BindingBase binding = activeBindings[key];            if (key.GetWpfObjects(out depObj, out depProp) && binding != null)                BindingOperations.SetBinding(depObj, depProp, binding);        }    }    DialogResult = DialogResult.OK;    Close();}

继续来看SavePanelStateAndClose方法的实现:

private void SavePanelStateAndClose(){    if (bindingPanel != null)    {        BindingBase binding = bindingPanel.Save(); // 见NumericBindingPanel类的Save()定义        if (binding != null)            activeBindings[bindingPanel.Property] = binding; // activeBindings为Dictionary<PropertyWrapper, BindingBase>,bindingPanel.Property为PropertyWrapper        bindingPanel.Dispose();        bindingPanel = null;    }}

我们再继续深入NumericBindingPanel.cs里面定义的Save方法:

public override System.Windows.Data.BindingBase Save(){    if (channel != null)    {        System.Windows.Data.Binding bind = new System.Windows.Data.Binding("Value");        ChannelDataProvider cdp = new ChannelDataProvider();        cdp.ChannelName = channel.PluginId + "." + channel.Name;        bind.Source = cdp; // 绑定的源数据为通道数据        bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;        cdp.Refresh();        ComposingConverter conv = new ComposingConverter();        if (checkBox1.Checked)        {            RangeConverter rc = new RangeConverter();            rc.Min = Decimal.ToDouble(minEdit.Value);            rc.Max = Decimal.ToDouble(maxEdit.Value);            conv.Converters.Add(rc);        }        conv.Converters.Add(new Kent.Boogaart.Converters.TypeConverter(cdp.Channel.Type, Property.PropertyType));        bind.Converter = conv;        bind.Mode = BindingMode.TwoWay;        DependencyObject depObj;        DependencyProperty depProp;        if (Property.GetWpfObjects(out depObj, out depProp))            bind.FallbackValue = depObj.GetValue(depProp);        return bind;    }    else         return base.Save();}

 

[开源]FreeSCADA的通道数据与控件属性关联以及自动刷新机制研究