【问题标题】:real time loop counter ouput wpf实时循环计数器输出 wpf
【发布时间】:2009-11-24 15:43:42
【问题描述】:

如果我想让一个文本框实时表示 wpf 中循环计数器的值,我该怎么办?

附加工作解决方案:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private delegate void UpdateTextBox(DependencyProperty dp, Object value);
...
private void MyMethod()
{
    ...
    int iMax=...;
    ...
    MyClass iMyClass = new MyClass(arguments);
        this.DataContext = iMyClass;
        UpdateTextBox updateTBox = new UpdateTextBox(textBlock1.SetValue);
        for (int i = 1; i <= iMax; i++)
        {
            iMyClass.MyClassMethod(i);
            Dispatcher.Invoke(updateTBox, System.Windows.Threading.DispatcherPriority.Background, new object[] { MyClass.MyPropertyProperty, iMyClass.myProperty });

        }

这是我根据您的建议尝试的代码,但它不起作用,我在文本框中写了“0”,所以我想绑定没问题,但循环不起作用。我还通过循环内的 textbox2.text="a" 使循环直接写入另一个文本框,但它也不起作用。

/// <summary>
/// Interaction logic for Window1.xaml
/// DOESNT WORK PROPERLY
/// </summary>
public partial class Window1 : Window
{


    public Window1()
    {
        InitializeComponent();
        TestClass tTest = new TestClass();
        this.DataContext = tTest ;
        tTest.StartLoop();
    }
}
public class TestClass : DependencyObject
{
    public TestClass()
    {
        bwLoop = new BackgroundWorker();

        bwLoop.DoWork += (sender, args) =>
        {

            // do your loop here -- this happens in a separate thread
            for (int i = 0; i < 10000; i++)
            {
                LoopCounter=i;
            }
        };
    }
    BackgroundWorker bwLoop;

    public int LoopCounter
    {
        get { return (int)GetValue(LoopCounterProperty); }
        set { SetValue(LoopCounterProperty, value); }
    }

    public static readonly DependencyProperty LoopCounterProperty = DependencyProperty.Register("LoopCounter", typeof(int), typeof(TestClass));

    public void StartLoop()
    {
        bwLoop.RunWorkerAsync();
    }
}

}

【问题讨论】:

    标签: wpf loops real-time


    【解决方案1】:
    1. 在后台进程中运行循环(以便 UI 可以在循环运行时自行更新)和

    2. 将循环计数器写入绑定到用户界面中的 TextBox 的属性(DependencyProperty 或带有 INotifyPropertyChanged 的​​ CLR 属性)。或者,您可以通过 Dispatcher.Invoke 直接更改 TextBox 的值(虽然这样不太优雅)。

    这有帮助吗?随时要求澄清...


    代码示例(未经测试),使用 DependencyProperty(必须绑定到 TextBox):

    BackgroundWorker bwLoop;
    public static readonly DependencyProperty LoopCounterProperty = 
        DependencyProperty.Register("LoopCounter", typeof(int),
        typeof(Window1), new FrameworkPropertyMetadata(0));
    
    public int LoopCounter  {
        get { return (int)this.GetValue(LoopCounterProperty); }
        set { this.SetValue(LoopCounterProperty, value); } 
    }
    
    private MyWindow() {
        ...
        bwLoop = new BackgroundWorker();
    
        bwLoop.DoWork += (sender, args) => {
            ...
            for (int i = 0; i < someLimit; i++) {
                Dispatcher.Invoke(new Action(() => LoopCounter=i));
                System.Threading.Thread.Sleep(250); // do your work here
            }
        }
    
        bwLoop.RunWorkerCompleted += (sender, args) => {
            if (args.Error != null)
                MessageBox.Show(args.Error.ToString());
        };
    }
    
    private void StartLoop() {
        bwLoop.RunWorkerAsync();
    }
    

    【讨论】:

    • 我设法用 DependancyProperty 和 Dispatcher.Invoke 做到了。您能否详细说明第一种可能性 - 在后台进程中运行循环并将循环计数器写入依赖属性中。和我做的不一样吗?
    • 我添加了一个代码示例,因此您可以将其与您的代码进行比较。如果您将代码添加到您的问题中,我可以对此发表评论。
    • 好的,你给我的真的看起来很优雅,而且很容易理解。我现在要睡觉了(这里是凌晨 3 点)。明天去看看,非常感谢!!!
    • 我明白了,谢谢。你怎么称呼我的方法?如果你只是通过调用它来完成它,我会有点惊讶它的工作原理,因为它与应用程序的其余部分在同一个线程中运行。
    • 实际上 MyMethod 是一个 button_OnClick 方法,当我单击窗口上的按钮时运行
    【解决方案2】:

    您可以使用数据绑定来不断更新文本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-06
      • 1970-01-01
      • 1970-01-01
      • 2016-02-27
      • 2014-10-05
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      相关资源
      最近更新 更多