0

我正在用 C# 开发简单的 Windows 窗体应用程序

我想显示一个 GUI,然后在后台启动一些任务。我想在表单中使用文本标签显示这些任务的进度。

Program.cs 非常简单:

   static void Main()
    {      
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Form mainForm = (new Form1());

        Application.Run(mainForm);
    }

对于我使用绑定的标签

partial class Form1 : INotifyPropertyChanged
{
    public string Title
    {
        get { return _title; }
        set
        {
            _title = value;
            if(PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Title"));
            }
        }


private void InitializeComponent()
    {
        this.label1 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this, "Title", true));
        this.label1.Location = new System.Drawing.Point(12, 9);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(46, 17);
        this.label1.TabIndex = 0;
        this.label1.Text = "label1";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(763, 255);
        this.Controls.Add(this.label1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
        this.PerformLayout();


    }
 private string _title = "xxgg";
    private System.Windows.Forms.Label label1;

    public event PropertyChangedEventHandler PropertyChanged;

然后我想运行一个后台工作者,但由于某种原因,Form1_Load 方法没有被执行。

 private void Form1_Load(object sender, System.EventArgs e)
    {
        System.Console.WriteLine("Form loaded");

        var bw = new BackgroundWorker();
        bw.DoWork += bw_DoWork;
        bw.RunWorkerAsync(); //Start the worker
    }

void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        InfoText it = new InfoText();
        FileMover fm = new FileMover(it);

        foreach(string path in sourcePaths)
        {
            fm.MoveFrom(path);
            this.Title = it.text;
        }
    }

如您所见,我想在运行后台工作程序时更新标题。

我的 Form1_Load 没有被调用的原因是什么?谢谢

编辑: 我在 Form1 类中添加了 BackgroundWorker 代码,如下所示:

 public partial class Form1 : Form
{
    BackgroundWorker bw;

    public Form1()
    {
        InitializeComponent();           

        it = new InfoText();

        bw = new BackgroundWorker();
        bw.DoWork += bw_DoWork;
        bw.WorkerReportsProgress = true;
        bw.ProgressChanged += Bw_ProgressChanged;
        bw.RunWorkerCompleted += Bw_RunWorkerCompleted;
        bw.RunWorkerAsync(); //Start the worker
    }

    private void Bw_RunWorkerCompleted(object sender,   RunWorkerCompletedEventArgs e)
    {
        Console.WriteLine("done");

        this.Title = it.text;
    }

    private void Bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        Console.WriteLine("progress changed " + e.ProgressPercentage);

        this.Title = it.text;
    }

    void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        FileMover fm = new FileMover(it);

        foreach(string path in sourcePaths)
        {
            fm.MoveFrom(path);
            bw.ReportProgress(1);
        }
    }
}

现在它完全符合我的要求。

4

1 回答 1

0

添加

this.Load += new System.EventHandler(Form1_Load);

在 InitializeComponent... 或类构造函数内部。

我想通过 GUI 设计器添加它,它更安全、更容易。

于 2015-11-20T18:09:33.133 回答