前端实现接收发送文本消息
This commit is contained in:
+50
-44
@@ -12,26 +12,63 @@ namespace StormChatWPF
|
||||
class Chat
|
||||
{
|
||||
public static List<User> ContactsList;//联系人集合
|
||||
public static Chat chat = new Chat();
|
||||
public static Chat chat=new Chat();
|
||||
private Chat()
|
||||
{
|
||||
StormClient.OnLoginDone += HaveLogin;
|
||||
StormClient.OnGetUserListDone += GetContactsList;
|
||||
StormClient.OnMessage +=ReciveMessage;
|
||||
StormClient.OnLoginDone += OnHaveLogin;
|
||||
StormClient.OnGetUserListDone += OnGetContactsList;
|
||||
StormClient.OnMessage +=OnMessage;
|
||||
}
|
||||
internal User SetContact { get; set; }//设置当前联系人对象
|
||||
|
||||
internal static void GetContactsList(ResultHead head, User[] users)
|
||||
internal static User CurrentContact { get; set; }//设置当前联系人对象
|
||||
/// <summary>
|
||||
/// 获取联系人列表(完成后打开主窗体)
|
||||
/// </summary>
|
||||
private void OnGetContactsList(ResultHead head, User[] users)
|
||||
{
|
||||
if (head.Error != "")
|
||||
{
|
||||
MessageBox.Show("无法获取联系人列表");
|
||||
}
|
||||
else
|
||||
ContactsList =new List<User>( users);
|
||||
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())
|
||||
{
|
||||
@@ -45,37 +82,6 @@ namespace StormChatWPF
|
||||
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,10 +20,10 @@
|
||||
<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,70,160,45" Width="75"/>
|
||||
<TextBox x:Name="AccountBox" TextWrapping="Wrap" Text="201701110203" Grid.Row="2" TextAlignment="Center" Margin="137.4,5,137.8,112.6" Grid.Column="1" Grid.ColumnSpan="2" Height="22" Width="120">
|
||||
|
||||
</TextBox>
|
||||
<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="120"/>
|
||||
<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>
|
||||
</Window>
|
||||
|
||||
@@ -29,15 +29,9 @@ namespace StormChatWPF
|
||||
}
|
||||
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()
|
||||
{
|
||||
}//展现自己发出的消息
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,14 +9,20 @@
|
||||
|
||||
>
|
||||
<Grid>
|
||||
<ListView x:Name="UsersList" HorizontalAlignment="Left" Width="201" Margin="0,64,0,10" IsEnabled="True" SelectionChanged="UsersList_SelectionChanged">
|
||||
<ListView.View>
|
||||
<GridView>
|
||||
<GridViewColumn/>
|
||||
</GridView>
|
||||
</ListView.View>
|
||||
</ListView>
|
||||
<TextBox HorizontalAlignment="Left" Height="90" Margin="201,320,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="583" />
|
||||
<Button Content="Button" HorizontalAlignment="Left" Margin="378,262,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="60*"/>
|
||||
<RowDefinition Height="300*"/>
|
||||
<RowDefinition Height="90*"/>
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="200*"/>
|
||||
<ColumnDefinition Width="200*"/>
|
||||
<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"/>
|
||||
<StackPanel x:Name="OutBox" Margin="0,0,0,0" Grid.ColumnSpan="3" Grid.Column="1" Grid.Row="1"/>
|
||||
</Grid>
|
||||
</Window>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Interact;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@@ -19,34 +20,87 @@ namespace StormChatWPF
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public static MainWindow Instence;
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
LoadContactsList();
|
||||
Instence = this;
|
||||
}
|
||||
private void AddUserList()
|
||||
private void LoadContactsList()
|
||||
{
|
||||
if (Chat.ContactsList.Any())
|
||||
{
|
||||
foreach (var user in Chat.ContactsList)
|
||||
{
|
||||
UsersList.Items.Add(user);
|
||||
UsersList.Items.Add(user.NickName);
|
||||
}
|
||||
}
|
||||
}//向联系人列表listview中添加元素
|
||||
|
||||
private void UsersList_Selected(object sender, RoutedEventArgs 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)
|
||||
{
|
||||
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界面
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user