那里。 我正在使用C#.wpf,并且我从C#源代码获取了一些代码,但是我无法使用它。有什么我必须改变?还是做?线程在C#中调用.WPF
// Delegates to enable async calls for setting controls properties
private delegate void SetTextCallback(System.Windows.Controls.TextBox control, string text);
// Thread safe updating of control's text property
private void SetText(System.Windows.Controls.TextBox control, string text)
{
if (control.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
Invoke(d, new object[] { control, text });
}
else
{
control.Text = text;
}
}
如上码,该错误是在InvokeRequired
和Invoke
目的是,我有一个文本框,其是内容,将递增为每个进程。
这里是文本框的代码。 SetText(currentIterationBox.Text = iteration.ToString());
代码有什么问题吗?
感谢您的帮助
编辑
// Delegates to enable async calls for setting controls properties
private delegate void SetTextCallback(System.Windows.Controls.TextBox control, string text);
// Thread safe updating of control's text property
private void SetText(System.Windows.Controls.TextBox control, string text)
{
if (Dispatcher.CheckAccess())
{
control.Text = text;
}
else
{
SetTextCallback d = new SetTextCallback(SetText);
Dispatcher.Invoke(d, new object[] { control, text });
}
}
而究竟是什么错误? – 2011-04-27 11:40:00
你有没有得到任何错误,或者你只假定你的代码有错误? – sikender 2011-04-27 11:41:51
有什么建议吗?错误'System.Windows.Controls.TextBox'不包含'InvokeRequired'的定义,并且没有找到接受'System.Windows.Controls.TextBox'类型的第一个参数的扩展方法'InvokeRequired'(你是否缺少使用指令或程序集引用?) – Reza 2011-04-27 11:43:30