【问题标题】:WPF: UI Element is NOT UpdatingWPF:UI 元素未更新
【发布时间】:2018-05-07 00:24:05
【问题描述】:

我有一个 BackgroundWorker,在该工作人员中我正在从 excel 文件中读取数据。如果 excel 文件中存在错误,worker 完成然后呈现另一个表单,用户可以在其中输入更正,然后按 Ok,然后从头开始再次运行 worker。当工作人员成功完成时,它应该更新我的主窗口上的标签,说它已经加载了 excel。但标签不会更新。当我调试它时,我可以看到更新标签的代码运行,但它根本不起作用。 请帮忙,这让我发疯了!

这是我的代码。

    private void worker_ReadFileData(object sender, DoWorkEventArgs e) {

        for (int j = 1; j < rcount + 1; j++) {
            worker.ReportProgress(j);

            // Do work


            if (j == 1) {
                ColumnIndex column = this.ValidateColumnIndexes(tableType);
                if (column != null) {    // If error in file, complete worker
                    fileData.isDataLoaded = false;
                    e.Result = fileData;
                    return;
                }
            }
        }

        fileData.isDataLoaded = true;
        e.Result = fileData;            // Pass the data to the completed method.

    }


    private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
        if (e.Error != null) {
        } else if (e.Cancelled) {
        } else {

            FileData fileData = (FileData) e.Result;

            if (fileData.isDataLoaded == true) {
                testLabel.Content = "It works!";
            } else {
                // Show Dialog where user can input the correction
                ColumnIndexPrompt columnIndexPrompt = new ColumnIndexPrompt(fileData.FilePath, fileData.FileExtension, fileData.TableType, fileData.Column);
                columnIndexPrompt.ShowDialog();
            }
        }
    }

    public void TriggerReadDataFile(string filePath, string fileExt, int tableType) {
        progBar.Value = 0;
        // Read the file data and populate the Registrars list, then show the datagrid
        worker.RunWorkerAsync(new FileData(filePath, fileExt, tableType));
    }

编辑: 这是我打开的第二个窗口中的代码(在上面的代码中使用 .ShowDialog())

    public ColumnIndexPrompt(string filePath, string fileExt, int tableType, ColumnIndex column) {
        InitializeComponent();

        this.filePath = filePath;
        this.fileExt = fileExt;
        this.tableType = tableType;
        this.column = column;

        lblColumnIndexErrorMsg.Text = column.ErrorMsg;
    }

    private void btnColumnIndexApply_Click(object sender, RoutedEventArgs e) {
        MainWindow originalForm = new MainWindow();
        int correctColumnNumber = int.Parse(txtColumnIndexCorrection.Text);
        column.Index = correctColumnNumber - 1;
        originalForm.UpdateSingleColumnIndex(column);
        originalForm.TriggerReadDataFile(this.filePath, this.fileExt, this.tableType);
        this.Close();
    }

【问题讨论】:

  • 您是否尝试过使用调度程序调用代码来更新文本?
  • 使用 Live Visual Tree 和 Live Property Explorer 调试您的标签。
  • @WPInfo 不,我没有。老实说,我什至不知道这是什么对不起。
  • @tabby 没听说过。我会调查的,谢谢!
  • @mjwills 是的,代码运行但没有任何反应。太诡异了!

标签: c# .net wpf windows user-interface


【解决方案1】:

您正在再次创建 MainWindow 对象,只需将其更改为:

MainWindow mainWindow =  Application.Current.MainWindow;

或者

MainWindow mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();

这里:

private void btnColumnIndexApply_Click(object sender, RoutedEventArgs e) 
{
    MainWindow originalForm = (MainWindow)Application.Current.MainWindow; //here
    int correctColumnNumber = int.Parse(txtColumnIndexCorrection.Text);
    column.Index = correctColumnNumber - 1;
    originalForm.UpdateSingleColumnIndex(column);
    originalForm.TriggerReadDataFile(this.filePath, this.fileExt, this.tableType);
    this.Close();
}

【讨论】:

  • 我认为你肯定在做某事!虽然,当我使用该代码时,Application.Current.MainWindow 出现错误;说:无法将类型“System.Windows.Window”隐式转换为“MyProject.MainWindow”
  • 你的启动窗口是什么?
  • 启动窗口名为MainWindow(命名空间:MyProject)
  • 尝试使用 (MainWindow)Application.Current.Windows.OfType().FirstOrDefault();
  • 我在使用该代码时遇到了错误。我试过了:MainWindow originalForm = (MainWindow)Application.Current.MainWindow; 它有效! WOOOOO 非常感谢!
猜你喜欢
  • 2014-08-01
  • 1970-01-01
  • 2022-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多