Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
修复
Browse files Browse the repository at this point in the history
  • Loading branch information
Yushu2606 committed Dec 2, 2023
1 parent d8a1fff commit da4bcab
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 22 deletions.
5 changes: 3 additions & 2 deletions Client/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ChatRoom.Utils;
using Microsoft.Extensions.Logging;
using System.CommandLine;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
Expand Down Expand Up @@ -46,7 +47,7 @@ private static async Task ChatMainAsync()
ip = Console.ReadLine();
try
{
await client.ConnectAsync(ip, 19132);
await client.ConnectAsync(IPAddress.TryParse(ip, out IPAddress address) ? address : IPAddress.Loopback, 19132);
}
catch (SocketException ex)
{
Expand Down Expand Up @@ -87,7 +88,7 @@ private static async Task ChatMainAsync()
client = new(SocketType.Stream, ProtocolType.Tcp);
try
{
await client.ConnectAsync(ip, 19132);
await client.ConnectAsync(IPAddress.TryParse(ip, out IPAddress address) ? address : IPAddress.Loopback, 19132);
}
catch (SocketException)
{
Expand Down
22 changes: 22 additions & 0 deletions ClientUI/ChatBoxHeightConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Globalization;
using System.Windows.Data;

namespace ChatRoom;

class ChatBoxHeightConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double result = (double)value - 102;
if (result < 0)
{
return 0;
}
return result;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
22 changes: 22 additions & 0 deletions ClientUI/ChatBoxWidthConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Globalization;
using System.Windows.Data;

namespace ChatRoom;

class ChatBoxWidthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double result = (double)value - 20;
if (result < 0)
{
return 0;
}
return result;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
22 changes: 22 additions & 0 deletions ClientUI/InputBoxWidthConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Globalization;
using System.Windows.Data;

namespace ChatRoom;

class InputBoxWidthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double result = (double)value - 125;
if (result < 0)
{
return 0;
}
return result;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
16 changes: 11 additions & 5 deletions ClientUI/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
<Window x:Class="ChatRoom.MainWindow"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ChatRoom" x:Class="ChatRoom.MainWindow"
mc:Ignorable="d"
Title="聊天室" Height="450" Width="800" FontFamily="Segoe UI Emoji" SizeChanged="WindowSizeChanged">
Title="聊天室" Height="450" Width="800" FontFamily="Segoe UI Emoji">
<Window.Resources>
<local:ChatBoxHeightConverter x:Key="ChatBoxHeightConverter"/>
<local:ChatBoxWidthConverter x:Key="ChatBoxWidthConverter"/>
<local:InputBoxWidthConverter x:Key="InputBoxWidthConverter"/>
</Window.Resources>
<Grid>
<Grid x:Name="LoginGrid">
<TextBox x:Name="IPBox" Height="24" Width="100" Margin="0,-35,0,0" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<Button Content="连接" Height="24" Width="50" Margin="0,35,0,0" Click="ConnectAsync" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
<Grid x:Name="RoomGrid" IsEnabled="False" Visibility="Hidden">
<TextBox x:Name="NameBox" Height="24" Width="100" Margin="10,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBox x:Name="ChatBox" Height="310" Width="768" Margin="10,39,0,0" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" IsReadOnly="True" VerticalScrollBarVisibility="Auto"/>
<TextBox x:Name="InputBox" Height="48" Width="675" Margin="0,0,115,10" KeyDown="EnterButtonDown" TextWrapping="Wrap" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
<TextBox x:Name="ChatBox" Height="{Binding ActualHeight, Converter={StaticResource ChatBoxHeightConverter}, ElementName=RoomGrid, IsAsync=True, Mode=OneWay}" Width="{Binding ActualWidth, Converter={StaticResource ChatBoxWidthConverter}, ElementName=RoomGrid, IsAsync=True, Mode=OneWay}" Margin="10,39,0,0" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" IsReadOnly="True" VerticalScrollBarVisibility="Auto"/>
<TextBox x:Name="InputBox" Height="48" Margin="0,0,115,10" KeyDown="EnterButtonDown" TextWrapping="Wrap" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="{Binding ActualWidth, Converter={StaticResource InputBoxWidthConverter}, ElementName=RoomGrid, IsAsync=True, Mode=OneWay}"/>
<Button x:Name="SendButton" Content="发送" Height="48" Width="100" Margin="0,0,10,10" Click="Send" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
</Grid>
</Grid>
</Window>
</Window>
18 changes: 3 additions & 15 deletions ClientUI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Windows;
Expand Down Expand Up @@ -31,7 +32,7 @@ private async void ConnectAsync(object sender, RoutedEventArgs e)
Client = new(SocketType.Stream, ProtocolType.Tcp);
try
{
await Client.ConnectAsync(ip, 19132);
await Client.ConnectAsync(IPAddress.TryParse(ip, out IPAddress address) ? address : IPAddress.Loopback, 19132);
}
catch (SocketException ex)
{
Expand Down Expand Up @@ -76,7 +77,7 @@ await Dispatcher.InvokeAsync(() =>
Client = new(SocketType.Stream, ProtocolType.Tcp);
try
{
await Client.ConnectAsync(ip, 19132);
await Client.ConnectAsync(IPAddress.TryParse(ip, out IPAddress address) ? address : IPAddress.Loopback, 19132);
break;
}
catch (SocketException)
Expand Down Expand Up @@ -149,17 +150,4 @@ private void EnterButtonDown(object sender, KeyEventArgs e)
Send(default, default);
}
}

private void WindowSizeChanged(object sender, SizeChangedEventArgs e)
{
if (e.WidthChanged)
{
ChatBox.Width = e.NewSize.Width - 36 < 0 ? 0 : e.NewSize.Width - 36;
InputBox.Width = e.NewSize.Width - 141 < 0 ? 0 : e.NewSize.Width - 141;
}
if (e.HeightChanged)
{
ChatBox.Height = e.NewSize.Height - 140 < 0 ? 0 : e.NewSize.Height - 140;
}
}
}

0 comments on commit da4bcab

Please sign in to comment.