如何将 Dispose 功能添加到 C# UserControl?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/167602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:27:50  来源:igfitidea点击:

How do I add Dispose functionality to a C# UserControl?

c#user-controlsdispose

提问by e-holder

I have a class which implements UserControl. In .NET 2005, a Dispose method is automatically created in the MyClass.Designer.cs partial class file that looks like this:

我有一个实现 UserControl 的类。在 .NET 2005 中,会在 MyClass.Designer.cs 部分类文件中自动创建一个 Dispose 方法,如下所示:

  protected override void Dispose(bool disposing)
  {
     if (disposing && (components != null))
     {
        components.Dispose();
     }
     base.Dispose(disposing);
  }

If I want to add my own Dispose functionality, where would I put it? Since this file is generated, I don't want to add code here and risk it getting blown away.

如果我想添加我自己的 Dispose 功能,我会把它放在哪里?由于这个文件是生成的,我不想在这里添加代码并冒着被吹走的风险。

采纳答案by Michael Damatov

In such a case I move the generated Disposemethod to the main file and extend it. Visual Studio respects this.

在这种情况下,我将生成的Dispose方法移动到主文件并扩展它。Visual Studio 尊重这一点。

An other approach would be using a partial method (C# 3.0).

另一种方法是使用部分方法(C# 3.0)。

回答by Micah

I believe in this case the code-generator honors your code. It should be safe to put it in the codebehind.

我相信在这种情况下,代码生成器会尊重您的代码。把它放在代码隐藏中应该是安全的。

回答by akmad

In VS 2005 (and 2008) you can update the Disposemethod and it will not get removed when you edit the control from the designer.

在 VS 2005(和 2008)中,您可以更新该Dispose方法,并且在您从设计器编辑控件时它不会被删除。

回答by Mark Heath

You can move it out from the .designer.cs file and into the main .cs file if you want. As has been said already, it won't be overwritten.

如果需要,您可以将其从 .designer.cs 文件移出到主 .cs 文件中。正如已经说过的那样,它不会被覆盖。

回答by Michael Damatov

All Componentclasses implement a Disposedevent. You can add an event handler for that event and clean up things in there.

所有Component类都实现一个Disposed事件。您可以为该事件添加一个事件处理程序并清理那里的内容。

For example, in your UserControlyou could add following method:

例如,在您UserControl可以添加以下方法:

private void OnDispose(object sender, EventArgs e)
{
    // do stuff on dispose
}

And in constructor (or in Loadevent handler) add the following line:

并在构造函数(或Load事件处理程序)中添加以下行:

Disposed += OnDispose;

回答by Michael Damatov

You just need to overload the public void Dispose() method in the Component Class that the user control inherits.

您只需要在用户控件继承的组件类中重载 public void Dispose() 方法。

make sure you pass the call to the base method as well as doing your dispose functionally or you'll break the functionality unless you fully implement it

确保您将调用传递给基本方法以及在功能上进行处理,否则除非您完全实现它,否则您将破坏该功能

回答by tridy.net

there is a Unloaded event for the UserControl that you can use to clean up,

UserControl 有一个 Unloaded 事件,您可以使用它来清理,

回答by dynamichael

I would think the cleanest way would be to have your control subscribe to its own Disposed() event and do your cleanup in there.

我认为最干净的方法是让您的控件订阅它自己的 Disposed() 事件并在那里进行清理。