重构消息输入区域并优化异步发送逻辑
在 `MainWindow.xaml` 中,将消息输入区域替换为 `materialDesign:Card`,调整了 `TextBox` 和 `Button` 的布局,并新增了 `Snackbar` 用于信息提示。 在 `MainWindow.xaml.cs` 中,将 `SendMessage_Click` 和 `SendMessage` 方法修改为异步,增加了对 `Socket` 连接状态的检查,并在连接失败时提供用户提示。同时新增了 `QueueMessage` 方法以支持将消息添加到 `Snackbar` 的消息队列中。
This commit is contained in:
parent
e82ae53a42
commit
635eb14c9c
@ -112,21 +112,18 @@
|
||||
</ItemsControl>
|
||||
</ScrollViewer>
|
||||
|
||||
<Grid Grid.Row="2" Background="{DynamicResource MaterialDesign.Brush.Primary.Foreground}">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*"/>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<TextBox x:Name="txtMessage" Grid.Column="0"
|
||||
materialDesign:HintAssist.Hint="输入消息..."
|
||||
AcceptsReturn="True" VerticalScrollBarVisibility="Auto"
|
||||
TextWrapping="Wrap" MinHeight="60" MaxHeight="120" Margin="5"/>
|
||||
|
||||
<Button x:Name="btnSend" Grid.Column="1" Content="发送" MinWidth="80"
|
||||
Style="{StaticResource MaterialDesignRaisedButton}"
|
||||
Click="SendMessage_Click"/>
|
||||
</Grid>
|
||||
<materialDesign:Card materialDesign:ElevationAssist.Elevation="Dp8" Grid.Row="2">
|
||||
<Grid Margin="5,0,5,0">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="*"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
<TextBox x:Name="txtMessage" Grid.Row="0" materialDesign:HintAssist.Hint="输入消息..." AcceptsReturn="True" VerticalScrollBarVisibility="Auto"
|
||||
TextWrapping="Wrap" MinHeight="50" MaxHeight="100" Margin="5" BorderBrush="#00000000"/>
|
||||
<Button x:Name="btnSend" Grid.Row="1" Content="发送" MinWidth="80" Style="{StaticResource MaterialDesignRaisedButton}"
|
||||
Click="SendMessage_Click" Width="20" HorizontalAlignment="Right" Margin="2,2,4,4"/>
|
||||
</Grid>
|
||||
</materialDesign:Card>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</TabItem>
|
||||
@ -143,5 +140,7 @@
|
||||
</TabItem>
|
||||
</TabControl>
|
||||
</materialDesign:Card>
|
||||
<!--信息框-->
|
||||
<materialDesign:Snackbar x:Name="SnackbarThree" MessageQueue="{materialDesign:MessageQueue}" />
|
||||
</Grid>
|
||||
</Window>
|
||||
|
@ -240,11 +240,11 @@ namespace chatclient
|
||||
log.Error("处理响应时发生错误", ex);
|
||||
}
|
||||
}
|
||||
private void SendMessage_Click(object sender, RoutedEventArgs e)
|
||||
private async void SendMessage_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
SendMessage();
|
||||
await SendMessage();
|
||||
}
|
||||
private void SendMessage()
|
||||
private async Task SendMessage()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(txtMessage.Text))
|
||||
return;
|
||||
@ -274,31 +274,53 @@ namespace chatclient
|
||||
string ChatJsonData = JsonSerializer.Serialize(newChatMessage);
|
||||
byte[] dataBytes = Encoding.UTF8.GetBytes(ChatJsonData);
|
||||
log.Info($"向服务器聊天信息(长度:{dataBytes.Length})");
|
||||
if (Client != null)
|
||||
// 检查Socket是否可用
|
||||
if (Client?.Connected == true)
|
||||
{
|
||||
if (Client.Connected)
|
||||
Client.Send(dataBytes);
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
Client.Send(dataBytes);
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
log.Info("未连接服务器,尝试连接");
|
||||
Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
Client.Connect(IPAddress.Parse(Server.ServerIP), Server.ServerPort);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
log.Error(ex);
|
||||
Client.Close();
|
||||
}
|
||||
}
|
||||
txtMessage.Clear();
|
||||
});
|
||||
return;
|
||||
}
|
||||
log.Info("未连接服务器,尝试异步连接");
|
||||
// 异步连接操作
|
||||
await Task.Run(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
Client?.Connect(IPAddress.Parse(Server.ServerIP), Server.ServerPort);
|
||||
StartReceive();
|
||||
Client?.Send(dataBytes);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
log.Error($"连接失败: {ex.Message}");
|
||||
Client?.Close();
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
var mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
|
||||
if (mainWindow != null)
|
||||
{
|
||||
QueueMessage("连接失败,请检查网络设置或服务器状态。");
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
// 添加到消息列表
|
||||
//Messages.Add(newMessage);
|
||||
// 清空输入框
|
||||
txtMessage.Clear();
|
||||
}
|
||||
private void QueueMessage(string message)
|
||||
{
|
||||
if (SnackbarThree.MessageQueue is { } messageQueue)
|
||||
{
|
||||
//use the message queue to send a message.
|
||||
//the message queue can be called from any thread
|
||||
Task.Factory.StartNew(() => messageQueue.Enqueue(message));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user