There are 2 programs. One WinPhone application other WPF. In both use Dispatcher
to access UI elements from the stream. Here is the code of WinPhone
Dispatcher.BeginInvoke (() = & gt; RateTextBox.Text = rate)
It all works. Dispatcher
is the object, there is a method BeginInvoke
.
Here is the code WPF
Dispatcher.CurrentDispatcher.BeginInvoke (() = & gt; textBox1.Text = content)
where Dispatcher
is a class, not an object. I can not understand why. I tried to test different design, but in the end there is an error
Error 2 Can not convert “lambda expression” of a type “System.Delegate”, because it is not a delegate
I can not understand why the error and why in two projects “different” Dispatcher’y, in the WinPhone application podrubleny their mobile library from the box, added nothing of his own.
In WPF Added WindowsBase.dll where I got the link to System.Windows.Threading
where it is Dispatcher
.
Who can explain the situation? And I’m more than a week I bet on these issues. Why WinPhone everything works fine as it is necessary, and WPF is not.
Added after:
Now all kompilitsa, but does not display the query results. I have tried the code on the console application, which do not require Dispatcher. Such code
(content = & gt; Console.WriteLine (content))
It all works. But I added Dispatcher.
(content = & gt; Dispatcher.CurrentDispatcher.BeginInvoke ((Action) (() = & gt; Console.WriteLine ( content))))
And yet the console.
Answer 1, Authority 100%
You need to explicitly specify the delegate type – all but a particular type. BeginInvoke
takes Delegate
– delegate any type.
To compile the lambda as a delegate to the compiler needs to know the type. He did not bring any of the signature of the method – because there is any type is allowed – neither of lambda – because there is no single mechanism of withdrawal of a specific delegate type only the parameters and return values. Here he falls in error.
Dispatcher.CurrentDispatcher.BeginInvoke ((Action) (() = & gt; textBox1.Text = content));
p.s. Do not use Invoke
– namuchaetes. Use async / await .
Answer 2, Authority 75%
WPF was written in the era of pre-generic, and it left a lot of legacy:.. Netipizirvoannye collection, vaguely typed delegates etc. WinRT created already in the modern era, so it does have an adequate method overloading. Treated it simply – adding extension-methods. For example:
public static class Exts
{
public static void BeginInvoke (this Dispatcher @this, Action action)
{
@ This.BeginInvoke (action);
}
public static void BeginInvoke (this Dispatcher @this, DispatcherPriority priority, Action action)
{
@ This.BeginInvoke (action, priority);
}
}
If there is such a class will work the usual overload you.
However, as far as possible should be used adapted for async / await method – InvokeAsync
:
await Dispatcher.InvokeAsync (() = & gt; RateTextBox.Text = rate);
Answer 3, Authority 50%
The compiler can not infer from your lambda delegate specific, and therefore indicates an error. Will cast it like this:
Dispatcher.CurrentDispatcher.BeginInvoke ((Action) (() = & gt; RateTextBox.Text = rate));
Answer 4, Authority 12%
On the console question. Dispatcher
is not some kind of magic, but a class that gives access to the standard Windows message loop. In the console application, no one spins this loop in the main thread – so the Dispatcher
does not work.