代码之家  ›  专栏  ›  技术社区  ›  Christian Klemm

WPF Chatbox控件

  •  0
  • Christian Klemm  · 技术社区  · 10 年前

    目前我正在一个聊天客户端上工作,并将我的客户端从windows表单(因为表单很烂)更改为WPF。我不确定哪个控件可以用来实现聊天框。我可以使用TextBox,但当它满了时,它不会显示完整的内容。我还尝试使用ListBox,但当我尝试添加项目时,它们不会显示。我使用此代码向其添加内容:

    internal void AddMessage(string message)
    {
        listBox_messages.Items.Add(message);
        listBox_messages.Items.Refresh();
    }
    

    有人知道哪种控制方式最适合这个目的吗?

    谢谢你的帮助!

    编辑: 我为此实现了一个TextBox并将其禁用。但是我用这个方法附加的文本没有显示出来。 我的班级:

    using System.ComponentModel;
    using System.Windows;
    
    namespace Chat_Client
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            /// <summary>
            /// MainWindow constructor
            /// </summary>
            public MainWindow()
            {
                InitializeComponent();
    
                textBox_messages.AppendText("Test" + "\n");
                textBox_messages.AppendText("Test" + "\n");
                textBox_messages.AppendText("Test" + "\n");
    
                Closing += OnWindowClosing;
            }
    
            private void OnWindowClosing(object sender, CancelEventArgs e)
            {
                Program.Shutdown();
            }
    
            private void button_connect_Click(object sender, RoutedEventArgs e)
            {
                if(Program.Connected)
                {
                    Program.Disconnect();
                }
                else
                {
                    Program.Connect();
                }            
            }
    
            private void button_sendMessage_Click(object sender, RoutedEventArgs e)
            {
    
            }
    
            internal void AddMessage(string message)
            {
                textBox_messages.AppendText(message + "\n");
            }
        }
    }
    

    测试字符串将显示,但AddMessage方法添加的文本不会显示。我可以验证该方法是否被调用,我只是用该方法中的断点检查了它。有人知道这是怎么发生的吗?

    3 回复  |  直到 10 年前
        1
  •  3
  •   El Cattivo    10 年前

    使现代化

    如果Program类从UI线程以外的其他线程调用AddMessage(字符串),则必须使用Dispatcher才能更新UI。

    Application.Current.Dispatcher.BeginInvoke(new Action(() =>
    {
        mainWindow.AddMessage(message);
    }));
    

    MVVM 是使用WPF的方式。

    1. 将您的邮件添加到 ObservableCollection 在ViewModel类(ChatViewModel.cs)中:
    public class ChatViewModel
    {
        public ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
    
        internal void AddMessage(string message)
        {
            Messages.Add(message);
        }
    }
    
    1. 将此ViewModel设置为视图的DataContext(ChatView.xml)
    public ChatView : UserControl
    {
        public ChatView()
        {
            InitializeComponent();
            DataContext = new ChatViewModel();
        }
    }
    
    1. 在XAML代码中将ObservableCollection绑定到 ItemsSource 您的财产 ListBox :
    <ListBox ItemsSource="{Binding Messages}") />
    

    将消息添加到Messages集合时,它应显示在ListBox中。这不是一个完整的例子,但它应该引导你走向正确的方向。

        2
  •  2
  •   Andrew    9 年前

    *响应您的编辑:您从哪里调用AddMessage()?我试过你的代码,只调用了AddMessage(“foo”);从按钮点击事件和工作良好。

    对于聊天框,我会使用TextBox来编写聊天消息,并用ScrollViewer将其包裹起来进行滚动。在TextBox上使用AppendText()编写消息后,可以调用ScrollToEnd()滚动到TextBox的底部。

    在XAML中:

    <ScrollViewer x:Name="ScrollViewer" ScrollChanged="ScrollViewer_OnScrollChanged"> <TextBox x:Name="ChatBox"/> </ScrollViewer>

    在后面的代码中:

        private void WriteToChat(string message)
        {
            ChatBox.AppendText(message);
            ChatBox.ScrollToEnd();
        }
    
        private bool _autoScroll = true;
        private void ScrollViewer_OnScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            if (e.ExtentHeightChange == 0)
            {
                _autoScroll = ScrollViewer.VerticalOffset == ScrollViewer.ScrollableHeight;
            }
    
            if (_autoScroll && e.ExtentHeightChange != 0)
            {
                ScrollViewer.ScrollToVerticalOffset(ScrollViewer.ExtentHeight);
            }
        }
    
        3
  •  0
  •   Amin Akhtar    10 年前

    您可以使用TextBox并在Xaml文件中将VerticalAlignment设置为Stretch。

    <TextBox x:Name=“TextBox”VerticalAlignment=“拉伸”>

    推荐文章