Listview完整显示用户头像及昵称信息

This commit is contained in:
Kaniu Tayou
2018-12-27 17:42:26 +08:00
parent 96c128b295
commit 688a53a9dd
6 changed files with 126 additions and 119 deletions
@@ -1,36 +1,36 @@
using Interact; using Interact;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data; using System.Windows.Data;
using System.Windows.Documents; using System.Windows.Documents;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Navigation; using System.Windows.Navigation;
using System.Windows.Shapes; using System.Windows.Shapes;
namespace StormChatWPF namespace StormChatWPF
{ {
/// <summary> /// <summary>
/// MainWindow.xaml 的交互逻辑 /// MainWindow.xaml 的交互逻辑
/// </summary> /// </summary>
public partial class LogWindow : Window public partial class LogWindow : Window
{ {
public static LogWindow Instence; public static LogWindow Instence;
public LogWindow() public LogWindow()
{ {
InitializeComponent(); InitializeComponent();
Instence = this; Instence = this;
} }
private void Login_button_Click(object sender, RoutedEventArgs e) private void Login_button_Click(object sender, RoutedEventArgs e)
{ {
Login_button.IsEnabled = false; Login_button.IsEnabled = false;
Chat.Log(AccountBox.Text,passwordBox.Password); Chat.Log(AccountBox.Text,passwordBox.Password);
} }
} }
} }
@@ -19,7 +19,8 @@
<ColumnDefinition Width="200*"/> <ColumnDefinition Width="200*"/>
<ColumnDefinition Width="200*"/> <ColumnDefinition Width="200*"/>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<ListView x:Name="UsersList" Margin="0,0,0,0" IsEnabled="True" SelectionChanged="UsersList_SelectionChanged" Grid.Row="1" Grid.RowSpan="2"/> <ListView x:Name="UsersList" Margin="0,0,0,0" IsEnabled="True" SelectionChanged="UsersList_SelectionChanged" Grid.Row="1" Grid.RowSpan="2">
</ListView>
<TextBox x:Name="InputBox" Margin="0,0,0,0" TextWrapping="Wrap" Text="" Grid.Row="2" Grid.ColumnSpan="3" Grid.Column="1" /> <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"/> <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 Grid.ColumnSpan="3" Grid.Column="1" Margin="0,0,0,0" Grid.Row="1" >
@@ -27,6 +28,6 @@
<StackPanel x:Name="OutBox" Margin="0,0,0,0" Grid.ColumnSpan="3" Grid.Column="1" Grid.Row="1" ScrollViewer.VerticalScrollBarVisibility="Hidden"/> <StackPanel x:Name="OutBox" Margin="0,0,0,0" Grid.ColumnSpan="3" Grid.Column="1" Grid.Row="1" ScrollViewer.VerticalScrollBarVisibility="Hidden"/>
</ScrollViewer.Content> </ScrollViewer.Content>
</ScrollViewer> </ScrollViewer>
<Button x:Name="button" Content="Button" Grid.Column="2" HorizontalAlignment="Left" Margin="73,31,0,0" Grid.Row="2" VerticalAlignment="Top" Width="75" Click="button_Click_1"/> <Button x:Name="button" Content="更改头像" Grid.Column="1" HorizontalAlignment="Left" Margin="0,64,0,-2" Grid.Row="2" VerticalAlignment="Top" Width="75" Click="button_Click_1"/>
</Grid> </Grid>
</Window> </Window>
@@ -1,83 +1,89 @@
using Interact; using Interact;
using StormChatWPF.UI; using StormChatWPF.UI;
using System; using System;
using System.Linq; using System.Linq;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
namespace StormChatWPF namespace StormChatWPF
{ {
/// <summary> /// <summary>
/// UserWindow.xaml 的交互逻辑 /// UserWindow.xaml 的交互逻辑
/// </summary> /// </summary>
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
public static MainWindow Instence; public static MainWindow Instence;
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
LoadContactsList(); LoadContactsList();
Instence = this; Instence = this;
} }
private void LoadContactsList() private void LoadContactsList()
{ {
if (Chat.ContactsList.Any()) if (Chat.ContactsList.Any())
{ {
foreach (var user in Chat.ContactsList) foreach (var user in Chat.ContactsList)
{ {
UsersList.Items.Add(user.NickName); if (user.Name !=User.Me.Name)
} {
} UsersList.Items.Add(new UI.ContactsInfo(user)
}//向联系人列表listview中添加元素 {
HorizontalAlignment = HorizontalAlignment.Center
private void UsersList_SelectionChanged(object sender, SelectionChangedEventArgs e) });
{ }
if (UsersList.SelectedItem != null) }
{ }
Chat.CurrentContact = Chat.ContactsList[UsersList.SelectedIndex]; }//向联系人列表listview中添加元素
}
} private void UsersList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
private void Button_Click(object sender, RoutedEventArgs e) if (UsersList.SelectedItem != null)
{ {
if (Chat.CurrentContact == null) Chat.CurrentContact = Chat.ContactsList[UsersList.SelectedIndex];
{ }
MessageBox.Show("请选择联系人!"); }
return;
} private void Button_Click(object sender, RoutedEventArgs e)
if (InputBox.Text!="") {
{ if (Chat.CurrentContact == null)
Chat.chat.SendMessage(InputBox.Text, Chat.CurrentContact); {
InputBox.Text = ""; MessageBox.Show("请选择联系人!");
} return;
} }
if (InputBox.Text != "")
internal void ShowMessage(Message message) {
{ Chat.chat.SendMessage(InputBox.Text, Chat.CurrentContact);
if (message.To == User.Me) InputBox.Text = "";
{ }
App.Current.Dispatcher.Invoke( }
(Action)delegate()
{ internal void ShowMessage(Message message)
OutBox.Children.Add(new StormChatWPF.UI.ChatBubble(message,HorizontalAlignment.Left)); {
}); if (message.To == User.Me)
}//接受到的的消息 {
else App.Current.Dispatcher.Invoke(
{ (Action)delegate ()
App.Current.Dispatcher.Invoke( {
(Action)delegate() OutBox.Children.Add(new StormChatWPF.UI.ChatBubble(message, HorizontalAlignment.Left));
{ });
OutBox.Children.Add(new StormChatWPF.UI.ChatBubble(message, HorizontalAlignment.Right)); }//接受到的的消息
}); else
}//发送的的消息 {
App.Current.Dispatcher.Invoke(
(Action)delegate ()
{
OutBox.Children.Add(new StormChatWPF.UI.ChatBubble(message, HorizontalAlignment.Right));
});
}//发送的的消息
}//将消息展现于UI界面 }//将消息展现于UI界面
private void button_Click_1(object sender, RoutedEventArgs e) private void button_Click_1(object sender, RoutedEventArgs e)
{ {
App.Current.Dispatcher.Invoke( App.Current.Dispatcher.Invoke(
(Action)delegate () (Action)delegate ()
{ {
}); });
} }
} }
} }
@@ -69,7 +69,7 @@
<Compile Include="UI\Contacts.xaml.cs"> <Compile Include="UI\Contacts.xaml.cs">
<DependentUpon>Contacts.xaml</DependentUpon> <DependentUpon>Contacts.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="UI\UserInfo.cs" /> <Compile Include="UI\ContactsInfo.cs" />
<Compile Include="Window1.xaml.cs"> <Compile Include="Window1.xaml.cs">
<DependentUpon>Window1.xaml</DependentUpon> <DependentUpon>Window1.xaml</DependentUpon>
</Compile> </Compile>
@@ -5,13 +5,13 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:StormChatWPF.UI" xmlns:local="clr-namespace:StormChatWPF.UI"
mc:Ignorable="d" mc:Ignorable="d"
d:DesignHeight="40" d:DesignWidth="120"> d:DesignHeight="40" d:DesignWidth="120" HorizontalAlignment="Stretch">
<Grid> <Grid>
<WrapPanel HorizontalAlignment="Left" Height="40" VerticalAlignment="Stretch" Width="120"> <WrapPanel Height="40" VerticalAlignment="Stretch">
<Image x:Name="HeadPicture" HorizontalAlignment="Left" Height="40" Width="40"/> <Image x:Name="HeadPicture" HorizontalAlignment="Left" Height="40" Width="40"/>
<Label x:Name="text" Content="Label" Height="40" Width="80"/> <Label x:Name="text" Content="Label" Height="40" FontSize="14" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center"/>
</WrapPanel> </WrapPanel>
</Grid> </Grid>
</UserControl> </UserControl>
@@ -4,9 +4,9 @@ using System.IO;
namespace StormChatWPF.UI namespace StormChatWPF.UI
{ {
internal class UserInfo : Contacts internal class ContactsInfo : Contacts
{ {
public UserInfo(User user) public ContactsInfo(User user)
{ {
image.BeginInit(); image.BeginInit();
image.StreamSource = user.Photo; image.StreamSource = user.Photo;