C# 如何将2D字符串数组传递给单击事件?
我试图将2d字符串数组作为参数传递给单击事件(searchButton\u click),该事件是在单击事件中生成的,因此我可以执行搜索功能。但是,“searchButton\u Click”匹配代理“RoutedEventHandler”时出现错误C# 如何将2D字符串数组传递给单击事件?,c#,.net,C#,.net,我试图将2d字符串数组作为参数传递给单击事件(searchButton\u click),该事件是在单击事件中生成的,因此我可以执行搜索功能。但是,“searchButton\u Click”匹配代理“RoutedEventHandler”时出现错误没有重载。 我在stackoverflow和其他来源上尝试了几种解决方案,但没有一种有效。 我是C#编程新手,任何帮助都将不胜感激 private string[,] LongRunningTask() { w
没有重载。
我在stackoverflow和其他来源上尝试了几种解决方案,但没有一种有效。
我是C#编程新手,任何帮助都将不胜感激
private string[,] LongRunningTask()
{
workSheetName = getWorkSheetName();
var sourceFile = OpenExcelFile(filePath);
var sourceWorkSheet = GetWorkSheet(sourceFile.Item1, sourceFile.Item2, workSheetName); //instantiating the object.
var doc_Max = GetDocRow_Col(sourceWorkSheet.Item1);
var maxRow_Col = GetMaxRow_Col(sourceWorkSheet.Item1, doc_Max.Item1, doc_Max.Item2);
int maxRowCount = maxRow_Col.Item1;
int maxColCount = maxRow_Col.Item2;
WriteLine("Last column with content is Row {0} ", maxRowCount);
WriteLine("Last column with content is Column {0} ", maxColCount);
var getItemsResult = getItems(sourceWorkSheet.Item1, maxRow_Col);
string[,] itemsArr = getItemsResult;
Bindng2DArrayToListview2(listview, itemsArr, maxColCount);
//enableItems();
GarbageCollector(sourceWorkSheet.Item1, sourceWorkSheet.Item2, sourceWorkSheet.Item3, sourceWorkSheet.Item4);
//searchButton.Click += (sender2, e2) => { searchButton_Click(sender2, e2, itemsArr); };
return itemsArr;
}
private async void StartTask()
{
this.progressLabel.Content = "Getting Sheets..";
try
{
await Task.Run(() => LongRunningTask());
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
this.progressLabel.Content = "Done";
}
您可以将
LongRunningTask
的结果放入类上的一个字段(我假设它是窗口本身),然后只需从搜索按钮单击即可访问该字段
例如
public partial class TheWindow : Window
{
private string[,] LongRunningTask()
{
// Your LongRunningTask implementation.
}
private async Task StartTask()
{
progressLabel.Content = "Getting Sheets...";
try
{
_itemsArr = await Task.Run(() => LongRunningTask());
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
progressLabel.Content = "Done!";
}
private void searchButton_Click(object sender, RoutedEventArgs e)
{
if (_itemsArr == null)
{
// The field is null for some reason. You'll have to decide what to do in this case, but
// naturally you can't use it if it's null.
return;
}
// Do the search using the field _itemsArr.
}
private string[,] _itemsArr = null;
}
这是一个WPF应用程序吗?你总是可以将字符串数组“展平”为一个字符串,元素之间用逗号或分号分隔,然后在方法searchButton_中单击将该字符串解析回2d数组(如果我正确理解了你的问题)。WPF按钮点击处理程序有一个特定的签名(object sender,RoutedEventArgs e)因为它是由WPF本身定义的-请参阅,这样您就不会重载它。从代码看,您似乎正在将itemsArr绑定到listview,那么为什么需要将其传递给单击处理程序?@stuartd是的。您不需要从StartTask
返回itemsArr
(我认为,不清楚是什么原因导致StartTask
运行)。您只需将LongRunningTask
的结果存储在类成员中,例如一个字段。然后在searchButton\u单击
事件中访问该字段。