Merge branch 'client-csharp' of https://github.com/mattuylee/stormchat into client-csharp

This commit is contained in:
Mattuy
2018-12-20 16:53:16 +08:00
9 changed files with 232 additions and 93 deletions
+54 -57
View File
@@ -12,79 +12,76 @@ namespace StormChatWPF
class Chat class Chat
{ {
public static List<User> ContactsList;//联系人集合 public static List<User> ContactsList;//联系人集合
public static Chat chat = new Chat(); public static Chat chat=new Chat();
private Chat() private Chat()
{ {
StormClient.OnLoginDone += HaveLogin; StormClient.OnLoginDone += OnHaveLogin;
StormClient.OnGetUserListDone += GetContactsList; StormClient.OnGetUserListDone += OnGetContactsList;
StormClient.OnMessage +=ReciveMessage; StormClient.OnMessage +=OnMessage;
} }
internal static User CurrentContact { get; set; }//设置当前联系人对象
/// <summary>
private User ChatNow; /// 获取联系人列表(完成后打开主窗体)
internal User SetContact /// </summary>
{ private void OnGetContactsList(ResultHead head, User[] users)
get
{
return ChatNow;
}
set
{
ChatNow = value;
}
}//设置当前联系人对象
internal static void GetContactsList(ResultHead head, User[] users)
{ {
if (head.Error != "") if (head.Error != "")
{ {
MessageBox.Show("无法获取联系人列表"); MessageBox.Show("无法获取联系人列表");
}
else
ContactsList =new List<User>( users);
return; return;
}//获取联系人列表 }
internal void Log(string user,string password) App.Current.Dispatcher.Invoke(
(Action)delegate ()
{
ContactsList = new List<User>(users);
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
LogWindow.Instence.Close();
});
}
private void OnHaveLogin(ResultHead head, User user)
{
if (head.Error == "")
{
User.Me = user;
StormClient.QueueGetUserList();
}
else
{
MessageBox.Show("请核对账号密码!");
}
}//登录完成
private void OnMessage(Message message)
{
if (MainWindow.Instence != null)
{
MainWindow.Instence.ShowMessage(message);
}
}//接受到新消息
internal void SendMessage(string text,User target)
{
Message msg = new Message(text,target);
Action<ResultHead> f = delegate (ResultHead head)
{
MainWindow.Instence.ShowMessage(msg);
};
StormClient.QueueSendMessage(msg,f);
}//发送消息
internal static void Log(string user, string password)
{ {
if (StormClient.Initialize()) if (StormClient.Initialize())
{ {
StormClient.QueueLogin(user,password); if (!StormClient.QueueLogin(user, password))
{
MessageBox.Show("登录失败");
}
} }
else else
{ {
MessageBox.Show("连接服务器失败!"); MessageBox.Show("连接服务器失败!");
} }
}//登录 }//登录
internal static void HaveLogin(ResultHead head, User user)
{
App.Current.Dispatcher.Invoke(
(Action)delegate ()
{
if (head.Error == "")
{
User.Me = user;
MainWindow userWindow = new MainWindow();
userWindow.Show();
LogWindow.Instence.Close();
}
else
{
MessageBox.Show("登录失败!");
}
});
}//登录完成
internal static void ReciveMessage(Message message)
{
LogWindow.Instence.ShowMessage(message);
}//接受到新消息的时候
internal static void SendMessage(string text,User target)
{
// Message msg = new Message(text);
// Action<BaseHead> f = delegate(BaseHead head)
// {
// };
// StormClient.QueueSendMessage(msg, target, f);
// LogWindow.Instence.ShowUserMessage();
}
} }
} }
+20 -8
View File
@@ -5,13 +5,25 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:StormChatWPF" xmlns:local="clr-namespace:StormChatWPF"
mc:Ignorable="d" mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"> Title="StormChat" Height="450" Width="800"
<Grid Margin="0,0,-0.4,-1"> Icon="UI/闪电.png">
<Image x:Name="LogImage" Margin="0,0,0,181" Source="/RESOURCES/logFormBackground.jpg" Stretch="None"/> <Grid Height="418" VerticalAlignment="Stretch" Margin="2,1,1,1">
<Button x:Name="Login_button" Content="LogIn" Margin="361,370,361,32" RenderTransformOrigin="0.664,1.51" Click="Login_button_Click"/> <Grid.ColumnDefinitions>
<TextBox x:Name="AccountBox" Margin="346,263,340,0" TextWrapping="Wrap" Text="201701110203" Height="15" VerticalAlignment="Top" /> <ColumnDefinition Width="200*"/>
<PasswordBox x:Name="passwordBox" Margin="346,311,0,0" Password="1233211234567" RenderTransformOrigin="0.503,0.59" Height="18" VerticalAlignment="Top" HorizontalAlignment="Left" Width="109"/> <ColumnDefinition Width="200*"/>
<ColumnDefinition Width="200*"/>
<ColumnDefinition Width="200*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="150*"/>
<RowDefinition Height="150*"/>
<RowDefinition Height="150*"/>
</Grid.RowDefinitions>
<Image x:Name="LogBackground" Grid.ColumnSpan="4" Height="268" Margin="10,10,10,0" Grid.RowSpan="2" VerticalAlignment="Top" Source="UI/登录界面底.png"/>
<Button x:Name="Login_button" Content="LogIn" Click="Login_button_Click" Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="2" Margin="160.6,69.6,160.6,44.6" Width="70" Height="25" HorizontalAlignment="Center"/>
<TextBox x:Name="AccountBox" TextWrapping="Wrap" Text="201701110203" Grid.Row="2" TextAlignment="Left" Margin="137.6,4.6,137.6,112.6" Grid.Column="1" Grid.ColumnSpan="2" Height="22" Width="122" HorizontalAlignment="Center" BorderThickness="0,0,0,1" VerticalAlignment="Top" />
<PasswordBox x:Name="passwordBox" Password="1233211234567" Margin="135,35,135,0" Grid.Column="1" Grid.Row="2" VerticalAlignment="Top" Grid.ColumnSpan="2" Height="22" Width="122" HorizontalAlignment="Center" BorderThickness="0,0,0,1" HorizontalContentAlignment="Left"/>
<Label x:Name="Account" Content="Account:" Grid.Column="1" Margin="0,138.8,70.2,0" Grid.Row="1" VerticalAlignment="Top" RenderTransformOrigin="0.415,0.379" Height="24" FontFamily="Comic Sans MS" FontStyle="Italic" Grid.RowSpan="2" HorizontalContentAlignment="Center" HorizontalAlignment="Right" Width="60"/>
<Label x:Name="Password" Content="Password:" Grid.Column="1" Margin="0,31.6,65.2,0" Grid.Row="2" VerticalAlignment="Top" FontFamily="Comic Sans MS" FontStyle="Italic" Height="24" HorizontalContentAlignment="Center" HorizontalAlignment="Right" Width="70"/>
</Grid> </Grid>
</Window> </Window>
+2 -7
View File
@@ -29,14 +29,9 @@ namespace StormChatWPF
} }
private void Login_button_Click(object sender, RoutedEventArgs e) private void Login_button_Click(object sender, RoutedEventArgs e)
{ {
Chat.chat.Log(AccountBox.Text,passwordBox.Password); Chat.Log(AccountBox.Text,passwordBox.Password);
} }
internal void ShowMessage(Message message)
{
}//将收到的消息展现于UI界面
internal void ShowUserMessage()
{
}
} }
} }
+19 -9
View File
@@ -9,14 +9,24 @@
> >
<Grid> <Grid>
<ListView x:Name="UsersList" HorizontalAlignment="Left" Width="201" Margin="0,64,0,10" IsEnabled="True" SelectionChanged="UsersList_SelectionChanged"> <Grid.RowDefinitions>
<ListView.View> <RowDefinition Height="60*"/>
<GridView> <RowDefinition Height="300*"/>
<GridViewColumn/> <RowDefinition Height="90*"/>
</GridView> </Grid.RowDefinitions>
</ListView.View> <Grid.ColumnDefinitions>
</ListView> <ColumnDefinition Width="200*"/>
<TextBox HorizontalAlignment="Left" Height="90" Margin="201,320,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="583" /> <ColumnDefinition Width="200*"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="378,262,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/> <ColumnDefinition Width="200*"/>
<ColumnDefinition Width="200*"/>
</Grid.ColumnDefinitions>
<ListView x:Name="UsersList" Margin="0,0,0,0" IsEnabled="True" SelectionChanged="UsersList_SelectionChanged" Grid.Row="1" Grid.RowSpan="2"/>
<TextBox x:Name="InputBox" Margin="0,0,0,0" TextWrapping="Wrap" Text="" Grid.Row="2" Grid.ColumnSpan="3" Grid.Column="1" />
<Button Content="Send" HorizontalAlignment="Right" Margin="0,0,0,0" VerticalAlignment="Bottom" Width="75" Click="Button_Click" Height="20" Grid.Row="2" Grid.Column="3"/>
<ScrollViewer Grid.ColumnSpan="3" Grid.Column="1" Margin="0,0,0,0" Grid.Row="1" >
<ScrollViewer.Content>
<StackPanel x:Name="OutBox" Margin="0,0,0,0" Grid.ColumnSpan="3" Grid.Column="1" Grid.Row="1" ScrollViewer.VerticalScrollBarVisibility="Hidden"/>
</ScrollViewer.Content>
</ScrollViewer>
</Grid> </Grid>
</Window> </Window>
+66 -12
View File
@@ -1,4 +1,5 @@
using System; using Interact;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@@ -19,34 +20,87 @@ namespace StormChatWPF
/// </summary> /// </summary>
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
public static MainWindow Instence;
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
LoadContactsList();
Instence = this;
} }
private void AddUserList() private void LoadContactsList()
{ {
if (Chat.userlist.Any()) if (Chat.ContactsList.Any())
{ {
foreach (var user in Chat.userlist) foreach (var user in Chat.ContactsList)
{ {
UsersList.Items.Add(user); UsersList.Items.Add(user.NickName);
} }
} }
}//向联系人列表listview中添加元素 }//向联系人列表listview中添加元素
private void UsersList_Selected(object sender, RoutedEventArgs e)
{
}
private void UsersList_SelectionChanged(object sender, SelectionChangedEventArgs e) private void UsersList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{ {
if (UsersList.SelectedItem != null)
{
Chat.CurrentContact = Chat.ContactsList[UsersList.SelectedIndex];
}
} }
private void Button_Click(object sender, RoutedEventArgs e) private void Button_Click(object sender, RoutedEventArgs e)
{ {
AddUserList(); Chat.chat.SendMessage(InputBox.Text,Chat.CurrentContact);
InputBox.Text = "";
} }
internal void ShowMessage(Message message)
{
if (message.To == User.Me)
{
App.Current.Dispatcher.Invoke(
delegate ()
{
TextBlock text = new TextBlock
{
MaxWidth = 400,
MinWidth = 10,
MinHeight = 30,
Text = message.Text,
TextWrapping = TextWrapping.Wrap,
FontFamily = new FontFamily("Comic Sans MS"),
HorizontalAlignment = HorizontalAlignment.Left
};
Label label = new Label
{
HorizontalAlignment = HorizontalAlignment.Left,
Content = text
};
OutBox.Children.Add(label);
});
}//接受到的的消息
else
{
App.Current.Dispatcher.Invoke(
delegate ()
{
TextBlock text = new TextBlock
{
MaxWidth = 400,
MinWidth = 10,
MinHeight = 30,
Text = message.Text,
TextWrapping = TextWrapping.Wrap,
FontFamily = new FontFamily("Comic Sans MS"),
HorizontalAlignment = HorizontalAlignment.Right
};
Label label = new Label
{
HorizontalAlignment=HorizontalAlignment.Right,
Content=text
};
OutBox.Children.Add(label);
});
}//发送的的消息
}//将消息展现于UI界面
} }
} }
+64
View File
@@ -0,0 +1,64 @@
using System;
using System.Windows;
using System.Windows.Controls;
namespace StormChatWPF
{
public class ScrollViewerExtensions
{
public static readonly DependencyProperty AlwaysScrollToEndProperty = DependencyProperty.RegisterAttached("AlwaysScrollToEnd", typeof(bool), typeof(ScrollViewerExtensions), new PropertyMetadata(false, AlwaysScrollToEndChanged));
private static bool _autoScroll;
private static void AlwaysScrollToEndChanged(object sender, DependencyPropertyChangedEventArgs e)
{
ScrollViewer scroll = sender as ScrollViewer;
if (scroll != null)
{
bool alwaysScrollToEnd = (e.NewValue != null) && (bool)e.NewValue;
if (alwaysScrollToEnd)
{
scroll.ScrollToEnd();
scroll.ScrollChanged += ScrollChanged;
// scroll.SizeChanged += Scroll_SizeChanged;
}
else { scroll.ScrollChanged -= ScrollChanged; /*scroll.ScrollChanged -= ScrollChanged; */}
}
else { throw new InvalidOperationException("The attached AlwaysScrollToEnd property can only be applied to ScrollViewer instances."); }
}
        //private static void Scroll_SizeChanged(object sender, SizeChangedEventArgs e)
        //{
        //    ScrollViewer scroll = sender as ScrollViewer;
        //    if (scroll == null) { throw new InvalidOperationException("The attached AlwaysScrollToEnd property can only be applied to ScrollViewer instances."); }
        //    double d = scroll.ActualHeight + scroll.ViewportHeight + scroll.ExtentHeight;
        //    scroll.ScrollToVerticalOffset(d);
        //}
        public static bool GetAlwaysScrollToEnd(ScrollViewer scroll)
{
if (scroll == null) { throw new ArgumentNullException("scroll"); }
return (bool)scroll.GetValue(AlwaysScrollToEndProperty);
}
public static void SetAlwaysScrollToEnd(ScrollViewer scroll, bool alwaysScrollToEnd)
{
if (scroll == null) { throw new ArgumentNullException("scroll"); }
scroll.SetValue(AlwaysScrollToEndProperty, alwaysScrollToEnd);
}
private static void ScrollChanged(object sender, ScrollChangedEventArgs e)
{
ScrollViewer scroll = sender as ScrollViewer;
if (scroll == null) { throw new InvalidOperationException("The attached AlwaysScrollToEnd property can only be applied to ScrollViewer instances."); }
if (e.ExtentHeightChange == 0) { _autoScroll = scroll.VerticalOffset == scroll.ScrollableHeight; }
if (_autoScroll && e.ExtentHeightChange != 0) { scroll.ScrollToVerticalOffset(scroll.ExtentHeight); }
}
}
}
+7
View File
@@ -63,6 +63,7 @@
<Compile Include="MainWindow.xaml.cs"> <Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon> <DependentUpon>MainWindow.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="ScrollViewer.cs" />
<Page Include="LogWindow.xaml"> <Page Include="LogWindow.xaml">
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>
@@ -115,5 +116,11 @@
<Name>Interact</Name> <Name>Interact</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Resource Include="UI\登录界面底.png" />
</ItemGroup>
<ItemGroup>
<Resource Include="UI\闪电.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>
Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB