如何调用带有参数的控件?我用谷歌搜索了这个,但无处可寻!
这是我得到的错误:
Additional information: Parameter count mismatch.
当我简单地检查文本框控件的文本属性是否为空时,就会发生这种情况。这适用于 WinForms:
if (this.textboxlink.Text == string.Empty)
SleepThreadThatIsntNavigating(5000);
它从 this if 行跳转到 catch block 并向我显示该消息。
这就是我尝试调用控件的方式:
// 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));
}
}
我做错了什么?从什么时候开始,当我只想阅读其内容时必须调用控件?
最佳答案
调用 Invoke 时,您并未指定参数 (text
)。当 Dispatcher 尝试运行您的方法时,它没有要提供的参数,您会得到一个异常。
尝试:
this.textboxlink.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
new TBXTextChanger(this.WriteToTextBox), text);
如果您想从文本框中读取值,一种选择是使用 lambda:
string textBoxValue = string.Empty;
this.textboxlink.Dispatcher.Invoke(DispatcherPriority.Normal,
new Action( () => { textBoxValue = this.textboxlink.Text; } ));
if (textBoxValue == string.Empty)
Thread.Sleep(5000);
关于c# - WPF 调用控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4016921/