添加基础功能

This commit is contained in:
绪山七寻 2025-06-01 20:33:52 +08:00
parent 427ba1ee5d
commit c410cacc65
8 changed files with 186 additions and 33 deletions

7
chatclient/App.config Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net configSource="config/log4net.config" />
</configuration>

View File

@ -6,7 +6,8 @@
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:chatclient"
mc:Ignorable="d"
Title="LoginWindow" Height="600" Width="360">
Title="LoginWindow" Height="590" Width="360" MinHeight="590" MinWidth="360" MaxHeight="590" MaxWidth="360"
ResizeMode="NoResize">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
@ -14,24 +15,29 @@
<RowDefinition Height="2*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="20">
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="6*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Style="{StaticResource MaterialDesignHeadline3TextBlock}" Text="登录" HorizontalAlignment="Center"/>
<Grid Grid.Row="1" Margin="10" Width="280">
<StackPanel>
<CheckBox Visibility="Hidden"/>
<TextBox VerticalAlignment="Top" materialDesign:HintAssist.Hint="Text (validated)" Style="{StaticResource MaterialDesignOutlinedTextBox}"/>
<CheckBox Visibility="Hidden"/>
<PasswordBox VerticalContentAlignment="Bottom" materialDesign:HintAssist.Hint="Password"
materialDesign:TextFieldAssist.HasClearButton="False"
IsEnabled="{Binding ElementName=RevealPasswordOutlinedEnabled, Path=IsChecked}"
Style="{StaticResource MaterialDesignOutlinedRevealPasswordBox}"/>
<CheckBox x:Name="jiji" Content="记住我" />
</StackPanel>
</Grid>
<Grid Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="10">
<StackPanel>
<Grid>
<materialDesign:PackIcon Kind="LoginVariant" Width="48" Height="48" Margin="54,0,0,0" VerticalAlignment="Center"/>
<TextBlock Style="{StaticResource MaterialDesignHeadline3TextBlock}" Text="登录" HorizontalAlignment="Left" Margin="107,0,0,4"/>
</Grid>
<Grid Width="280">
<StackPanel>
<TextBox VerticalAlignment="Top" materialDesign:HintAssist.Hint="用户名" Text="{Binding UserName}" Style="{StaticResource MaterialDesignOutlinedTextBox}" Margin="0,2,0,5"/>
<PasswordBox VerticalContentAlignment="Bottom" materialDesign:HintAssist.Hint="密码"
materialDesign:TextFieldAssist.HasClearButton="False" Margin="0,10,0,5"
materialDesign:PasswordBoxAssist.Password="{Binding UserPassword}"
Style="{StaticResource MaterialDesignOutlinedRevealPasswordBox}"/>
<Grid>
<CheckBox x:Name="jzw" Content="记住我"/>
<TextBlock Text="{Binding LoginMsg}" Style="{StaticResource MaterialDesignBody2TextBlock}" HorizontalAlignment="Right" Foreground="#FFCC3333"/>
</Grid>
<Button Content="登录" Margin="0,20,0,0" Click="Login_Click"
IsEnabled="{Binding DataContext.ControlsEnabled, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"
Style="{StaticResource MaterialDesignRaisedButton}" ToolTip="登录账号" FontWeight="Normal"/>
</StackPanel>
</Grid>
</StackPanel>
</Grid>
</Grid>
</Window>

View File

@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
@ -11,18 +13,69 @@ using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net.Sockets;
using System.IO;
using log4net;
using log4net.Config;
namespace chatclient
{
/// <summary>
/// LoginWindow.xaml 的交互逻辑
/// </summary>
public partial class LoginWindow : Window
public partial class LoginWindow : Window,INotifyPropertyChanged
{
private static readonly ILog log = LogManager.GetLogger(typeof(LoginWindow));
public LoginWindow()
{
InitializeComponent();
this.DataContext = this;
}
public event PropertyChangedEventHandler? PropertyChanged;
private void Update (string UpdateName)
{
PropertyChangedEventHandler? handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(UpdateName));
}
}
private string? _LoginMsg;
public string UserName { get; set; } = "";
public string UserPassword { get; set; } = "";
public string? LoginMsg
{
get { return _LoginMsg; }
set
{
_LoginMsg = value;
Update("LoginMsg");
}
}
private void Login_Click(object sender, RoutedEventArgs e)
{
if (UserName == "")
{
LoginMsg = "用户名不能为空";
}
else if (UserPassword == "")
{
LoginMsg = "密码不能为空";
}
else
{
var LoginData = new
{
type = "login",
username = UserName,
password = UserPassword
};
string LoginLsonData = JsonSerializer.Serialize(LoginData);
byte[] dataBytes = Encoding.UTF8.GetBytes(LoginLsonData);
MainWindow.Client.Send(dataBytes);
}
}
}
}

View File

@ -6,22 +6,27 @@
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:chatclient"
mc:Ignorable="d"
Title="ChatWindow" Height="450" Width="800"
Title="ChatWindow" Height="450" Width="800" MinHeight="240" MinWidth="380"
Style="{StaticResource MaterialDesignWindow}">
<Grid>
<materialDesign:Card
>
<materialDesign:Card>
<TabControl VerticalContentAlignment="Bottom" materialDesign:ColorZoneAssist.Mode="PrimaryMid" Style="{StaticResource MaterialDesignNavigationRailTabControl}">
<materialDesign:NavigationRailAssist.FloatingContent>
<Button IsEnabled="{Binding DataContext.ControlsEnabled, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"
Style="{StaticResource MaterialDesignFloatingActionMiniLightButton}" Margin="12" ToolTip="name">
<Image Source="/user.png" />
</Button>
</materialDesign:NavigationRailAssist.FloatingContent>
<TabItem>
<TabItem.Header>
<StackPanel Width="auto" Height="auto">
<materialDesign:PackIcon Width="24" Height="24" HorizontalAlignment="Center" Kind="Cog" />
<TextBlock HorizontalAlignment="Center" Text="All Files" />
<materialDesign:PackIcon Width="24" Height="24" HorizontalAlignment="Center" Kind="Message" />
<TextBlock HorizontalAlignment="Center" Text="消息" />
</StackPanel>
</TabItem.Header>
<StackPanel>
<TextBlock Text="123"/>
</StackPanel>
<Grid>
</Grid>
</TabItem>
<TabItem>
<TabItem.Header>
@ -31,7 +36,7 @@
</StackPanel>
</TabItem.Header>
<StackPanel>
<TextBlock Text="这里没有东西..."/>
</StackPanel>
</TabItem>
</TabControl>

View File

@ -8,6 +8,13 @@ using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System;
using System.Security.Policy;
using log4net;
using log4net.Config;
namespace chatclient
{
@ -16,13 +23,50 @@ namespace chatclient
/// </summary>
public partial class MainWindow : Window
{
static string? receive;
private static readonly ILog log = LogManager.GetLogger(typeof(MainWindow));
public static Socket Client;
public MainWindow()
{
InitializeComponent();
log.Info("Hello World!");
this.Hide();
Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
log.Info("连接服务器 127.0.0.1:5555 ");
Client.Connect("127.0.0.1", 5555);
}
catch (Exception ex) {
log.Error(ex);
}
LoginWindow Login = new LoginWindow();
Login.Show();
Thread th = new Thread(Receive);
th.Start();
}
private void PackIcon_DpiChanged()
static void Receive()
{
byte[] buffer = new byte[1024];
try
{
while (true)
{
int num = Client.Receive(buffer);
if (num == 0) break;
receive = Encoding.UTF8.GetString(buffer, 0, num);
response(receive);
}
}
catch (Exception ex) {
log.Error(ex);
}
finally
{
Client.Close();
}
}
static void response(string msg) {
}
}

12
chatclient/chatapi.cs Normal file
View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace chatclient
{
internal class api
{
}
}

View File

@ -9,9 +9,14 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="log4net" Version="3.1.0" />
<PackageReference Include="MaterialDesignThemes" Version="5.2.1" />
<PackageReference Include="MaterialDesignThemes.MahApps" Version="5.2.1" />
<PackageReference Include="MaterialDesignThemes.Wpf" Version="1.0.1" />
</ItemGroup>
<ItemGroup>
<Resource Include="user.png" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<datePattern value="yyyyMM\\yyyyMMdd'_chat.log'" />
<encoding value="utf-8" />
<file value="..\\..\\log\\" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<staticLogFileName value="false" />
<param name="MaxSizeRollBackups" value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date %thread %-5level %logger]%ndc - %message%newline" />
</layout>
</appender>
<root>
<level value="All" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
</configuration>