UI设计方案:
在设计窗体UI之前,先要了解一些主要的接口和帮助类:
对于主窗的左侧列表,容器内的Item必须实现ILeftItem的接口,比如联系人、系统消息、群等,接口包含点击事件
public interface ILeftItem
{
void Click();
}
项目的最外围容器是MainWindow,他涵盖了第一级菜单、第二级菜单、内容区等
其中更新菜单是在类UIStatic中:
/// <summary>
/// 设置第一级"业务"菜单文本
/// </summary>
internal static void SetBussTabText(string str)
{
mainWindow.SetBussTabText(str);
} /// <summary>
/// 设置第一级"聊天"菜单文本
/// </summary>
/// <param name="str"></param>
internal static void SetHomeTabText(string str)
{
mainWindow.SetHomeTabText(str);
} /// <summary>
/// 更新子菜单
/// </summary>
internal static void SetHomeTabSubText()
{
mainWindow.SetHomeSubTabText();
}
SetHomeSubTabText是更新子菜单,在MainWindow中实现,代码形如:
public void SetHomeSubTabText()
{
Link lkChat = new Link();
lkChat.DisplayName = "Contact";
lkChat.Source = new Uri(@"/Pages/Home.xaml", UriKind.Relative); ;
tabHome.Links.Add(lkChat);
Link lkMucChat = new Link();
lkMucChat.DisplayName = "Group Chat";
lkMucChat.Source = new Uri(@"/Pages/MucHome.xaml", UriKind.Relative); ;
tabHome.Links.Add(lkMucChat);
......
}
登陆后,进入的UI是Home.xmal,UI初始化的逻辑是左侧的列表由本地数据库查询生成,形成初始左侧List UI,自动绑定数据源
LeftListView.ItemsSource = itemsList; 消息、IQ的监听事件也在该方法进行:
private void BuildUI()
{
UIStatic.mainHome = this; if (Config.IsCheckPresence)
{
XmppStatic.xmppCon.OnRosterStart += new ObjectHandler(xmppCon_OnRosterStart);
XmppStatic.xmppCon.OnRosterItem += new XmppClientConnection.RosterHandler(xmppCon_OnRosterItem);
XmppStatic.xmppCon.OnRosterEnd += new ObjectHandler(xmppCon_OnRosterEnd);
XmppStatic.xmppCon.OnPresence += new PresenceHandler(xmppCon_OnPresence);
}
XmppStatic.xmppCon.OnIq += new IqHandler(xmppCon_OnIq);
XmppStatic.xmppCon.OnMessage += new MessageHandler(xmppCon_OnMessage); LoadListViewDataFromDB();
LeftListView.ItemsSource = itemsList; if (!Config.IsDebug)
{
LogTextBox.Visibility = System.Windows.Visibility.Hidden;
LogTextBox.Height = ;
}
}
当新消息到来,在HandleMessage中处理。
private void HandleMessage()
{
if (NewestMSG.Type == MessageType.chat)//单聊
{
SetChatPanelUI(new ChatRoom(NewestMSG.From.Bare, NewestMSG.From.Bare));
UpdateMessageArea();
}
else //系统消息
{
if (NewestMSG.Type == MessageType.groupchat) // MUC服务
{
SetChatPanelUI(new MUChatRoom(NewestMSG.From.Bare, NewestMSG.Body.ToString()));
UpdateMessageArea(); }
else if (NewestMSG.Type == MessageType.error) // Error
{ }
else //公告
{......
代码下载地址:https://lightchat.codeplex.com/