¿Cómo puedo añadir la funcionalidad Dispose a un C # UserControl?


Tengo una clase que implementa UserControl. En. NET 2005, un método Dispose se crea automáticamente en el MyClass.Diseñador.archivo de clase parcial cs que se ve así:

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

Si quiero agregar mi propia funcionalidad de Eliminación, ¿dónde la pondría? Dado que se genera este archivo, no quiero agregar código aquí y correr el riesgo de que se pierda.

Author: e-holder, 2008-10-03

8 answers

En tal caso muevo el método generado Dispose al archivo principal y lo extiendo. Visual Studio respeta esto.

Otro enfoque sería utilizar un método parcial (C# 3.0).

 51
Author: Michael Damatov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-09-23 13:20:35

Todas las clases Component implementan un evento Disposed. Puede agregar un controlador de eventos para ese evento y limpiar las cosas allí.

Por ejemplo, en su UserControl podría agregar el siguiente método:

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

Y en constructor (o en Load controlador de eventos) agregue la siguiente línea:

Disposed += OnDispose;
 64
Author: jahu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-19 14:14:34

Creo que en este caso el generador de código honra su código. Debería ser seguro ponerlo en el código detrás.

 7
Author: Micah,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-10-03 16:07:59

En VS 2005 (y 2008) puede actualizar el método Dispose y no se eliminará cuando edite el control desde el diseñador.

 6
Author: akmad,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-10-03 16:08:43

Se puede mover desde el .diseñador.archivo cs y en el principal .archivo CS si quieres. Como ya se ha dicho, no se sobrescribirá.

 2
Author: Mark Heath,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-10-03 16:09:23

Solo necesita sobrecargar el método public void Dispose() en la Clase de Componente que hereda el control de usuario.

Asegúrese de pasar la llamada al método base, así como hacer su disposición funcionalmente o romperá la funcionalidad a menos que la implemente completamente

 2
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-04-22 10:35:27

Creo que la forma más limpia sería tener su control suscribirse a su propio evento Disposed() y hacer su limpieza allí.

 0
Author: dynamichael,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-13 05:35:07

Hay un evento descargado para el UserControl que puede usar para limpiar,

 -2
Author: tridy.net,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-25 14:48:14