C#:用于即时消息发送器(聊天)的 Richtextbox

发布于 2024-10-06 13:17:11 字数 640 浏览 3 评论 0原文

我想要我自己的即时消息程序(聊天)。并且普通文本框不支持格式化/彩色文本。 我读了一些关于 WPF 中的 Richtextbox 的文章,块、段落和运行的新概念非常有趣。 序列化这些对象并将它们发送到其他聊天客户端是个好主意吗? (文字格式应与作者原文一致) 如果我想将输入文本框中的块添加到输出文本框(仅用于测试),我会得到一个异常,即这些块/段落被其他 Richtextbox 使用。 然后我保存了这些对象的引用,将其从第一个文本框中删除,并将它们添加到第二个文本框中。

 For example:


    FlowDocument oldTextDocument = richTextBoxMessageBox.Document;

            richTextBoxMessageBox.Document = new FlowDocument();

            while(oldTextDocument.Blocks.Count > 0)
            {
                richTextBoxChatHistory.Document.Blocks.Add(oldTextDocument.Blocks.FirstBlock);
            }

(我不能用 for-each 来做到这一点,因为这会导致异常。)

i want my own Instant Messager (Chat). And the normal textbox doesn't support formatted/colored text.
I read some articles about the Richtextbox in WPF and the new concept with Blocks, Paragraphs and Runs is pretty interesting.
Is it a good idea to serialize these objects and send them to the other chat-clients? (The text should be formatted, like the author's orginal text)
If i want to add the blocks from the input-textbox to the output-textbox (only for testing), i get a exception that the blocks/paragraphs are used by an other richtextbox.
Then i saved the reference from these objects, removed it from the first textbox and added them to the second textbox.

 For example:


    FlowDocument oldTextDocument = richTextBoxMessageBox.Document;

            richTextBoxMessageBox.Document = new FlowDocument();

            while(oldTextDocument.Blocks.Count > 0)
            {
                richTextBoxChatHistory.Document.Blocks.Add(oldTextDocument.Blocks.FirstBlock);
            }

(I cant do it with for-each because this will cause a exception.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城挽邺 2024-10-13 13:17:11

我认为将序列化对象发送到其他客户端不是一个好主意,因为它们会有一些(相当大的)开销。

我前段时间做了一个聊天应用程序,我(成功地)使用了WebBrowser作为聊天窗口中的主要控件(它支持很多格式、富媒体等),并且我只通过网络发送html文本(加密的) 。

关于您的代码,您不能在两个不同的控件中拥有相同的 Paragraph 实例,因为它是一个 ContentElement - 就像您不能在两个不同的面板中拥有相同的标签一样。

I don't think it's a good idea to send serialized objects to the other clients, because they will have some (considerable) overhead.

I did some time ago a chat application and i used (successfully) the WebBrowser as the main control in the chat windows (which supports a lot of formating, rich media, etc), and I only send over the network html text (encrypted).

Regarding your code, you cannot have the same instance of a Paragraph for example in two different controls, because it is a ContentElement - like you cannot have the same Label in two different panels.

红ご颜醉 2024-10-13 13:17:11

Richtextbox 的工作方式就像我制作的即时聊天应用程序的魅力一样,只需确保您研究 Flowdocument 的行为并扩展她的基类即可。因此,您可以使用属性更改 cq 依赖属性之类的内容来编写干净的代码。事实上,您必须小心填写段落,它必须在控制器中的属性上完成,而不是在表单后面的代码中完成。如果你想保持代码干净。只需看看: http://www.lebroitsolutions.nl/en/projects-chat .aspx。您可以在哪里下载代码,我已经提供了更多信息。流式传输流文档(而不是整个富文本框)的开销是最小的。

Richtextbox works like a charm for a instant chat app I've made, just make sure you study the behavior of the Flowdocument and expand her base class. So you can use stuff like the property changed c.q. dependency property, to write clean code. Indeed you've to be careful about filling a paragraph, it has to be done on a property in the controller not in the code behind of the form. If you want to keep your code clean. Just have a look at: http://www.lebroitsolutions.nl/en/projects-chat.aspx. Where you can download the code and I've provided more info. The overhead for streaming a flowdocument (not the richtextbox as an whole) is minimal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文