這節我們實作上節沒實作的糾結的進出房間的消息提示
我們清楚的知道,我們每個區都是一個使用者控件,我們的線上使用者進出時,得到聊天區域顯示資訊,這就涉及到使用者控件之間的消息傳遞了。
線上使用者區說:反正我是沒權直接去實時聊天區寫東西,沒辦法,找中介委托設定一下了。
那誰是中介呢?委托誰呢?當然是衣食父母index.xaml了,是它撐着整個家庭的。
好了,知道中介了,那我就簽個委托書了:
回到onlineuser.xaml.cs裡,簽寫全局的委托書[helpsetsystemmsg]:

public partial class onlineuser : usercontrol
{
public delegate void helpsetsystemmsg(string msg);
public event helpsetsystemmsg helpsetsystemmsgevent;
public onlineuser()
{
//..省略。。。
}
void client_notifyuserupdatereceived(object sender, gameservice.notifyuserupdatereceivedeventargs e)
void client_getplayerlistcompleted(object sender, gameservice.getplayerlistcompletedeventargs e)
}

ok,委托書有了,我隻管調了,其它事情讓index.xaml中介幫我搞定。
當有使用者進出的時候,此時會收到消息,然後委托人家幫我設定一下消息:

void client_notifyuserupdatereceived(object sender, gameservice.notifyuserupdatereceivedeventargs e)
//...省略一堆原有代碼...
helpsetsystemmsgevent("系統消息:"+e.player.nickname + (e.isenter?" 進入":"退出")+"房間");

好了,我這邊不管了,反正我按委托書上的辦,其它你們搞定了。
接着衣食父母這邊呢[index.xmal.cs]
收了人家的委托,自然要辦事了,以前沒人找我辦事,孩子都不管了,現在有人找了,孩子間要通訊,得提升一個高度管理才行:
于是,構造函數裡的執行個體化,全先調到全局定義了:

public partial class index : usercontrol
chess chesscontrol;
onlineuser onlineusercontrol;
chat chatcontrol;
public index()
initializecomponent();
chesscontrol=new chess();//執行個體化控件
chessboard.child = chesscontrol;//加載控件
onlineusercontrol = new onlineuser();//今天新加的線上使用者
onlineuserboard.child = onlineusercontrol;
chatcontrol = new chat();//也是今天新加的---看這裡看這裡兩行
chatboard.child = chatcontrol;

好了,得幫孩子做點事件了:

public partial class index : usercontrol
//..省略三行...
//..省略n行...
//下面為委托事件
onlineusercontrol.helpsetsystemmsgevent += new onlineuser.helpsetsystemmsg(onlineusercontrol_helpsetsystemmsgevent);
void onlineusercontrol_helpsetsystemmsgevent(string msg)
chatcontrol.addmsg(msg);

看,兩行代碼搞寫,在onlineuser的委托中,調用了chat的addmsg[我有先見之明,是以手癢定義了這個方法]
好了,到此,index.xaml的事也做完了
至于chatcontrol,上節不知覺的實作了addmsg方法,剛好對口可以被調用,省事去另寫方法。
ok,少不了f5運作看看效果:
看,消息提示了,ok,本小節就到此了,關于控件消息傳遞,以後章節會經常用到,大夥看仔細點。
版權聲明:本文原創發表于部落格園,作者為路過秋天,原文連結:
http://www.cnblogs.com/cyq1162/archive/2010/07/24/1784374.html