如何在另一个C#WPF类的循环中返回一个值 [英] How do I return a value in a loop from another class C# WPF

查看:59
本文介绍了如何在另一个C#WPF类的循环中返回一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的wpf项目中有另一个公共类,我希望动态地从循环中返回值。循环完成后我不想这样做



我尝试过:



I have another public class in my wpf project and I want to return the value from the loop on the fly. I don't want to do it after the loop is completed

What I have tried:

public MainWindow()
{
   void ShowNumbers()
{
   foreach (var item in Numbers.Number.Generate)
  {
  txtbox1.Text = item;
  }
 }
}
public class Numbers
{
  public Number()
   {
      for (int i = 0, i < 100000, i++)
       {
         return i;
       }
    }
}
}

推荐答案

有点疯狂的想法,但您可能希望使用 yield(C#Reference) [ ^ ]。
A bit of a crazy idea, but yo may like to look into using yield (C# Reference)[^].


不知道你想做什么,但是这段代码

Don't know what you want to do, but this code
public Number()
{
   for (int i = 0, i < 100000, i++)
    {
      return i;
    }
}



可以简化为


can be simplified to

public Number()
{
    return 0;
}


您可以尝试创建一个事件来触发每次迭代,然后在监听类中处理它。 />


只需确保如果您在调度程序线程上更新了UI,或者您将收到错误。
You could try creating an event that fires every iteration and then handling that in the 'listening' class.

Just make sure that if you're updated the UI you do so on the dispatcher thread or you'll get errors.


这篇关于如何在另一个C#WPF类的循环中返回一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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