Problem with C # BeginInvoke

Why do I have this error and how to fix it. thanks for the help

Error 4: Cannot convert lambda expression to enter "System.Delegate" because it is not a delegate type

void provider_GetAssignmentsComplete(object sender, QP_Truck_Model.Providers.GetAssignmentsEventArgs e) { lvMyAssignments.Dispatcher.BeginInvoke(() => { lvMyAssignments.ItemsSource = e.HandOverDocs; }); } 
+6
c # wpf
source share
2 answers

In some cases, a Lambda expression is implicitly converted to delegates. In particular, if a method expects a Delegate type, you must first explicitly specify lambda for the compiler to accept it.

What you can do is explicitly pour lambda, which should allow you to use BeginInvoke :

 lvMyAssignments.Dispatcher.BeginInvoke( (Action)(() => { lvMyAssignments.ItemsSource = e.HandOverDocs; })); 

Usually, if you have a method with a strongly typed delegate signature, for example:

 public static void BeginInvoke( Action d ) { ... } 

The compiler can convert the lambda expression to the corresponding delegate signature. But if the method is poorly printed:

 public static void BeginInvoke( Delegate d ) { ... } 

the compiler will not accept lambda. However, you can apply the lambda expression to a specific delegate subscription (such as Action), and then pass this to the method. The compiler cannot automatically do this for you, because there are many different types of delegates that might be a valid match for a lambda signature ... and the compiler does not know what will be right.

+10
source share

Pass it to a Delegate, such as an Action

 lvMyAssignments.Dispatcher.BeginInvoke((Action)(() => lvMyAssignments.ItemsSource = e.HandOverDocs))); 
+3
source share

All Articles