WPF学习笔记(一)
现在属于项目并发阶段,不光要做新一代煤矿智能安全监控监测系统,还要抽出时间做新一代井下数字集群及GSM无线通讯系统,真是应接不暇啦,好在前一个项目监控中心联网系统平台已经顺利结束,并且关闭了,要不就得3个项目并行开发了,可怜了我的小身板呀,可不抗这么折腾了。
新一代井下数字集群及GSM无线通讯系统正处于需求调研阶段,估计跟硬件的协议还会定一段时间,而新一代煤矿智能安全监控监测系统已经将数据库设计好了,下面开始正式的界面功能开发,准备用WPF进行页面的设计,虽说现在JquerUI、HTML5、EasyUI还是很火,下面着重记录下学习WPF的心得。
WPF:Windows Presentation Foundation
在wpf中,用户界面设计使用的语言是xaml extensible application markup language
可以更好的面向windows 7、Windows Server 2008 和Windows Server 2008 R2
使用WPF的好处就不多说啦。
进入实践,实践是检验真理的唯一标准!
准备开发一个界面,用来控制数据库的连接,使用xml进行数据的存储
xml如下:
<DataBase>
<Item key="ServerIP" value="ed8+sx/4Qz37OOESyrpDkw==" />
<Item key="DBName" value="AkWRIZAcop6TE42ISWf8jw==" />
<Item key="UserName" value="C1CIY+qVLWBelkhioTy05g==" />
<Item key="UserPassWord" value="kQqaHenI6yhnHrrytme6WA==" />
</DataBase>
其中的value是经过加密的。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <Window x:Class="KJ2012G.ini.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="KJ2012G通讯配置" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="350" Width="525"> 5 <Grid> 6 <TabControl Height="297" HorizontalAlignment="Left" Margin="23,14,0,0" Name="tabControl1" VerticalAlignment="Top" Width="468"> 7 <TabItem Header="数据库配置" Name="数据库配置"> 8 <Grid Height="259"> 9 <Grid.RowDefinitions> 10 <RowDefinition Height="182*" /> 11 <RowDefinition Height="8*" /> 12 <RowDefinition Height="8*" /> 13 </Grid.RowDefinitions> 14 <GroupBox Header="DB节点配置" Height="222" HorizontalAlignment="Left" Margin="15,15,0,0" Name="DB节点配置" VerticalAlignment="Top" Width="437" Grid.RowSpan="3"></GroupBox> 15 <Grid Margin="35,39,40,30"> 16 <Grid.ColumnDefinitions> 17 <ColumnDefinition Width="29*" /> 18 <ColumnDefinition Width="142" /> 19 <ColumnDefinition Width="234*" /> 20 </Grid.ColumnDefinitions> 21 <Label Content="数据库地址:" Grid.Column="1" Height="28" HorizontalAlignment="Left" Margin="31,15,0,0" Name="label1" VerticalAlignment="Top" Width="86" /> 22 <Label Content="数据库名称:" Grid.Column="1" Height="28" HorizontalAlignment="Left" Margin="32,43,0,0" Name="label2" VerticalAlignment="Top" Width="83" /> 23 <Label Content="用 户 名:" Grid.Column="1" Height="28" HorizontalAlignment="Left" Margin="31,75,0,0" Name="label3" VerticalAlignment="Top" /> 24 <Label Content="密 码:" Grid.Column="1" Height="28" HorizontalAlignment="Left" Margin="32,105,0,0" Name="label4" VerticalAlignment="Top" /> 25 <TextBox Grid.Column="2" Height="23" HorizontalAlignment="Left" Margin="13,17,0,0" Name="txtserverName" VerticalAlignment="Top" Width="120" /> 26 <TextBox Grid.Column="2" Height="23" HorizontalAlignment="Left" Margin="13,48,0,0" Name="txtdbName" VerticalAlignment="Top" Width="120" /> 27 <TextBox Grid.Column="2" Height="23" HorizontalAlignment="Left" Margin="13,75,0,0" Name="txtUserName" VerticalAlignment="Top" Width="120" /> 28 <TextBox Grid.Column="2" Height="23" HorizontalAlignment="Left" Margin="13,105,0,0" Name="txtUserPassword" VerticalAlignment="Top" Width="120" /> 29 </Grid> 30 <Button Content="保存" Height="23" HorizontalAlignment="Left" Margin="50,214,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" /> 31 <Button Content="运行" Height="23" HorizontalAlignment="Left" Margin="191,214,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> 32 <Button Content="退出" Height="23" HorizontalAlignment="Left" Margin="319,214,0,0" Name="button3" VerticalAlignment="Top" Width="75" Click="button3_Click" /> 33 </Grid> 34 </TabItem> 35 </TabControl> 36 </Grid> 37 </Window>
后台部分代码界面
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public MainWindow() 2 { 3 InitializeComponent(); 4 string serverName = Common.XMLConfig.GetDecryptoItem(Common.Constants.ConfigFileName, Common.Constants.DBSectionName, "ServerIP", ""); 5 string dbName = Common.XMLConfig.GetDecryptoItem(Common.Constants.ConfigFileName, Common.Constants.DBSectionName, "DBName", ""); 6 string UserName = Common.XMLConfig.GetDecryptoItem(Common.Constants.ConfigFileName, Common.Constants.DBSectionName, "UserName", ""); 7 string UserPassword = Common.XMLConfig.GetDecryptoItem(Common.Constants.ConfigFileName,Common.Constants.DBSectionName,"UserPassWord",""); 8 9 this.txtserverName.Text = serverName; 10 this.txtdbName.Text = dbName; 11 this.txtUserName.Text = UserName; 12 this.txtUserPassword.Text = UserPassword; 13 } 14 15 private void button2_Click(object sender, RoutedEventArgs e) 16 { 17 string serverName = this.txtserverName.Text; 18 string dbName = this.txtdbName.Text; 19 string UserName = this.txtUserName.Text; 20 string UserPassword = this.txtUserPassword.Text; 21 Common.XMLConfig.SetEncryptItem(Common.Constants.ConfigFileName, Common.Constants.DBSectionName, "ServerIP", serverName); 22 Common.XMLConfig.SetEncryptItem(Common.Constants.ConfigFileName, Common.Constants.DBSectionName, "DBName", dbName); 23 Common.XMLConfig.SetEncryptItem(Common.Constants.ConfigFileName, Common.Constants.DBSectionName, "UserName", UserName); 24 Common.XMLConfig.SetEncryptItem(Common.Constants.ConfigFileName, Common.Constants.DBSectionName, "UserPassWord", UserPassword); 25 MessageBox.Show("数据库配置已配置成功"); 26 } 27 28 private void button3_Click(object sender, RoutedEventArgs e) 29 { 30 this.Close(); 31 }
这样数据库配置界面就设计好了,下一节准备进行串口设置以及主从机设置