今天周末,加班做一個監控系統,涉及到了需要配置的部分,比如,需要配置TCP端口、完成端口的工作者線程個數、是否開啟加密等配置信息。在XcodeFactory1.1以前的版本中有自動生成XML配置文件和對應的解析類的功能,很好。但是,我還是要自己寫個UI界面來顯示和修改XML配置文件中的內容,這就很乏味了,把幾個控件在窗體上拖來拖去,排放整齊,加個“確定”按鈕和“取消”按鈕,然后寫處理函數,真是無聊。
這種事情應該交給工具來自動生成,于是我決定讓XCodeFactory來為我自動生成這個配置窗體。我首先定義了配置窗體的基礎接口,看起來像這個樣子。
public interface IConfigForm
{
void Initialize(string configPath) ; //初始化xml配置文件解析類
void SaveConfig() ; //將配置內容寫入配置文件
void DisplayConfig() ;//將配置文件的內容顯示到UI
}
XCodeFactory所要做的就是根據配置的具體內容生成IConfigForm的實現類,完全是很簡單的問題。花了將近兩個小時的時間,為XCodeFactory實現了這一功能,并將XCodeFactory版本更新為1.2 。
下面是XCodeFactory自動生成窗體的界面截圖(沒有經過任何改動哦):
生成的主要代碼如下:
#region Initialize
public void Initialize(string configPath)
{
this.theConfigParser = new MonitorSystemConfigParser(configPath) ;
this.DisplayConfig() ;
}
#endregion
#region DisplayConfig
private void DisplayConfig()
{
this.textBox_Port.Text = this.theConfigParser.Port.ToString() ;
this.textBox_SmsSpan.Text = this.theConfigParser.SmsSpan.ToString() ;
this.textBox_SmsComNum.Text = this.theConfigParser.SmsComNum.ToString() ;
this.textBox_SmsRate.Text = this.theConfigParser.SmsRate.ToString() ;
this.checkBox_UserValidated.Checked = this.theConfigParser.UserValidated ;
}
#endregion
#region SaveConfig
private void SaveConfig()
{
this.theConfigParser.Port = int.Parse(this.textBox_Port.Text) ;
this.theConfigParser.SmsSpan = int.Parse(this.textBox_SmsSpan.Text) ;
this.theConfigParser.SmsComNum = int.Parse(this.textBox_SmsComNum.Text) ;
this.theConfigParser.SmsRate = int.Parse(this.textBox_SmsRate.Text) ;
this.theConfigParser.UserValidated = this.checkBox_UserValidated.Checked ;
}
#endregion
#region buttonClick
private void button_save_Click(object sender, System.EventArgs e)
{
this.SaveConfig() ;
MessageBox.Show("成功修改配置!") ;
this.Close() ;
}
private void button_cancel_Click(object sender, System.EventArgs e)
{
this.Close() ;
}
#endregion
真是太方便了,我對此功能相當滿意!以后的任何關于配置的問題都可以全部交給XCodeFactory來解決。
如果你想試試,可以email向我索取最新版本的XCodeFactory。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
