![](/img/trans.png)
[英]C# UWP - how to pass additional parameter to an event handler at runtime
[英]How can I correctly pass an input parameter to an event handler method in C#?
我是C#和.NET的新手,我有以下问题。
在我的代码中,我有这样的事情:
Table table = new Table();
lstLabelType.SelectedIndexChanged += new System.EventHandler(SelectDocumentTypeChanged);
将事件处理程序附加到视图中的下拉元素。
因此,当用户将所选值更改为此下拉列表时,将执行SelectDocumentTypeChanged()方法,此方法:
private void SelectDocumentTypeChanged(object sender, EventArgs e)
{
Debug.WriteLine("SelectDocumentTypeChanged() STARTED");
SPWeb contextWeb = SPContext.Current.Web;
DropDownList listaTipiDocumenti = (DropDownList)sender;
tipoDocumentoSelezionato = listaTipiDocumenti.SelectedValue;
this.renderizzaEtichetteFacoltative(tipoDocumentoSelezionato, table);
string url = contextWeb.Url;
string link = url + "/ARXEIA WEBPART/Stampa Etichetta.aspx?IsDlg=1&postazione=" + macchina + "&tipoDoc=" + tipoDocumentoSelezionato;
SPUtility.Redirect(link, SPRedirectFlags.Default, Context);
}
现在我有一个问题。 我必须将在我的代码中创建的Table表对象传递给此事件处理程序方法,因为在此方法中我必须使用它。
我该如何正确实现此行为? 当用户更改下拉列表中的值时,如何自动调用此Table表对象到SelectDocumentTypeChanged()方法?
尝试编写如下代码:
Table table = new Table();
lstLabelType.SelectedIndexChanged += (sender, e) =>
{
Debug.WriteLine("SelectDocumentTypeChanged() STARTED");
SPWeb contextWeb = SPContext.Current.Web;
DropDownList listaTipiDocumenti = (DropDownList)sender;
tipoDocumentoSelezionato = listaTipiDocumenti.SelectedValue;
this.renderizzaEtichetteFacoltative(tipoDocumentoSelezionato, table);
string url = contextWeb.Url;
string link = url + "/ARXEIA WEBPART/Stampa Etichetta.aspx?IsDlg=1&postazione=" + macchina + "&tipoDoc=" + tipoDocumentoSelezionato;
SPUtility.Redirect(link, SPRedirectFlags.Default, Context);
};
现在您可以直接在事件处理程序中使用table
。
您甚至可以立即忽略sender
并执行此操作:
lstLabelType.SelectedIndexChanged += (s, e) =>
{
Debug.WriteLine("SelectDocumentTypeChanged() STARTED");
SPWeb contextWeb = SPContext.Current.Web;
tipoDocumentoSelezionato = lstLabelType.SelectedValue;
this.renderizzaEtichetteFacoltative(tipoDocumentoSelezionato, table);
string url = contextWeb.Url;
string link = url + "/ARXEIA WEBPART/Stampa Etichetta.aspx?IsDlg=1&postazione=" + macchina + "&tipoDoc=" + tipoDocumentoSelezionato;
SPUtility.Redirect(link, SPRedirectFlags.Default, Context);
};
如果要保留单独的方法,可以执行此操作:
Table table = new Table();
lstLabelType.SelectedIndexChanged += (o, e) => SelectDocumentTypeChanged(lstLabelType, table);
private void SelectDocumentTypeChanged(DropDownList lstLabelType, Table table)
{
Debug.WriteLine("SelectDocumentTypeChanged() STARTED");
SPWeb contextWeb = SPContext.Current.Web;
tipoDocumentoSelezionato = lstLabelType.SelectedValue;
this.renderizzaEtichetteFacoltative(tipoDocumentoSelezionato, table);
string url = contextWeb.Url;
string link = url + "/ARXEIA WEBPART/Stampa Etichetta.aspx?IsDlg=1&postazione=" + macchina + "&tipoDoc=" + tipoDocumentoSelezionato;
SPUtility.Redirect(link, SPRedirectFlags.Default, Context);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.