UI不从异步事件更新

时间:2017-07-10 20:17:50

标签: c# user-interface asynchronous event-handling

我使用异步套接字客户端事件从服务器接收消息。 我从客户端DataIn(我的事件名称)收到消息并添加到列表框,但不会在UI上显示!

protected void WebSocketClientControl1_OnChatNotification(List < SocketUi > sender) {
  ClientScript.RegisterStartupScript(GetType(), "hwa", "javascript:__doPostBack('WebSocketClientControl1','')", true);
}

我无法使用(Response.Redirect&amp; Server.Transfer)。 这个2函数有错误运行时间。

我调用javaScript函数来显示消息,而不是在屏幕上发生。

1 个答案:

答案 0 :(得分:0)

我的套接字代码

    public delegate void OnChatNotification(List<SocketUi> sender);

public class WebSocketClientControl : System.Web.UI.Control, IPostBackDataHandler
{
    public static ClientService _internalClientService = new ClientService(DServerConfig.ServerAddress.ToString(),
        DServerConfig.ServerSoketPort);

    public event OnChatNotification OnChatNotification = delegate { };

    public WebSocketClientControl()
    {
        _internalClientService.OnChat += _internalClientService_OnChat;
    }

    public void Connect(long userID, SocketEnums.EntityType usertype, string username, string key)
    {
        _internalClientService.Connect(userID, usertype, username, key);
    }

    private void _internalClientService_OnChat(List<SocketUI.SocketUi> sender)
    {
        if (OnChatNotification != null)
            OnChatNotification(sender);
    }

    public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
    {

        String presentValue = postDataKey;
        String postedValue = postCollection[postDataKey];

        if (presentValue == null || !presentValue.Equals(postedValue))
        {
            return true;
        }
        return false;
    }

    public void RaisePostDataChangedEvent()
    {
    }

}

我的ui代码

    private void WebSocketClientControl1_OnChatNotification(List<SocketUI.SocketUi> sender)
    {
        foreach (SocketUi socketUi in sender)
        {
            switch (socketUi.DSocketType)
            {
                case SocketEnums.DSocketType.Chat:

                    foreach (SocketUI.tb_Chat chatUi in socketUi.Chats)
                    {

                                for (int i = 0; i < ASPxPageControl1.TabPages.Count; i++)
                                {
                                    if (ASPxPageControl1.TabPages[i].Name == "uxTabPage_" + _channels[i].ID.ToString())
                                    {
                                        switch (chatUi.ChatMessegeType)
                                        {
                                            case (int)Enums.ChatMessegeType.Message:
                                               SetNewMessageOnUi(HelperD.UiChat_To_Tb_Chat(chatUi));
                                                break;

                                            case (int)Enums.ChatMessegeType.Readed:

                                                break;

                                            case (int)Enums.ChatMessegeType.OnLinedUser:

                                                break;
                                            case (int)Enums.ChatMessegeType.OffLinedUser:

                                                break;

                                            case (int)Enums.ChatMessegeType.JoinChannle:
                                            case (int)Enums.ChatMessegeType.LeftChannle:
                                                GetUserChannel(chatUi.ChannelID.ID);
                                                break;
                                            case (int)Enums.ChatMessegeType.TypingUser:
                                                for (int j = 0; j < ASPxPageControl1.TabPages[i].Controls.Count; j++)
                                                {
                                                    if (ASPxPageControl1.TabPages[i].Controls[j] is System.Web.UI.WebControls.Label &&
                                                        ASPxPageControl1.TabPages[i].Controls[j].ID ==
                                                        "uxLabel_Status" + ASPxPageControl1.TabPages[i].DataItem.ToString())
                                                    {
                                                System.Web.UI.WebControls.Label uxLabel_StatusTemp = new System.Web.UI.WebControls.Label();
                                                        uxLabel_StatusTemp = (System.Web.UI.WebControls.Label)ASPxPageControl1.TabPages[i].Controls[j];
                                                        uxLabel_StatusTemp.Text = " درحال تایپ " + socketUi.UserName + "...";
                                                      //  timer1.Start();
                                                    }
                                                }
                                                break;

                                }//End For
                                 // listBox_Message.Items.Add("Me:=>" + chatUi.Message + "\n\r");
                            }
                        }
                    }//End For
                    break;

            }
        }
    }


    private void SetNewMessageOnUi(UiSideLanguage.Database.Chat.tb_Chat item)
    {
        ASPxTextBox_Message.Text = "";
        for (int i = 0; i < ASPxPageControl1.TabPages.Count; i++)
        {
            for (int j = 0; j < ASPxPageControl1.TabPages[i].Controls.Count; j++)
            {
                if (ASPxPageControl1.TabPages[i].Controls[j] is System.Web.UI.WebControls.ListBox && ASPxPageControl1.TabPages[i].Controls[j].ID == "uxListView_Chat" + ASPxPageControl1.TabPages[i].DataItem.ToString())
                {
                    System.Web.UI.WebControls.ListBox userListView = (System.Web.UI.WebControls.ListBox)ASPxPageControl1.TabPages[i].Controls[j];
                    System.Web.UI.WebControls.ListItem listViewItemTemp = new System.Web.UI.WebControls.ListItem();
                    if (item.AddUser.ID == Language.CacheEntity.CurrentUser.ID)
                    {
                        listViewItemTemp.Text = " :من " + item.Message;
                        //   listViewItemTemp.ForeColor = Color.DarkCyan;
                    }
                    else
                    {

                        listViewItemTemp.Text = item.AddUser.UserName + " : " + item.Message;
                        //    listViewItemTemp.ForeColor = Color.DarkRed;
                    }

                    listViewItemTemp.Value = item.MessageFlagID.ToString();

                    System.Web.UI.WebControls.ListItem isHaveChatItem = null;
                    foreach (System.Web.UI.WebControls.ListItem chatitemListView in userListView.Items)
                    {
                        if (Language.HelperD.GetLong(chatitemListView.Value) == item.MessageFlagID)
                        {
                            isHaveChatItem = chatitemListView;
                            break;

                        }
                    }
                    if (isHaveChatItem != null)
                    {
                        if (item.AddUser.ID == Language.CacheEntity.CurrentUser.ID)
                        {
                            isHaveChatItem.Text = " :من " + item.Message;
                            //   isHaveChatItem.ForeColor = Color.DarkCyan;
                        }
                        else
                        {

                            isHaveChatItem.Text = item.ToUser.UserName + " : " + item.Message;
                            // isHaveChatItem.ForeColor = Color.DarkRed;
                        }

                        isHaveChatItem.Value = item.MessageFlagID.ToString();
                    }
                    else
                    {
                        userListView.Items.Add(listViewItemTemp);
                    }


                }
            }
        }

    }

此功能适用于更新用户界面&gt;&gt;&gt; SetNewMessageOnUi

我在运行时创建对象。

此代码已经完成 userListView.Items.Add(listViewItemTemp); 和ListView有项目但在UI上没有设置。 UpdatePanle中的所有对象