文本框上的附加文本仅在最后显示在 wpf 中 [英] appendtext on text box only shown in the end all together in wpf

查看:27
本文介绍了文本框上的附加文本仅在最后显示在 wpf 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有一个简单的文本框.其他功能:另一个来自用户 textbox1 的输入和一个按钮.一旦用户在 textbox1 中输入一个值并按下按钮,我就会开始检查并向用户打印消息.我的问题是我没有实时看到这些消息,一次一个.消息立即出现,最后.我没有定义数据绑定,因为我认为既然它是一个简单的东西,我就不需要它,还是我错了?这是我程序的一小部分,它位于按钮单击事件处理程序中.

I have a simple textbox in my program. Other features : another input from the user textbox1, and a button. Once the user enters a value in textbox1, and presses the button, I start to check and print messages to the user. My problem is that I don't see those messages on real time, one at a time. The messages appear at once, in the end. I didn't define data binding, because I thought that since it's a simple thing, I wouldn't need it, or am i wrong ? This is a very small portion of my program, it's in the button click event handler.

MainText.AppendText("Starting Refiling...\u2028");
foreach (DocumentData doc in Docs)
{
    try
    {
        wsProxy.RefileDocument(doc);
        MainText.AppendText(String.Format("Refilling doc # {0}.{1}\u2028", doc.DocNum, doc.DocVer));
    }
    catch (Exception exc)
    {
        if (exc.Message.Contains("Document is in use") == true)
            MainText.AppendText(String.Format("There was a problem refilling doc # {0}, it is in use.\u2028",doc.DocNum));
        else
            MainText.AppendText(String.Format("There was a problem refilling doc # {0} : {1}.\u2028", doc.DocNum, exc.Message));
    }
}

推荐答案

您在 GUI 线程中执行所有循环/打印.基本上你是在给它展示新项目,而不是给它时间来展示它们.创建一个后台工作人员并让他在您发布的 foreach 循环.这应该允许 UI 线程在文本更改时更新视图,而不是在所有更改结束时更新一次.我发布的链接包含有关如何使用 backgroundworker 类的示例,但这是我要做的.

You're doing all your looping/printing in the GUI thread. Basically you're giving it new items to show and not giving it time to show them. Create a background worker and let him do the work in the foreach loop that you posted. This should allow the UI thread to update the view as the text changes, instead of getting the one update at the end with all your changes. The link I posted includes examples on how to use the backgroundworker class, but here's what I'd do.

创建一个后台工作者:

private readonly BackgroundWorker worker = new BackgroundWorker();

初始化他:

public MainWindow()
  {
     InitializeComponent();

     worker.DoWork += worker_DoWork;
  }

为他创建一个任务:

void worker_DoWork( object sender, DoWorkEventArgs e)
{
   // Set up a string to hold our data so we only need to use the dispatcher in one place
   string toAppend = "";
   foreach (DocumentData doc in Docs)
   {
      toAppend = "";
      try
      {
         wsProxy.RefileDocument(doc);
         toAppend = String.Format("Refilling doc # {0}.{1}\u2028", doc.DocNum, doc.DocVer);  
      }
      catch (Exception exc)
      {
         if (exc.Message.Contains("Document is in use"))
            toAppend = String.Format("There was a problem refilling doc # {0}, it is in use.\u2028",doc.DocNum);
         else
            toAppend = String.Format("There was a problem refilling doc # {0} : {1}.\u2028", doc.DocNum, exc.Message);
      }

      // Update the text from the main thread to avoid exceptions
      Dispatcher.Invoke((Action)delegate
      {
         MainText.AppendText(toAppend);
      });
   }
}

当你收到按钮按下事件时启动他:

Start him when you get the button press event:

private void Button_Click(object sender, RoutedEventArgs e)
  {
     worker.RunWorkerAsync();
  }

这篇关于文本框上的附加文本仅在最后显示在 wpf 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆