[开源]FreeSCADA的通道数据与控件属性关联以及自动刷新机制研究
【參考文章】:
1. WPF之Binding深入探討, 地址:http://www.cnblogs.com/cappuccino/p/3251631.html
?
1. 幾個重要的類列表:
a) Designer工程下的CommonBindingDialog.cs:
b)?Designer工程下的NumericBindingPanel.cs(或者StringBindingPanel.cs):
c) CommonGUI工程下的SchemaDocument.cs(LoadSchema()和SaveSchema())和RunTime工程下的ShemaView.cs(LoadDocument()):
分別實現(xiàn)WPF界面的XMAL文件方式存儲和XMAL文件方式載入,和實現(xiàn)WinForm下的WPF界面的載入。
2.?通道數(shù)據(jù)與控件屬性關聯(lián)(以及自動刷新機制):
a) 通道數(shù)據(jù)與控件屬性綁定:
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在這里執(zhí)行過后,就是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(); }繼續(xù)來看SavePanelStateAndClose方法的實現(xiàn):
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為PropertyWrapperbindingPanel.Dispose();bindingPanel = null;} }我們再繼續(xù)深入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; // 綁定的源數(shù)據(jù)為通道數(shù)據(jù)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(); }?這里關鍵是WPF編程方式下System.Windows.Data.Binding類的使用,下面我們來看一個簡單的WPF示例工程,該工程演示了WPF中一個TextBox的Text屬性與
數(shù)據(jù)源的綁定、以及子線程中綁定數(shù)據(jù)源值更新的自動同步:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes;using System.Threading; using System.ComponentModel;namespace WpfApplication1 {/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{public class DataSource : INotifyPropertyChanged{private int _index;public int Index{get { return _index; }set{_index = value;if (PropertyChanged != null){this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Index"));}}}public event PropertyChangedEventHandler PropertyChanged;} System.Windows.Data.Binding _bind;Thread _thread;DataSource _dataSource;bool _run;public MainWindow(){InitializeComponent();_dataSource = new DataSource();// System.Windows.Data.Binding方式_bind = new System.Windows.Data.Binding();_bind.Source = _dataSource;_bind.Path = new PropertyPath("Index");_bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;this.textBox1.SetBinding(TextBox.TextProperty, _bind);_run = true;_thread = new Thread(Test);_thread.Start();}void Test(){while (_run) // 里面不能放阻塞式的方法,否則邏輯可能一直卡住{_dataSource.Index++;Thread.Sleep(100);}}private void Window_Closing(object sender, CancelEventArgs e){_run = false;if (_thread != null){_thread.Join();_thread = null;}}} }工程界面:
實際運行界面:
posted on 2016-09-03 17:29 jayhust 閱讀(...) 評論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/jayhust/p/5837542.html
總結(jié)
以上是生活随笔為你收集整理的[开源]FreeSCADA的通道数据与控件属性关联以及自动刷新机制研究的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode45 Jump Game
- 下一篇: 使用jQuery发送POST,Ajax请