Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF调用控件_C#_Wpf_Invoke - Fatal编程技术网

C# WPF调用控件

C# WPF调用控件,c#,wpf,invoke,C#,Wpf,Invoke,如何使用参数调用控件?我已经用谷歌搜索过了,但是没有找到 这是我得到的错误: 其他信息:参数计数不匹配 当我简单地检查textbox控件的text属性是否为空时,就会发生这种情况。这在WinForms中工作: if (this.textboxlink.Text == string.Empty) SleepThreadThatIsntNavigating(5000); 它从这条线跳到catch块,并向我显示该消息 以下是我尝试调用控件的方式: // the delegate: priva

如何使用参数调用控件?我已经用谷歌搜索过了,但是没有找到

这是我得到的错误:

其他信息:参数计数不匹配

当我简单地检查textbox控件的text属性是否为空时,就会发生这种情况。这在WinForms中工作:

if (this.textboxlink.Text == string.Empty)
   SleepThreadThatIsntNavigating(5000);
它从这条线跳到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));
    }
}

我做错了什么?既然我只想读取控件的内容,那么我什么时候必须调用它呢?

调用invoke时,您没有指定参数(
text
)。当Dispatcher尝试运行您的方法时,它没有要提供的参数,您会得到一个异常

尝试:


如果要从文本框中读取值,一个选项是使用lambda:

string textBoxValue = string.Empty;

this.textboxlink.Dispatcher.Invoke(DispatcherPriority.Normal, 
     new Action( () => { textBoxValue = this.textboxlink.Text; } ));

if (textBoxValue == string.Empty)
    Thread.Sleep(5000);

Reed是正确的,但您需要这样做的原因是GUI元素不是线程安全的,因此所有GUI操作都必须在GUI线程上完成,以确保正确读取内容。对于这样的读取操作,这一点不太明显,但是对于写入操作,这是非常必要的,因此.NET framework只要求在GUI线程中完成对GUI的所有访问。

好的,我解决了这个问题,这是我的第一个问题。但是我如何读取文本框的内容呢?通常,在单独的线程中调用work函数之前,您会读取它……但是我必须不断地读取它。不只是在它之前。工作线程是一个无止境的线程,只有在程序关闭时才会停止。此应用程序在WinForm中运行良好,我正在将其迁移到WPF@Yustme:从技术上讲,这在Windows窗体中也可能是一个问题。。。你用的是什么版本的C#/VS?好的,谢谢。我想我需要在代码中做一个极端的修改。仍然不起作用。
string textBoxValue = string.Empty;

this.textboxlink.Dispatcher.Invoke(DispatcherPriority.Normal, 
     new Action( () => { textBoxValue = this.textboxlink.Text; } ));

if (textBoxValue == string.Empty)
    Thread.Sleep(5000);