文本框文本管理
我有一个 WPF 应用程序,我试图在其中创建一个与 Visual Studio 2010 中的“输出窗口”非常相似的“诊断面板”。它只包含一个文本框,其中收集所有类型的输出并将其附加到文本框中使用 .AppendText()
方法。
我们有几个客户一直让这个诊断面板保持打开状态并且从不清除它。因此,随着新的输出不断出现,会显示大量文本...
WPF TextBox 有一个 MaxLength 属性,我将其设置为 0。根据 MSDN 文档,“当此属性设置为 0 时,可以在控件中输入的文本的最大长度仅受可用内存的限制。”
对于离开此面板的客户,我讨厌让文本和内存不受控制地增长...我认为这最终可能会导致应用程序挂起...
我很好奇是否有一个好的方法来管理这个..我想保留文本框中显示的一定数量的行,并在新行出现时丢弃最旧的行...
有什么想法吗?
谢谢!
I have a WPF application where I'm trying to create a "diagnostics panel" that's very similar to the "Output Window" in Visual Studio 2010. It simply contains a text box where all types of output are collected and appended in the text box using the .AppendText()
method.
We have a couple of customers who leave this diagnostics panel up all the time and never clear it. As a result, a very large amount of text gets displayed as new output continues to come in...
The WPF TextBox has a MaxLength property which I leave set to 0. According to the MSDN documentation, "When this property is set to 0, the maximum length of the text that can be entered in the control is limited only by available memory."
For the customers that leave this panel up, I hate to just let the text and memory grow uncontrolled... I think this may eventually cause the application to hang up...
I am curious if there's a good way to mange this... I want to keep a certain number of lines displayed in the text box and discard the oldest as new lines come in...
Any thoughts?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不使用一个列表框,每个句子都有自己的文本块 - 这样你就可以实现虚拟化?
http://social.msdn.microsoft.com/Forums/en/wpf/thread/98090161-0abf-4799-bbcb-852dcc0f0608
Why not use a listbox with each sentence getting its own textblock - so you can get virtualization?
http://social.msdn.microsoft.com/Forums/en/wpf/thread/98090161-0abf-4799-bbcb-852dcc0f0608
您的代码后面可以有一个
DispatcherTimer
。有了这个,您可以将其设置为每 10 分钟(或您想要的任何时间段)Tick
。在 Tick 事件处理程序方法中,您可以获取文本框中的文本,丢弃除要保存的文本量之外的所有文本,然后将该文本设置回文本框。您还可以将文本保存到日志文本文件中。您必须弄清楚要向文本文件附加什么内容,这样您就不会多次向其中写入相同的文本。这取决于您的具体需求。
DispatcherTimer 文档
就像 Xaisoft 所说,你不应该'不要使用
TextBox
来实现此目的,可能使用TextBlock
代替。您可能必须将其放入ScrollViewer
中,我不记得了。You could have a
DispatcherTimer
in your code behind. With this you, you can set it toTick
every 10 minutes (or whatever time period you want). And in the Tick event handler method, you can take the text in your textbox, throw away the all but the amount of text you want to save and then set the that text back to the textbox.You could also save the text to a log text file. You'd have to figure out what to append to the text file so you won't write the same text to it multiple times. This depends on what exactly your needs are.
DispatcherTimer documenation
Like Xaisoft said, you shouldn't use a
TextBox
for this, probably aTextBlock
instead. You might have to put that inside aScrollViewer
, I don't remember.处理方法如下:
如果您的用户确实想要保存所有内容,没什么大不了的,它已经保存到该文件中。
每次执行应用程序时,或在某个适当的时间间隔,将日志记录滚动到新文件中。
Here's how you handle it:
If your users really want to save everything, no biggie, it already is saved to that file.
Upon each execution of the app, or at some appropriate interval, roll your logging to a new file.