实时循环计数器输出wpf

发布于 2024-08-12 06:21:56 字数 1995 浏览 1 评论 0原文

如果我想要一个文本框实时表示 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();
    }
}

}

What can I do if I want to have a text-box representing in real time the value of a loop counter in wpf?

appending working solution:

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

        }

Here is the code I tried according to your suggestion, but it doesnt work, I get "0" written in the textbox, so I suppose the binding is OK, but the loop doesnt work. I also made the loop to write into another textbox directly by textbox2.text="a" inside the loop, but it didnt work either.

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫瑟鸿黎 2024-08-19 06:21:56
  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();
}
  1. Run the loop in a background process (so that the UI can update itself while the loop is running) and

  2. write the loop counter into a property (DependencyProperty or CLR property with INotifyPropertyChanged) which is bound to a TextBox in your user interface. Alternatively, you can directly change the value of the TextBox via Dispatcher.Invoke (this is less elegant, though).

Does this help? Feel free to ask for clarification...


Code example (untested), using a DependencyProperty (which must be bound to a 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();
}
离鸿 2024-08-19 06:21:56

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

you can use Data Binding for continuously updating the text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文