From f4de02ec3847e2e3e1cd8fc2734a2bd73554185e Mon Sep 17 00:00:00 2001
From: XuShanQiXun <3401460572@qq.com>
Date: Sat, 7 Jun 2025 18:37:25 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B6=88=E6=81=AF=E5=A4=84?=
=?UTF-8?q?=E7=90=86=E4=B8=8E=E7=95=8C=E9=9D=A2=E7=BF=BB=E8=AF=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
在 `ChatDataModel.cs` 中添加了控件引用并重命名消息类型属性。
在 `TrayIconManager.cs` 中将菜单项文本翻译为中文并注释掉部分代码。
调整 `LoginWindow.xaml` 的窗口尺寸。
在 `MainWindow.xaml` 中添加了 `x:Name` 属性和数据触发器,改善了用户体验。
在 `MainWindow.xaml.cs` 中添加了用户登录时的欢迎消息逻辑,并统一了消息类型属性名称。
---
chatclient/Data/ChatDataModel.cs | 4 +++-
chatclient/Data/TrayIconManager.cs | 13 ++++++-------
chatclient/LoginWindow.xaml | 2 +-
chatclient/MainWindow.xaml | 14 ++++++++++----
chatclient/MainWindow.xaml.cs | 16 +++++++++++++---
5 files changed, 33 insertions(+), 16 deletions(-)
diff --git a/chatclient/Data/ChatDataModel.cs b/chatclient/Data/ChatDataModel.cs
index 27b02d2..255987a 100644
--- a/chatclient/Data/ChatDataModel.cs
+++ b/chatclient/Data/ChatDataModel.cs
@@ -6,6 +6,8 @@ using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows;
using System.Windows.Media.Imaging;
+using System.Windows.Controls;
+using System.Windows.Input;
namespace chatclient.Data
{
@@ -21,7 +23,7 @@ namespace chatclient.Data
///
/// 消息类型(文本、图片、文件、系统消息等)
///
- public required MessageType Type { get; set; } = MessageType.Text;
+ public required MessageType MsgType { get; set; } = MessageType.Text;
///
/// 消息发送者的头像图片
///
diff --git a/chatclient/Data/TrayIconManager.cs b/chatclient/Data/TrayIconManager.cs
index b4207c7..a0c34ca 100644
--- a/chatclient/Data/TrayIconManager.cs
+++ b/chatclient/Data/TrayIconManager.cs
@@ -51,13 +51,13 @@ namespace chatclient.Data
};
// 添加菜单项
- contextMenu.Items.Add(CreateMenuItem("Open Application", PackIconKind.WindowRestore, OpenApp_Click));
- contextMenu.Items.Add(CreateMenuItem("Settings", PackIconKind.Cog, Settings_Click));
+ contextMenu.Items.Add(CreateMenuItem("打开程序", PackIconKind.WindowRestore, OpenApp_Click));
+ contextMenu.Items.Add(CreateMenuItem("设置", PackIconKind.Cog, Settings_Click));
contextMenu.Items.Add(new Separator { Style = (Style)Application.Current.Resources["MaterialDesignLightSeparator"] });
- contextMenu.Items.Add(CreateMenuItem("Check for Updates", PackIconKind.Update, Updates_Click));
- contextMenu.Items.Add(CreateMenuItem("Help", PackIconKind.HelpCircle, Help_Click));
- contextMenu.Items.Add(new Separator { Style = (Style)Application.Current.Resources["MaterialDesignLightSeparator"] });
- contextMenu.Items.Add(CreateMenuItem("Exit", PackIconKind.Power, Exit_Click));
+ //contextMenu.Items.Add(CreateMenuItem("Check for Updates", PackIconKind.Update, Updates_Click));
+ //contextMenu.Items.Add(CreateMenuItem("Help", PackIconKind.HelpCircle, Help_Click));
+ //contextMenu.Items.Add(new Separator { Style = (Style)Application.Current.Resources["MaterialDesignLightSeparator"] });
+ contextMenu.Items.Add(CreateMenuItem("退出", PackIconKind.Power, Exit_Click));
return contextMenu;
}
@@ -109,7 +109,6 @@ namespace chatclient.Data
private void Exit_Click(object sender, RoutedEventArgs e)
{
- // 释放托盘图标资源
Dispose();
Application.Current.Shutdown();
}
diff --git a/chatclient/LoginWindow.xaml b/chatclient/LoginWindow.xaml
index eeaa12e..61fab8c 100644
--- a/chatclient/LoginWindow.xaml
+++ b/chatclient/LoginWindow.xaml
@@ -7,7 +7,7 @@
xmlns:local="clr-namespace:chatclient"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls" x:Class="chatclient.LoginWindow"
mc:Ignorable="d"
- Title="LoginWindow" Height="590" Width="360" MinHeight="590" MinWidth="360" MaxHeight="590" MaxWidth="360"
+ Title="LoginWindow" Height="590" Width="330" MinHeight="590" MinWidth="330" MaxHeight="590" MaxWidth="330"
ResizeMode="NoResize" Closing="Window_Closing">
diff --git a/chatclient/MainWindow.xaml b/chatclient/MainWindow.xaml
index af3ebf5..cce7b90 100644
--- a/chatclient/MainWindow.xaml
+++ b/chatclient/MainWindow.xaml
@@ -80,9 +80,9 @@
-
-
-
+
+
+
@@ -96,6 +96,12 @@
+
+
+
+
+
+
@@ -119,7 +125,7 @@
+ TextWrapping="Wrap" MinHeight="50" MaxHeight="100" Margin="5" BorderBrush="#00000000" CaretBrush="#00673AB7" SelectionBrush="#00B39DDB"/>
diff --git a/chatclient/MainWindow.xaml.cs b/chatclient/MainWindow.xaml.cs
index 34bef44..1c252b4 100644
--- a/chatclient/MainWindow.xaml.cs
+++ b/chatclient/MainWindow.xaml.cs
@@ -149,6 +149,17 @@ namespace chatclient
var loginWindow = Application.Current.Windows.OfType().FirstOrDefault();
loginWindow?.Close();
mainWindow.Activate();
+ var chatmessage = new ChatMessage
+ {
+ Sender = "System",
+ MsgType = MessageType.System,
+ Image = new BitmapImage(new Uri("pack://application:,,,/resource/user.png")),
+ Content = $"你好 {UserName} !",
+ Timestamp = DateTime.Now,
+ Alignment = HorizontalAlignment.Center,
+ SenderColor = new SolidColorBrush(Colors.Gray)
+ };
+ mainWindow?.Messages.Add(chatmessage);
}
});
log.Info($"用户 {UserName} 登录成功(token:{token})");
@@ -190,7 +201,7 @@ namespace chatclient
var chatmessage = new ChatMessage
{
Sender = chat.user ?? "未知用户",
- Type = MessageType.Text,
+ MsgType = MessageType.Text,
Image = new BitmapImage(new Uri(chat.image ?? "pack://application:,,,/resource/user.png", UriKind.Absolute)),
Content = chat.message ?? "(无内容)",
Timestamp = DateTime.Now,
@@ -205,7 +216,7 @@ namespace chatclient
var chatmessage = new ChatMessage
{
Sender = chat.user ?? "未知用户",
- Type = MessageType.Text,
+ MsgType = MessageType.Text,
Image = new BitmapImage(new Uri(chat.image ?? "pack://application:,,,/resource/user.png", UriKind.Absolute)),
Content = chat.message ?? "(无内容)",
Timestamp = chat.timestamp,
@@ -215,7 +226,6 @@ namespace chatclient
var mainWindow = Application.Current.Windows.OfType().FirstOrDefault();
mainWindow?.Messages.Add(chatmessage);
}
-
});
}
else