【问题标题】:Reading text from multiple WPF textbox controls from a different thread从不同线程的多个 WPF 文本框控件中读取文本
【发布时间】:2018-09-29 06:27:59
【问题描述】:

在我的 WPF 应用程序中,我需要从多个文本框中读取文本。因为代码运行在与 UI 不同的线程中,所以我需要使用 Dispatcher.invoke()

目前我正在使用一个可以正常工作的文本框,但现在我需要所有文本。我需要为每个文本框写一个Dispatcher.invoke 还是有办法编写一个函数,以便我传入一个文本框控件引用并返回文本?

【问题讨论】:

    标签: wpf multithreading wpf-controls


    【解决方案1】:

    您可以在同一个 Invoke 调用中从所有 TextBox 字段中获取文本。

    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);
        }));
    }
    

    没有理由你必须打多个电话。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      • 2010-11-28
      • 1970-01-01
      • 2018-01-20
      相关资源
      最近更新 更多