finished basics
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
xmlns:local="clr-namespace:Yapper"
|
||||
xmlns:viewmodel="clr-namespace:YapperClient.MVVM.ViewModel"
|
||||
mc:Ignorable="d"
|
||||
Title="MainWindow" Height="450" Width="800">
|
||||
Title="MainWindow" Height="474" Width="816">
|
||||
|
||||
<Window.DataContext>
|
||||
<viewmodel:MainViewModel/>
|
||||
@@ -41,11 +41,14 @@
|
||||
</DockPanel>
|
||||
|
||||
<StackPanel Grid.Column="1">
|
||||
<ListView Height="380"></ListView>
|
||||
<ListView Height="380"
|
||||
ItemsSource="{Binding Messages}"></ListView>
|
||||
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<TextBox Height="55" Width="545" VerticalContentAlignment="Center"></TextBox>
|
||||
<Button Width="55" Content="send"/>
|
||||
<TextBox Height="55" Width="545" VerticalContentAlignment="Center"
|
||||
Text="{Binding Message, UpdateSourceTrigger=PropertyChanged}"></TextBox>
|
||||
<Button Width="55" Content="send"
|
||||
Command="{Binding SendMessageCommand}"/>
|
||||
</StackPanel>
|
||||
|
||||
</StackPanel>
|
||||
|
||||
@@ -14,16 +14,40 @@ namespace YapperClient.MVVM.ViewModel
|
||||
class MainViewModel
|
||||
{
|
||||
public ObservableCollection<UserModel> Users { get; set; }
|
||||
public ObservableCollection<string> Messages { get; set; }
|
||||
public RelayCommand ConnectToServerCommand { get; set; }
|
||||
public RelayCommand SendMessageCommand { get; set; }
|
||||
|
||||
public string UserName { get; set; }
|
||||
public string Message { get; set; }
|
||||
|
||||
private Server _server;
|
||||
public MainViewModel() {
|
||||
Users = new ObservableCollection<UserModel>();
|
||||
Messages = new ObservableCollection<string>();
|
||||
|
||||
_server = new Server();
|
||||
_server.connectedEvent += UserConnected;
|
||||
_server.msgReceivedEvent += MessageReceived;
|
||||
_server.userDisconnectedEvent += RemoveUser;
|
||||
ConnectToServerCommand = new RelayCommand(o => _server.ConnectToServer(UserName), o => !string.IsNullOrEmpty(UserName));
|
||||
|
||||
SendMessageCommand = new RelayCommand(o => _server.SendMessageToServer(Message), o => !string.IsNullOrEmpty(Message));
|
||||
}
|
||||
|
||||
private void RemoveUser()
|
||||
{
|
||||
var uid = _server.PacketReader.ReadMessage();
|
||||
var user = Users.FirstOrDefault(x => x.UID == uid);
|
||||
|
||||
Application.Current.Dispatcher.Invoke(() => Users.Remove(user));
|
||||
}
|
||||
|
||||
|
||||
private void MessageReceived()
|
||||
{
|
||||
var msg = _server.PacketReader.ReadMessage();
|
||||
Application.Current.Dispatcher.Invoke(() => Messages.Add(msg));
|
||||
}
|
||||
|
||||
private void UserConnected()
|
||||
|
||||
Reference in New Issue
Block a user