如何使用参数调用控件?我用Google搜索了这个,但无处可寻!
这是我得到的错误:
附加信息:参数计数不匹配.
当我简单地检查文本框控件的text属性是否为空时,会发生这种情况.这适用于WinForms:
if (this.textboxlink.Text == string.Empty)
SleepThreadThatIsntNavigating(5000);
Run Code Online (Sandbox Code Playgroud)
如果该行到catch块并且向我显示该消息,它会从此跳转.
这是我尝试调用控件的方式:
// the delegate:
private delegate void TBXTextChanger(string text);
private void WriteToTextBox(string text)
{
if (this.textboxlink.Dispatcher.CheckAccess())
{
this.textboxlink.Text = text;
}
else
{
this.textboxlink.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
new TBXTextChanger(this.WriteToTextBox));
}
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?而且,当我只想阅读其内容时何时需要调用控件?
Ree*_*sey 24
当你调用Invoke时,你没有指定你的参数(text).当Dispatcher尝试运行您的方法时,它没有要提供的参数,并且您会收到异常.
尝试:
this.textboxlink.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
new TBXTextChanger(this.WriteToTextBox), text);
Run Code Online (Sandbox Code Playgroud)
如果要从文本框中读取值,一个选项是使用lambda:
string textBoxValue = string.Empty;
this.textboxlink.Dispatcher.Invoke(DispatcherPriority.Normal,
new Action( () => { textBoxValue = this.textboxlink.Text; } ));
if (textBoxValue == string.Empty)
Thread.Sleep(5000);
Run Code Online (Sandbox Code Playgroud)