A Chat window

This commit is contained in:
Paul Schneider 2016-10-10 12:58:14 +02:00
commit c88fc39a29
11 changed files with 137 additions and 1 deletions

View file

@ -0,0 +1,54 @@
using Microsoft.AspNet.SignalR.Client;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using Xamarin.Forms;
namespace BookAStar.Pages
{
public partial class ChatPage : ContentPage
{
public ObservableCollection<string> Messages { get; set; }
public string ChatUser { get; set; }
private HubConnection chatHubConnection;
private IHubProxy chatHubProxy;
public ChatPage()
{
InitializeComponent();
Title = "Chat";
sendButton.Clicked += async (sender, args) => {
IsBusy = true;
try
{
await chatHubProxy.Invoke<string>("Send", ChatUser, messageEntry.Text);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
IsBusy = false;
};
messageList.ItemsSource = Messages = new ObservableCollection<string>();
}
protected override async void OnAppearing()
{
IsBusy = true;
chatHubConnection = new HubConnection(Constants.SignalRHubsUrl);
chatHubProxy = chatHubConnection.CreateHubProxy("ChatHub");
chatHubProxy.On<string, string>("AddMessage", (n, m) => {
Messages.Add(string.Format("{0} says: {1}", n, m));
});
await chatHubConnection.Start();
IsBusy = false;
sendButton.IsEnabled = true;
}
}
}