问题
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()
.
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?
回答1:
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.
来源:https://stackoverflow.com/questions/2676605/reading-text-from-multiple-wpf-textbox-controls-from-a-different-thread