从不同的线程中读取多个WPF文本框控件中的文本 [英] Reading text from multiple WPF textbox controls from a different thread
问题描述
在我的WPF应用程序中,我需要从几个文本框中读取文本.因为代码在与UI不同的线程中运行,所以我需要使用Dispatcher.invoke()
.
In my WPF application I need to read text from several textboxes. Because the code is running in a different thread to the UI, I need to use Dispatcher.invoke()
.
目前,我正在使用一个效果很好的文本框,但现在我需要所有文本.我是否需要为每个文本框编写Dispatcher.invoke
,或者是否可以编写函数,以便传递文本框控件引用并返回文本?
At the moment I’m working with one textbox which works fine, but now I need all the texts. Do I need to write a Dispatcher.invoke
for each textbox or is there a way to write a function so I pass in a textbox control reference and it returns the text?
推荐答案
您只需在同一Invoke调用中从所有TextBox字段中获取文本即可.
You could just grab the text from all the TextBox fields in the same Invoke call.
public MainWindow()
{
InitializeComponent();
Thread thread = new Thread(new ThreadStart(this.ThreadFunc));
thread.Start();
}
private delegate void InvokeDelegate();
private void ThreadFunc()
{
Dispatcher.Invoke(new InvokeDelegate(() =>
{
Debug.WriteLine(this.textBox1.Text + this.textBox2.Text);
}));
}
没有理由您必须打多个电话.
There's no reason you would have to make multiple calls.
这篇关于从不同的线程中读取多个WPF文本框控件中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!