protected override void OnAppearing() { base.OnAppearing(); ChatEntry.keepOpen = true; lblUsername.Text = _name; imgProfile.Source = _profile; getConversation(_userid).Wait(); MessagingCenter.Subscribe <object, NotificatonModel.RemoteNoti>(this, "NotificationRecieved", (object arg1, NotificatonModel.RemoteNoti arg2) => { var cm = new ChatModel { Incoming = true, Outgoing = false, msg_desc = arg2.msg, msg_datetime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") }; if (_list == null) { _list = new List <ChatModel>(); } _list.Add(cm); items = new ChatItemList(_list); flowlistview.FlowItemsSource = items.Items; var lastItem = flowlistview.FlowItemsSource.OfType <object>().Last(); Device.BeginInvokeOnMainThread(() => flowlistview.ScrollTo(lastItem, ScrollToPosition.End, false)); }); }
private void processMessasge(string obj) { try { var data = JObject.Parse(obj); var type = data["type"].ToString(); var sender_id = data["sender_id"].ToString(); var msg = data["msg"].ToString(); var reciever_id = data["reciever_id"].ToString(); if (!string.IsNullOrEmpty(sender_id)) { if (Convert.ToInt32(sender_id) != StaticDataModel.UserId) { if (type == "message") { _list.Add(new ChatModel { Incoming = true, Outgoing = false, msg_desc = msg }); items = new ChatItemList(_list); flowlistview.FlowItemsSource = items.Items; } } } } catch (Exception ex) { } }
private async Task getConversation(int u_id) { string ret = string.Empty; StaticMethods.ShowLoader(); Task.Factory.StartNew( // tasks allow you to use the lambda syntax to pass wor () => { _list = WebService.GetChatConversation(u_id); }).ContinueWith( t => { Device.BeginInvokeOnMainThread(() => { if (_list != null) { for (int i = 0; i < _list.Count; i++) { if (!string.IsNullOrEmpty(_list[i].profile_pic)) { _list[i].profile_pic = Constants.PRO_PIC_IMG_URL + _list[i].profile_pic; } else { _list[i].profile_pic = "defaultprofile.png"; } if (_list[i].users_id == StaticDataModel.UserId.ToString()) { _list[i].Outgoing = true; _list[i].Incoming = false; } else { _list[i].Incoming = true; _list[i].Outgoing = false; } //_list[i].profile_pic = Constants.PRO_PIC_IMG_URL + _list[i].profile_pic; } var list = _list.OrderBy(x => x.msg_id).ToList(); items = new ChatItemList(list); flowlistview.FlowItemsSource = items.Items; var lastItem = flowlistview.ItemsSource.OfType <object>().Last(); //System.Threading.Tasks.Task.Factory.StartNew(() => //{ flowlistview.ScrollTo(lastItem, ScrollToPosition.End, false); //}); } }); }, TaskScheduler.FromCurrentSynchronizationContext() ); }
void Send() { try { JObject jsonObject = new JObject(); jsonObject.Add("msg", txtComment.Text); jsonObject.Add("type", "message"); jsonObject.Add("sender_id", StaticDataModel.UserId); jsonObject.Add("reciever_id", _userid); var json = jsonObject.ToString(); connection.Send(json); cm = new ChatModel { Incoming = false, Outgoing = true, msg_desc = txtComment.Text , msg_datetime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") }; if (_list == null) { _list = new List <ChatModel>(); } _list.Add(cm); items = new ChatItemList(_list); flowlistview.FlowItemsSource = items.Items; var lastItem = flowlistview.FlowItemsSource.OfType <object>().Last(); Device.BeginInvokeOnMainThread(() => flowlistview.ScrollTo(lastItem, ScrollToPosition.End, false)); sendMessage().Wait(); } catch (Exception ex) { } }