偶有闲时,看到了有人问起WinForm直接窗体参数的传递。回想起之前的牛X老师所教的方法,特写一小例子,共享之。
所谓观察者,就好比警察抓小偷时放出的眼线,眼线观察小偷,一有行动,立刻反应给警察。
现有FrmMain(警察),FrmChild1,FrmChild2(姑且当作两个小偷吧),一个定义委托和事件的类MiddleModule,它就是观察者。
且看代码:MiddleModule:using System;
using System.Collections.Generic;
using System.Text;namespace Observer
{
/// <summary>
/// 定义发布消息的委托
/// </summary>
/// <param name="sender">发布者</param>
/// <param name="msg">消息</param>
public delegate void Send(object sender, object msg);
/// <summary>
/// 观察者的中间模块组建
/// </summary>
public class MiddleModule
{
/// <summary>
///消息发布的事件
/// </summary>
public static event Send eventSend; public static void SendMessage(object sender, object msg)
{
if (eventSend != null)
{
eventSend(sender, msg);
}
}
}
}警察:
namespace WinObserver
{
public partial class FrmMain : Form
{
/// <summary>
/// 构造方法,在构造主窗体的时候就订阅来自FrmChild1的消息
/// </summary>
public FrmMain()
{
InitializeComponent();
MiddleModule.eventSend += new Send(MiddleModule_eventSend);
} /// <summary>
/// 接收FrmChild1的回传数据
/// </summary>
/// <param name="sender"></param>
/// <param name="msg"></param>
void MiddleModule_eventSend(object sender, object msg)
{
FrmChild1 frmChild1 = sender as FrmChild1;
if (null != frmChild1)
{
this.lblMsg.Text = "FrmChild1的回传数据:"+msg.ToString();
}
} private void btnShow1_Click(object sender, EventArgs e)
{
FrmChild1 frmChild1 = FrmChild1.CreateInstance();
frmChild1.Show();
} private void btnShow2_Click(object sender, EventArgs e)
{
FrmChild2 frmChild2 = FrmChild2.CreateInstance();
frmChild2.Show();
} private void btnSend_Click(object sender, EventArgs e)
{
//由Observer模块传递数据
if (String.IsNullOrEmpty(this.txtMsg.Text))
{
MiddleModule.SendMessage(this, String.Empty);
}
else
{
MiddleModule.SendMessage(this, this.txtMsg.Text);
}
}
}
}小偷1:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Observer;namespace WinObserver
{
public partial class FrmChild1 : Form
{
private static FrmChild1 frmChild1 = null; private FrmChild1()
{
InitializeComponent();
MiddleModule.eventSend += new Send(MiddleModule_eventSend);
}
void MiddleModule_eventSend(object sender, object msg)
{
FrmMain frmMain = sender as FrmMain;
if (null != frmMain)
{
this.lblMsg.Text = msg.ToString();
}
} public static FrmChild1 CreateInstance()
{
if (null == frmChild1)
{
frmChild1 = new FrmChild1();
}
return frmChild1;
} private void btnSendBack_Click(object sender, EventArgs e)
{
//由Observer模块传递数据
if (String.IsNullOrEmpty(this.txtMsg.Text))
{
MiddleModule.SendMessage(this, String.Empty);
}
else
{
MiddleModule.SendMessage(this, this.txtMsg.Text);
}
}
}
}小偷2:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Observer;namespace WinObserver
{
public partial class FrmChild2 : Form
{
private static FrmChild2 frmChild2 = null; /// <summary>
/// 接收来自FrmMain传递过来的数据
/// </summary>
private FrmChild2()
{
InitializeComponent();
MiddleModule.eventSend += new Send(MiddleModule_eventSend);
} void MiddleModule_eventSend(object sender, object msg)
{
FrmMain frmMain = sender as FrmMain;
if (null != frmMain)
{
this.lblMsg.Text = msg.ToString();
}
} public static FrmChild2 CreateInstance()
{
if (null == frmChild2)
{
frmChild2 = new FrmChild2();
}
return frmChild2;
}
}
}源代码,大家可以到http://download.csdn.net/source/1986872下载。
所谓观察者,就好比警察抓小偷时放出的眼线,眼线观察小偷,一有行动,立刻反应给警察。
现有FrmMain(警察),FrmChild1,FrmChild2(姑且当作两个小偷吧),一个定义委托和事件的类MiddleModule,它就是观察者。
且看代码:MiddleModule:using System;
using System.Collections.Generic;
using System.Text;namespace Observer
{
/// <summary>
/// 定义发布消息的委托
/// </summary>
/// <param name="sender">发布者</param>
/// <param name="msg">消息</param>
public delegate void Send(object sender, object msg);
/// <summary>
/// 观察者的中间模块组建
/// </summary>
public class MiddleModule
{
/// <summary>
///消息发布的事件
/// </summary>
public static event Send eventSend; public static void SendMessage(object sender, object msg)
{
if (eventSend != null)
{
eventSend(sender, msg);
}
}
}
}警察:
namespace WinObserver
{
public partial class FrmMain : Form
{
/// <summary>
/// 构造方法,在构造主窗体的时候就订阅来自FrmChild1的消息
/// </summary>
public FrmMain()
{
InitializeComponent();
MiddleModule.eventSend += new Send(MiddleModule_eventSend);
} /// <summary>
/// 接收FrmChild1的回传数据
/// </summary>
/// <param name="sender"></param>
/// <param name="msg"></param>
void MiddleModule_eventSend(object sender, object msg)
{
FrmChild1 frmChild1 = sender as FrmChild1;
if (null != frmChild1)
{
this.lblMsg.Text = "FrmChild1的回传数据:"+msg.ToString();
}
} private void btnShow1_Click(object sender, EventArgs e)
{
FrmChild1 frmChild1 = FrmChild1.CreateInstance();
frmChild1.Show();
} private void btnShow2_Click(object sender, EventArgs e)
{
FrmChild2 frmChild2 = FrmChild2.CreateInstance();
frmChild2.Show();
} private void btnSend_Click(object sender, EventArgs e)
{
//由Observer模块传递数据
if (String.IsNullOrEmpty(this.txtMsg.Text))
{
MiddleModule.SendMessage(this, String.Empty);
}
else
{
MiddleModule.SendMessage(this, this.txtMsg.Text);
}
}
}
}小偷1:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Observer;namespace WinObserver
{
public partial class FrmChild1 : Form
{
private static FrmChild1 frmChild1 = null; private FrmChild1()
{
InitializeComponent();
MiddleModule.eventSend += new Send(MiddleModule_eventSend);
}
void MiddleModule_eventSend(object sender, object msg)
{
FrmMain frmMain = sender as FrmMain;
if (null != frmMain)
{
this.lblMsg.Text = msg.ToString();
}
} public static FrmChild1 CreateInstance()
{
if (null == frmChild1)
{
frmChild1 = new FrmChild1();
}
return frmChild1;
} private void btnSendBack_Click(object sender, EventArgs e)
{
//由Observer模块传递数据
if (String.IsNullOrEmpty(this.txtMsg.Text))
{
MiddleModule.SendMessage(this, String.Empty);
}
else
{
MiddleModule.SendMessage(this, this.txtMsg.Text);
}
}
}
}小偷2:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Observer;namespace WinObserver
{
public partial class FrmChild2 : Form
{
private static FrmChild2 frmChild2 = null; /// <summary>
/// 接收来自FrmMain传递过来的数据
/// </summary>
private FrmChild2()
{
InitializeComponent();
MiddleModule.eventSend += new Send(MiddleModule_eventSend);
} void MiddleModule_eventSend(object sender, object msg)
{
FrmMain frmMain = sender as FrmMain;
if (null != frmMain)
{
this.lblMsg.Text = msg.ToString();
}
} public static FrmChild2 CreateInstance()
{
if (null == frmChild2)
{
frmChild2 = new FrmChild2();
}
return frmChild2;
}
}
}源代码,大家可以到http://download.csdn.net/source/1986872下载。
{
m_PicCha = new PictureCharacterTable(m_dbConn);//图片窗体
m_frmProperty = new Frm_Property(m_dbConn, m_PicCha, m_picShow);// 属性窗体
m_fileshow = new Frm_FileShowing(m_frmProperty, m_picShow);//文件显示窗体
m_folder = new Frm_Folder(m_fileshow, m_dbConn, m_frmProperty, m_PicCha);//文件夹窗 体
}
有何不好?
理解有误,以为有个static 就不同了。偶试一下了
{
/// <summary>
///消息发布的事件
/// </summary>
public static event Send eventSend; public static void SendMessage(object sender, object msg)
{
if (eventSend != null)
{
eventSend(sender, msg);
}
}
}这个类在每一个子窗口中这样加托管MiddleModule.eventSend += new Send(MiddleModule_eventSend);
是不是每次都生成一个新的实例啊?
还有每个子窗口中有这样一句private static FrmChild1 frmChild1 = null;
会导致子窗口关闭后,再按主窗口的showchild时会报错。不明白为什么要储存这个子变量......d大家继续讨论啊~!
{
if (null == frmChild1 || frmChild1.IsDisposed)
{
frmChild1 = new FrmChild1();
}
return frmChild1;
}
public static FrmChild2 CreateInstance()
{
if (null == frmChild2 || frmChild2.IsDisposed)
{
frmChild2 = new FrmChild2();
}
return frmChild2;
}
1、他已经很熟悉了各种设计模式,更是了解了面向对象的本质,在特定的问题上都已经可以自由发挥和变通了。
2、另一类就是“吃不到葡萄说葡萄酸的”那类人了。所以我建议代码写的不怎么样的?或者说是平时在工作中维护产品的时候经常要去改旧的已有的组件的那些同志,我就建议你好好去学习下设计模式,对于这一块的资料,我相信HeadFirst一书应该算是不错的了,他不仅阐述了重要的常用的几种模式,更是把面向对象编程中的思想给提炼了一下。所以很值得去学习。还有一点就是:如果你新到一个公司,不要自以为在架构方面的认识还算不错就去对公司的产品架构指指点点,这样只能说明你还嫩,因为有的产品可能是N年前的东西,你用现在的软件工程思想去看,那自然是落后的,所以在你提出XXXX架构不合理的时候,希望你已经把重构过的架构拿出来,那样会比你在那乱指点来的有说服力。并且可以很快在你的team里奠定你的地位。所以送给眼高手低的同志一句话:“少说多做”