c# Delegate and Dispatcher problem
i get this error when trying this:
ERROR method name expected.
How should i do to correct the proble开发者_如何学Cm
delegate void DelegateFillList(DeliveryDoc[] deliveryDocs);
private void FillListViewAssignment(DeliveryDoc[] docs) {
if(lvMyAssignments.Dispatcher.CheckAccess()) {
lvMyAssignments.ItemsSource = docs;
lvAllOngoingAssignments.ItemsSource = docs;
if(m_tempDeliveryDocs != null) {
txtblockHandOverCount.Text = m_tempDeliveryDocs.Length.ToString();
}
} else {
lvMyAssignments.Dispatcher.BeginInvoke(
new DelegateFillList(FillListViewAssignment(docs)), null);
}
}
This is the problem:
new DelegateFillList(FillListViewAssignment(docs)
You can't create a delegate that way. You need to provide a method group which is just the name of the method:
lvMyAssignments.Dispatcher.BeginInvoke
(new DelegateFillList(FillListViewAssignment), new object[]{docs});
Alternatively, you could do it in two statements:
DelegateFillList fillList = FillListViewAssignment;
lvMyAssignments.Dispatcher.BeginInvoke(fillList, new object[]{docs});
The reason for the extra "wrapping" array is that you've only got one argument, which is an array - you don't want it to try to interpret that as a bunch of different arguments.
Change the last line to:
lvMyAssignments.Dispatcher.BeginInvoke(
new DelegateFillList(FillListViewAssignment), docs);
This line:
lvMyAssignments.Dispatcher.BeginInvoke(
new DelegateFillList(FillListViewAssignment(docs)), null);
Notice that you pass a method call to the delegate, not the method name. Change it to read:
lvMyAssignments.Dispatcher.BeginInvoke(
new DelegateFillList(FillListViewAssignment), null);
^
|
+- removed (docs)
I dun think you have to specify the arguments in the else part..
Try this :
lvMyAssignments.Dispatcher.BeginInvoke(new DelegateFillList(FillListViewAssignment), new object[]{docs});
EDITED - Included new object[]{docs}
. Thanks to Jon and Henk
精彩评论