¿Cómo subo un evento en un usercontrol y lo cojo en la página principal?
Tengo un UserControl
, y necesito notificar a la página principal que se hizo clic en un botón en el UserControl
. ¿Cómo subo un evento en UserControl
y lo cojo en la página principal? Traté de usar static
, y muchos me sugirieron ir a eventos!
3 answers
Echa un vistazo a Evento Burbujeante { http://msdn.microsoft.com/en-us/library/aa719644%28vs.71%29.aspx
Editar para añadir un ejemplo rápido: (y edición adicional para mejorar el formato)
Control De Usuario
public event EventHandler StatusUpdated;
private void FunctionThatRaisesEvent()
{
//Null check makes sure the main page is attached to the event
if (this.StatusUpdated != null)
this.StatusUpdated(this, new EventArgs());
}
Página Principal/Formulario
public void MyApp()
{
//USERCONTROL = your control with the StatusUpdated event
this.USERCONTROL.StatusUpdated += new EventHandler(MyEventHandlerFunction_StatusUpdated);
}
public void MyEventHandlerFunction_StatusUpdated(object sender, EventArgs e)
{
//your code here
}
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-01-11 11:23:52
Simplemente agrega un evento en tu control:
public event EventHandler SomethingHappened;
Y subirlo cuando desee notificar al padre:
if(SomethingHappened != null) SomethingHappened(this, new EventArgs);
Si necesita EventArgs personalizados, intente EventHandler<T>
en su lugar con T
siendo un tipo derivado de EventArgs
.
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
2011-05-31 19:56:51
O si está buscando una solución más desacoplada, puede usar un modelo de editor / suscriptor de messenger como MVVM Light Messenger aquí
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
2011-05-31 20:05:31