简体   繁体   English

WPF 延迟异步更新 UI 控件?

[英]WPF update UI Control asynchronously with a delay?

public void TextChangedEvent(object sender, EventArgs e)
        {
            TextBox changedTextBox = sender as TextBox;
            List<TextBox> textBoxes = new List<TextBox>();
            //gets Grid that contains all the textboxes
            var parentGrid = VisualHierarchyHelper.FindParent<Grid>(changedTextBox);
            var mainGrid = VisualHierarchyHelper.FindParent<Grid>(parentGrid);
            //ALL 81 textboxes
            var textBoxChildren = VisualHierarchyHelper.FindVisualChildren<TextBox>(mainGrid).ToList();
            for (int i = 0; i < textBoxChildren.Count; i++)
            {
                SendWithDelay(textBoxChildren[i]);
            }
        }

        async Task SendWithDelay(TextBox txt)
        {
            await Task.Delay(1000);
            txt.Background = Brushes.Red;
        }

I want 81 grids to get a red background 1 by one, now it just waits a bit and updates everything at once.我想要 81 个网格一个一个地获得红色背景,现在它只需稍等一下并立即更新所有内容。

Any suggestions?有什么建议?

Note: This code is runnable but it does complain about SendWithDelay(textBoxChildren[i]);注意:此代码是可运行的,但它确实抱怨SendWithDelay(textBoxChildren[i]); not being awaited, if I change this and also change void to async Task it says Error CS0407 'Task EventHandling.TextChangedEvent(object, EventArgs)' has the wrong return type .没有等待,如果我更改此设置并将void更改为async Task它会显示Error CS0407 'Task EventHandling.TextChangedEvent(object, EventArgs)' has the wrong return type

EDIT编辑

This is what I have now:这就是我现在所拥有的:

public void TextChangedEvent(object sender, EventArgs e)
        {
            TextBox changedTextBox = sender as TextBox;
            List<TextBox> textBoxes = new List<TextBox>();
            //gets Grid that contains all the textboxes
            var parentGrid = VisualHierarchyHelper.FindParent<Grid>(changedTextBox);
            var mainGrid = VisualHierarchyHelper.FindParent<Grid>(parentGrid);
            //ALL 81 textboxes
            var textBoxChildren = VisualHierarchyHelper.FindVisualChildren<TextBox>(mainGrid).ToList();
            for (int i = 0; i < textBoxChildren.Count; i++)
            {
                x SendWithDelay(textBoxChildren[i]);
            }
        }

        async void SendWithDelay(TextBox txt)
        {
            txt.Background = Brushes.Red;   
            x await Task.Delay(500);
        } 

The breakpoints(x) just alternate between each other.断点(x)只是彼此交替。 Then when they're done everything gets red.然后当他们完成时,一切都会变红。

You can make make TextChangedEvent async:您可以使TextChangedEvent异步:

public async void TextChangedEvent(object sender, EventArgs e) {

And await the call to SendWithDelay :并等待对SendWithDelay的调用:

await SendWithDelay(textBoxChildren[i]);

It'll get rid of all the warnings you're getting, but it seems like you want SendWithDelay to be "Fire and forget", this way you can change SendWithDelay to async void instead of async Task :它将消除您收到的所有警告,但似乎您希望SendWithDelay为“即发即SendWithDelay ”,这样您就可以将SendWithDelay更改为async void而不是async Task

async Task SendWithDelay(TextBox txt) {

And not await every call to it:不是等待每次调用它:

SendWithDelay(textBoxChildren[i]);

1 second Delay 1 秒延迟

async Task LongRunningProcess()
{

    await Task.Delay(1000);

}

In your loop在你的循环中

public async void TextChangedEvent(object sender, EventArgs e)
{
  for (int i = 0; i < textBoxChildren.Count; i++)
  {
     txt.Background = Brushes.Red;
     await Task.Run(() => LongRunningProcess());
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM