优化用户登录和聊天消息处理逻辑
在 `chatapi.cs` 中为 `LoginResultData` 添加 `username` 属性。 更新 `MainWindow.xaml` 中的 `TextBlock` 绑定属性为 `UserName`,并调整头像的 `Margin` 属性。 在 `MainWindow.xaml.cs` 中优化登录成功后的 `UserName` 赋值逻辑,改进聊天消息的创建方式,注释掉旧逻辑,更新默认消息内容为 `(无内容)`,提升代码可读性和用户体验。
This commit is contained in:
parent
c3496de067
commit
e82ae53a42
@ -20,6 +20,7 @@ namespace chatclient.Data
|
||||
public string? status { get; set; }
|
||||
public string? message { get; set; }
|
||||
public string? token { get; set; }
|
||||
public string? username { get; set; }
|
||||
}
|
||||
internal class SignData
|
||||
{
|
||||
|
@ -14,10 +14,10 @@
|
||||
<TabControl VerticalContentAlignment="Bottom" materialDesign:ColorZoneAssist.Mode="PrimaryMid" Style="{StaticResource MaterialDesignNavigationRailTabControl}">
|
||||
<materialDesign:NavigationRailAssist.FloatingContent>
|
||||
<StackPanel>
|
||||
<Button Style="{StaticResource MaterialDesignFloatingActionMiniLightButton}" Margin="12" >
|
||||
<Button Style="{StaticResource MaterialDesignFloatingActionMiniLightButton}" Margin="12">
|
||||
<Image Source="/resource/user.png" />
|
||||
</Button>
|
||||
<TextBlock Text="{Binding Username}"/>
|
||||
<TextBlock Text="{Binding UserName}" TextAlignment="Center" MaxWidth="64" MaxHeight="64" TextWrapping="Wrap"/>
|
||||
</StackPanel>
|
||||
</materialDesign:NavigationRailAssist.FloatingContent>
|
||||
<TabItem>
|
||||
@ -69,7 +69,7 @@
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<!-- 左侧头像(仅当Alignment=Left时显示) -->
|
||||
<Border x:Name="leftAvatar" Grid.Column="0" Width="40" Height="40" Margin="10,0,10,0" CornerRadius="20" VerticalAlignment="Top">
|
||||
<Border x:Name="leftAvatar" Grid.Column="0" Width="40" Height="40" Margin="10,0,5,0" CornerRadius="20" VerticalAlignment="Top">
|
||||
<!-- 图片头像 -->
|
||||
<Image Source="{Binding Image}" Stretch="UniformToFill" HorizontalAlignment="Center" VerticalAlignment="Center">
|
||||
<Image.Clip>
|
||||
@ -86,7 +86,7 @@
|
||||
</StackPanel>
|
||||
</materialDesign:Card>
|
||||
<!-- 右侧头像(仅当Alignment=Right时显示) -->
|
||||
<Border x:Name="rightAvatar" Grid.Column="2" Width="40" Height="40" Margin="10,0,10,0" CornerRadius="20" VerticalAlignment="Top">
|
||||
<Border x:Name="rightAvatar" Grid.Column="2" Width="40" Height="40" Margin="5,0,10,0" CornerRadius="20" VerticalAlignment="Top">
|
||||
<Image Source="{Binding Image}" Stretch="UniformToFill" HorizontalAlignment="Center" VerticalAlignment="Center">
|
||||
<Image.Clip>
|
||||
<EllipseGeometry Center="20,20" RadiusX="20" RadiusY="20"/>
|
||||
|
@ -135,6 +135,7 @@ namespace chatclient
|
||||
if (LoginResponse!.status == "success" && LoginResponse != null)
|
||||
{
|
||||
token = LoginResponse.token;
|
||||
UserName = LoginResponse.username ?? "Unnamed";
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
var mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
|
||||
@ -181,18 +182,37 @@ namespace chatclient
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
// 处理聊天消息
|
||||
if (chat.user == UserName)
|
||||
{
|
||||
var chatmessage = new ChatMessage
|
||||
{
|
||||
Sender = chat.user ?? "未知用户",
|
||||
Type = MessageType.Text,
|
||||
Image = new BitmapImage(new Uri(chat.image ?? "pack://application:,,,/resource/user.png", UriKind.Absolute)),
|
||||
Content = chat.message ?? "无内容",
|
||||
Content = chat.message ?? "(无内容)",
|
||||
Timestamp = DateTime.Now,
|
||||
Alignment = HorizontalAlignment.Right,
|
||||
SenderColor = new SolidColorBrush(Colors.Blue)
|
||||
};
|
||||
var mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
|
||||
mainWindow?.Messages.Add(chatmessage);
|
||||
}
|
||||
else
|
||||
{
|
||||
var chatmessage = new ChatMessage
|
||||
{
|
||||
Sender = chat.user ?? "未知用户",
|
||||
Type = MessageType.Text,
|
||||
Image = new BitmapImage(new Uri(chat.image ?? "pack://application:,,,/resource/user.png", UriKind.Absolute)),
|
||||
Content = chat.message ?? "(无内容)",
|
||||
Timestamp = chat.timestamp,
|
||||
Alignment = HorizontalAlignment.Left,
|
||||
SenderColor = new SolidColorBrush(Colors.Black)
|
||||
};
|
||||
var mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
|
||||
mainWindow?.Messages.Add(chatmessage);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
else
|
||||
@ -234,21 +254,22 @@ namespace chatclient
|
||||
// 判断是否为群组,若是则收件人设为“所有人”,否则为联系人显示名
|
||||
//string recipient = contact?.IsGroup == true ? "所有人" : contact?.DisplayName;
|
||||
|
||||
// 弃用的方法
|
||||
// 创建新消息
|
||||
var newMessage = new ChatMessage
|
||||
{
|
||||
Sender = "我",
|
||||
Type = MessageType.Text,
|
||||
Image = new BitmapImage(new Uri("pack://application:,,,/resource/user.png", UriKind.Absolute)), // 默认头像
|
||||
Content = txtMessage.Text,
|
||||
Timestamp = DateTime.Now,
|
||||
Alignment = HorizontalAlignment.Right, // 自己发送的消息靠右
|
||||
SenderColor = new SolidColorBrush(Colors.Blue)
|
||||
};
|
||||
//var newMessage = new ChatMessage
|
||||
//{
|
||||
// Sender = "我",
|
||||
// Type = MessageType.Text,
|
||||
// Image = new BitmapImage(new Uri("pack://application:,,,/resource/user.png", UriKind.Absolute)), // 默认头像
|
||||
// Content = txtMessage.Text,
|
||||
// Timestamp = DateTime.Now,
|
||||
// Alignment = HorizontalAlignment.Right, // 自己发送的消息靠右
|
||||
// SenderColor = new SolidColorBrush(Colors.Blue)
|
||||
//};
|
||||
var newChatMessage = new ChatData
|
||||
{
|
||||
type = "chat",
|
||||
message = newMessage.Content
|
||||
message = txtMessage.Text
|
||||
};
|
||||
string ChatJsonData = JsonSerializer.Serialize(newChatMessage);
|
||||
byte[] dataBytes = Encoding.UTF8.GetBytes(ChatJsonData);
|
||||
@ -272,14 +293,10 @@ namespace chatclient
|
||||
log.Error(ex);
|
||||
Client.Close();
|
||||
}
|
||||
//finally
|
||||
//{
|
||||
//
|
||||
//}
|
||||
}
|
||||
}
|
||||
// 添加到消息列表
|
||||
Messages.Add(newMessage);
|
||||
//Messages.Add(newMessage);
|
||||
// 清空输入框
|
||||
txtMessage.Clear();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user