/// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isConference"></param>
        /// <returns></returns>
        public ChatViewModel GetChatViewModel(int id, bool isConference)
        {
            _currentChatId = id;
            _isCurrentConference = isConference;

            try
            {
                var currentMessages = _messagesCache.GetItems(id);

                string title = EntitiesHelpers.GetChatTitle(id, isConference);

                // Load only 30 first from cache.
                int finish = currentMessages.Count - MESSAGES_COUNT;
                if (finish < 0)
                    finish = 0;

                int count = MESSAGES_COUNT;
                if (currentMessages.Count < count)
                    count = currentMessages.Count;

                var filtered = new Message[count];// new List<Message>(MESSAGES_COUNT);
                for (int i = currentMessages.Count - 1, j = count - 1; i >= finish; i--, j--)
                    filtered[j] = currentMessages[i];

                _currentChatViewModel = new ChatViewModel(id, title, isConference, filtered, _imageCache);

                if (!currentMessages.Any()) // Get messages only first time for this chat.
                {
                    MessagesGet getMessages = null;
                    if (isConference)
                        getMessages = new MessagesGet(-1, id, 0, MESSAGES_COUNT, _UpdateCurrentMessagesList);
                    else
                        getMessages = new MessagesGet(id, -1, 0, MESSAGES_COUNT, _UpdateCurrentMessagesList);

                    getMessages.Execute();
                }
                else
                    _UpdateReadStatusOfMessages(currentMessages); // This method will be called in _UpdateCurrentMessagesList

                _currentChatViewModel.UpdateStatusInfo();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("GetChatViewModel failed: " + ex.Message);
            }

            return _currentChatViewModel;
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            string id = string.Empty;
            string title = string.Empty;

            _UpdateApplicationBar();

            try
            {
                if (NavigationContext.QueryString.TryGetValue("id", out id))
                {
                    _id = Convert.ToInt32(id);
                    _model = App.Current.EntityService.GetChatViewModel(_id, true);

                    // After chat creating it should be manually set.
                    if (NavigationContext.QueryString.TryGetValue("title", out title))
                        _model.Name = title;

                    this.DataContext = (object)_model;
                    _ScrollIntoView();
                    _model.Messages.CollectionChanged += Messages_CollectionChanged;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("OnNavigatedTo in GroupChatPage failed: " + ex.Message);
            }
        }
 public void CloseCurrentChat()
 {
     _currentChatId = -1;
     _currentChatViewModel = null;
 }