I am working on a WPF application in VS2008 and decided to reuse some code from another WPF application. However I have a strange issue with the following line:
Message.Dispatcher.Invoke(() => { Message.Text = "Looking for orders..."; });
This code works fine in the original application but throws the normal “Cannot convert lambda expression to type ‘System.Delegate’ because it is not a delegate type” error in the new application.
I know I can cast the expression as an Action to get it to work; but I am curious as to why the same piece of code compiles and works in one project but not another.
There might be an extension method defined somewhere in the old project that handles the casting!
Something like:
Then you could just do the following without trouble:
Update:
Turns out the .NET Framework has a set of extension methods for Dispatcher that can handle these kind of things build in already.
http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcherextensions.aspx