![](/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.