c# - WPF 调用控件

标签 c# wpf invoke

如何调用带有参数的控件?我用谷歌搜索了这个,但无处可寻!

invoke ui thread

这是我得到的错误:

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/

相关文章:

c# - 调用线程无法访问此对象,因为另一个线程拥有它

google-cloud-platform - 如何从 bq 命令行工具调用 BigQuery 过程?

c# - Windows窗体中dll的调用方法

c# - 通过命令断开登录的远程桌面用户

c# - 使用泛型方法而不是方法参数

wpf - 无法加载程序集,来自 HRESULT : 0x80131040 的异常

wpf - 如何从 Model 与 ViewModel 通信

c# - 当 DataGrid 中自动生成列为 true 时如何进行文本换行

c# - ASP.NET Identity中UserStore和UserManager的使用有什么区别?

c# - 泛型类型的隐式运算符