Esempio n. 1
0
        private void MessagesList_ItemClick(object sender, ItemClickEventArgs e)
        {
            Debug.WriteLine(((Message)e.ClickedItem).Content);

            Messages.Add(new Message(Guid.NewGuid().ToString(), Contacts[0], DateTime.Now));
            MessagesList.UpdateLayout();
            MessagesList.ScrollIntoView(Messages.Last());
        }
Esempio n. 2
0
        private void ScrollToBottom()
        {
            if (MessagesList.Items != null)
            {
                var selectedIndex = MessagesList.Items.Count - 1;
                if (selectedIndex < 0)
                {
                    return;
                }

                MessagesList.SelectedIndex = selectedIndex;
            }
            MessagesList.UpdateLayout();

            MessagesList.ScrollIntoView(MessagesList.SelectedItem);
        }
Esempio n. 3
0
        // Handles messages from bot.
        async Task ReadBotMessagesAsync(DirectLineClient client, string conversationId)
        {
            // Optionally set watermark - this is last message id seen by bot. It is for paging.
            string watermark = null;

            while (true)
            {
                // Get all messages returned by bot.
                var convActivities = await client.Conversations.GetActivitiesAsync(conversationId, watermark);

                watermark = convActivities?.Watermark;

                // Get messages from your bot - From.Name should match your Bot Handle.
                var messagesFromBotText = from x in convActivities.Activities
                                          where x.From.Name == botHandle
                                          select x;

                // Iterate through all messages.
                foreach (Activity message in messagesFromBotText)
                {
                    message.Text = userName + message.Text;
                    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                                                                                                () => {
                        // Add message to the list and update ListView source to display response on the UI.
                        if (!_messagesFromBot.Contains(message))
                        {
                            _messagesFromBot.Add(newActivity);                         // Adds user query to chat window.
                            _messagesFromBot.Add(message);                             // Adds bot reponse to chat window.
                        }
                        MessagesList.ItemsSource = _messagesFromBot;

                        // Auto-scrolls to last item in chat
                        MessagesList?.ScrollIntoView(MessagesList.Items[_messagesFromBot.Count - 1], ScrollIntoViewAlignment.Leading);
                    });
                }

                await Task.Delay(TimeSpan.FromSeconds(2)).ConfigureAwait(false);
            }
        }