first commit
This commit is contained in:
38
Yapper/MVVM/Core/RelayCommand.cs
Normal file
38
Yapper/MVVM/Core/RelayCommand.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace YapperClient.MVVM.Core
|
||||
{
|
||||
class RelayCommand : ICommand
|
||||
{
|
||||
private Action<object> execute;
|
||||
private Func<object, bool> canExecute;
|
||||
|
||||
public event EventHandler CanExecuteChanged
|
||||
{
|
||||
add { CommandManager.RequerySuggested += value; }
|
||||
remove { CommandManager.RequerySuggested -= value;}
|
||||
}
|
||||
|
||||
public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
|
||||
{
|
||||
this.execute = execute;
|
||||
this.canExecute = canExecute;
|
||||
}
|
||||
|
||||
public bool CanExecute(object parameter)
|
||||
{
|
||||
return this.canExecute == null || this.canExecute(parameter);
|
||||
}
|
||||
|
||||
public void Execute(object parameter)
|
||||
{
|
||||
this.execute(parameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Yapper/MVVM/Model/UserModel.cs
Normal file
14
Yapper/MVVM/Model/UserModel.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace YapperClient.MVVM.Model
|
||||
{
|
||||
class UserModel
|
||||
{
|
||||
public string UserName { get; set; }
|
||||
public string UID { get; set; }
|
||||
}
|
||||
}
|
||||
53
Yapper/MVVM/View/MainWindow.xaml
Normal file
53
Yapper/MVVM/View/MainWindow.xaml
Normal file
@@ -0,0 +1,53 @@
|
||||
<Window x:Class="Yapper.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:Yapper"
|
||||
xmlns:viewmodel="clr-namespace:YapperClient.MVVM.ViewModel"
|
||||
mc:Ignorable="d"
|
||||
Title="MainWindow" Height="450" Width="800">
|
||||
|
||||
<Window.DataContext>
|
||||
<viewmodel:MainViewModel/>
|
||||
</Window.DataContext>
|
||||
|
||||
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="200"/>
|
||||
<ColumnDefinition />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<DockPanel>
|
||||
<TextBox Height="25"
|
||||
DockPanel.Dock="Top"
|
||||
Text="{Binding UserName, UpdateSourceTrigger=PropertyChanged}"
|
||||
/>
|
||||
<Button Height="25"
|
||||
DockPanel.Dock="Top"
|
||||
Content="connect"
|
||||
Command="{Binding ConnectToServerCommand}"
|
||||
/>
|
||||
<ListView
|
||||
ItemsSource="{Binding Users}"
|
||||
>
|
||||
<ListView.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Text="{Binding UserName}"/>
|
||||
</DataTemplate>
|
||||
</ListView.ItemTemplate>
|
||||
</ListView>
|
||||
</DockPanel>
|
||||
|
||||
<StackPanel Grid.Column="1">
|
||||
<ListView Height="380"></ListView>
|
||||
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<TextBox Height="55" Width="545" VerticalContentAlignment="Center"></TextBox>
|
||||
<Button Width="55" Content="send"/>
|
||||
</StackPanel>
|
||||
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Window>
|
||||
24
Yapper/MVVM/View/MainWindow.xaml.cs
Normal file
24
Yapper/MVVM/View/MainWindow.xaml.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace Yapper
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWindow.xaml
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
43
Yapper/MVVM/ViewModel/MainViewModel.cs
Normal file
43
Yapper/MVVM/ViewModel/MainViewModel.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using YapperClient.MVVM.Core;
|
||||
using YapperClient.MVVM.Model;
|
||||
using YapperClient.Net;
|
||||
|
||||
namespace YapperClient.MVVM.ViewModel
|
||||
{
|
||||
class MainViewModel
|
||||
{
|
||||
public ObservableCollection<UserModel> Users { get; set; }
|
||||
public RelayCommand ConnectToServerCommand { get; set; }
|
||||
|
||||
public string UserName { get; set; }
|
||||
|
||||
private Server _server;
|
||||
public MainViewModel() {
|
||||
Users = new ObservableCollection<UserModel>();
|
||||
_server = new Server();
|
||||
_server.connectedEvent += UserConnected;
|
||||
ConnectToServerCommand = new RelayCommand(o => _server.ConnectToServer(UserName), o => !string.IsNullOrEmpty(UserName));
|
||||
}
|
||||
|
||||
private void UserConnected()
|
||||
{
|
||||
var user = new UserModel
|
||||
{
|
||||
UserName = _server.PacketReader.ReadMessage(),
|
||||
UID = _server.PacketReader.ReadMessage(),
|
||||
};
|
||||
|
||||
if (!Users.Any(x => x.UID == user.UID))
|
||||
{
|
||||
Application.Current.Dispatcher.Invoke(() => Users.Add(user));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user