WPF调用控制 [英] WPF invoke a control
问题描述
我如何可以调用一个带参数的控制?我GOOGLE了这件事,但没有去哪里找!
How can i invoke a control with parameters? I've googled this up, but no where to find!
这是错误我得到:
更多信息:参数数量不匹配
Additional information: Parameter count mismatch.
和发生这种情况时,我做一个简单的检查一个TextBox控件的Text属性是否为空。这在WinForms的:
And this happens when i do a simple check whether the text property of a textbox control is empty or not. This works in WinForms:
if (this.textboxlink.Text == string.Empty)
SleepThreadThatIsntNavigating(5000);
从这个跳跃如果行catch块,并表明我的消息。
It jumps from this if line to the catch block and shows me that message.
这是我如何尝试调用控件:
This is how i try to invoke the control:
//委托:
私人委托无效TBXTextChanger(串文本);
// 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));
}
}
我在做什么错了?而且,由于我什么时候来调用控件时,我只是想读的内容?
What am I doing wrong? And since when do i have to invoke a control when i just want to read its content?
推荐答案
当你调用调用,你再没有指定你的论点(文本
)。当调度员试图运行你的方法,它没有一个参数提供,你会得到一个异常
When you call Invoke, you're not specifying your argument (text
). When the Dispatcher tries to run your method, it doesn't have a parameter to supply, and you get an exception.
尝试:
this.textboxlink.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
new TBXTextChanger(this.WriteToTextBox), text);
如果您想从文本中读取的值中,一种选择是使用lambda:
If you want to read the value from a text box, one option is to use a lambda:
string textBoxValue = string.Empty;
this.textboxlink.Dispatcher.Invoke(DispatcherPriority.Normal,
new Action( () => { textBoxValue = this.textboxlink.Text; } ));
if (textBoxValue == string.Empty)
Thread.Sleep(5000);
这篇关于WPF调用控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!