C#从第一个更新第二个窗口

时间:2015-11-10 11:56:19

标签: c# wpf window

如何从循环内的MainWindow中更新第二个窗口(它是带有进度条的简单WPF窗口以显示进度)?

好吧,我正在加载一些可能花费大约10秒的文件。加载时我希望我的ProgressWindow显示已加载了多少个文件。

这是我的MainThread:

private void loadFiles(String[] paths) {

        // Show Progress Window
        MyProgressWindow progWindow = new MyProgressWindow();
        progWindow.ShowProgressWindow(0, paths.Length);
        //Globals.MyProgressController.ShowProgressWindow(0, paths.Length);

        foreach (String path in paths) {

            // Load the file
            loadFile(path);

            // Refresh the Table
            refreshTableAsync();

            // Update Progress Window
            progWindow.UpdateProgressWindow(1);
            //Globals.MyProgressController.UpdateProgressWindow(1);
        }

        // Close Progress Window
        progWindow.CloseProgressWindow();
        //Globals.MyProgressController.CloseProgressWindow();
    }

这是ProgressWindowController

public class MyProgressWindow {
    private ProgressWindow progressWindow;

    public void ShowProgressWindow(int startIndex, int maxIndex) {
        progressWindow = new ProgressWindow();
        progressWindow.Initialize(startIndex, maxIndex);
        progressWindow.Show();
    }

    public void UpdateProgressWindow(int value) {
        progressWindow.Update(value);
        //progressWindow.Update();
    }

    public void CloseProgressWindow() {
        progressWindow.Close();
    }
}

最后是ProgressWindow本身:

public partial class ProgressWindow : Window {

    private int actualValue = 0;
    private int maxValue = 0;

    public ProgressWindow() {
        InitializeComponent();
    }

    public void Initialize(int startValue, int maxValue) {
        this.actualValue = startValue;
        this.maxValue = maxValue;
    }

    public void Update(int value) {
        actualValue += value;
        lblPercentage.Content = actualValue + " / " + maxValue;
        pbProgress.Value = ((double)value / maxValue) * 100;
    }
}

我读过thati应该通过线程来做,但是我无法让它工作,而且我找到的所有代码都帮助了其他人,并没有做我需要的。

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您提出的问题不正确。您正在根据您的问题更新第二个窗口,但建议使用单独的线程是您真正要求的。

我建议您阅读有关BackgroundWorker课程Here的内容,该课程甚至可以解释进度更新。

相关问题