WPF 用户控件导致内存不足异常

发布于 2024-08-24 07:44:35 字数 1119 浏览 6 评论 0原文

寻找免费的拼写检查解决方案,我认为我这样做很聪明,但我想不是。

我创建了一个基于 Windows 窗体的应用程序,我希望该窗体将用户指定数量的用户控件(带有文本框)添加到面板上。然后,用户可以单击某个按钮,该面板上的控件将被清除,并添加新控件。用户执行某操作并重复该过程。现在,我希望这些文本框支持拼写检查,并四处寻找免费的解决方案。

WPF 文本框支持拼写检查,而常规 win 表单中的文本框则不支持。我认为我可以通过将这些 WPF 文本框添加到 ElementHost 对象(该对象又位于面板内)来使用它们。该面板将是一个用户控件。

因此,在我的应用程序中,我将能够将这些用户控件的实例添加到表单中,并利用 .NET 的拼写检查功能。这实际上有效,但在使用该应用程序一段时间后,发现该应用程序最终会由于内存不足错误而冻结。我已查明这些 WPF 控件的内存错误,因为普通文本框不会发生此问题。

当窗口打开并指定控件数量时,这几乎就是添加控件的方式:

Dim xOffset As Integer = 0
For i As Integer = 0 To theNumber
    Dim myUserControl As New SpecialUserControl()
    myPanel.Controls.Add(myUserControl)
    myUserControl.Location = New Point(7, 7)
    myUserControl.Location = New Point(xOffset, 7)
    xOffset = xOffset + 207
Next

注意:

  • myPanel 是表单上的面板
  • SpecialUserControl 是带有 WPF 文本框的用户控件(在 ElementHost 对象内)

当用户按下按钮,面板被清除:

myUserControl.Controls.Clear()

然后用户可以重复该过程。

当我尝试寻找解决方案时,互联网上有很多结果,我认为我遇到的问题是由于即使清除面板后 WPF 控件也没有消失。根据这个结论,我尝试了不同的解决方案来处理这些控件或将它们设置为空,但内存问题不断发生。如果有人可以在这里给我一些建议或想法,我将非常感激。

Looking for a free spell checking solution, I thought I was so smart in doing this but I guess not.

I have created a windows form based application and I want the form to add a user specified amount of user controls (with textboxes) on to a panel. The user can then click some button and the controls on this panel are cleared and new ones are added. The user does something and the process is repeated. Now, I wanted these textboxes to support spell checking and looked all over for a free solution.

WPF textboxes support spell checking where the ones in regular win forms do not. I thought I would be able to use these WPF textboxes by adding them to an ElementHost object which is, in turn, within a panel. This panel would be a user control.

So, in my application, I would be able to add instances of these user controls onto the form and make use of .NET's spell checking goodness. This actually worked but after using the application for a while, found that the application would eventually freeze on me due to out of memory errors. I have pinpointed the memory errors to these WPF controls since this problem does not happen with normal textboxes.

When the window is opened and the number of controls is specified, this is pretty much how the controls are added:

Dim xOffset As Integer = 0
For i As Integer = 0 To theNumber
    Dim myUserControl As New SpecialUserControl()
    myPanel.Controls.Add(myUserControl)
    myUserControl.Location = New Point(7, 7)
    myUserControl.Location = New Point(xOffset, 7)
    xOffset = xOffset + 207
Next

Note that:

  • myPanel is a panel on a form
  • SpecialUserControl is the user control with WPF textbox (within an ElementHost object)

When the user pressed a button, the panel is cleared:

myUserControl.Controls.Clear()

The user can then repeat the process.

There are a lot of results on the internet when I tried to find a solution and I'm thinking that the problem I am having is due to the fact that the WPF control is not going away even after clearing the panel. Following this conclusion, I have tried different solutions regarding disposing these controls or setting them to nothing but the memory problem keeps occurring. If someone could give me some advice or ideas here, I'd really appreciate it.

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

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

发布评论

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

评论(1

暗恋未遂 2024-08-31 07:44:35

我认为这可能只是因为这些用户控件的创建速度比收集速度快。我已经更改了程序,以便在不需要时不会创建任何这些特殊的用户控件。该程序可以与更多可管理的 WPF 控件配合使用。

I've decided that this may just be due to the fact that these user controls are being created faster than they can be collected. I've changed the program so that it doesn't create any of these special user controls if it isn't necessary. The program works fine with a more manageable number of WPF controls.

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