简体   繁体   English

来自 Excel 的 WPF UI 更新

[英]WPF UI Update from excel

I've got a Loading page that starts up (typically I have my home page load it on startup but I've swapped it recently).我有一个正在启动的加载页面(通常我的主页在启动时加载它,但我最近交换了它)。 I have an excel file with tons of data in it.我有一个 excel 文件,里面有大量数据。 I have a class for dumping that data.我有一个用于转储该数据的课程。 I also have a class for each of the lists I want to create.我还为我要创建的每个列表创建了一个类。 The loading page should be updating it's percentage as it goes.加载页面应该随着它的进行更新它的百分比。

I was able to get it to work in the example below.我能够让它在下面的例子中工作。

public partial class Window1 : Window
{
    public MainController main = new MainController();
    public ExcelImporter tempExcel = new ExcelImporter();
    List<Character.MainRace> listRaces = new List<Character.MainRace>();
    List<string> tempString = new List<string>();

    public Window1()
    {
        InitializeComponent();
        BackgroundWorker worker = new BackgroundWorker();
        worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        worker.WorkerReportsProgress = true;
        worker.DoWork += worker_DoWork;
        worker.ProgressChanged += worker_ProgressChanged;
        worker.RunWorkerAsync();
    }

    private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBarTest.Value = e.ProgressPercentage;
        textBlockPercent.Text = e.ProgressPercentage.ToString() + "%";
        textBlockTest.Text = (string)e.UserState;
    }

    private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        tempExcel.Create();
        tempExcel.SetSheet("Races");
        var worker = sender as BackgroundWorker;
        worker.ReportProgress(0, String.Format("Process Iteration 1."));
        List<string> myRaceName = tempExcel.GetRangeValue("A2", "A46");
        List<string> myRaceShortDescription = tempExcel.GetRangeValue("N2", "N46");
        for (int raceSelection = 0; raceSelection < myRaceName.Count - 1; raceSelection++)
        {
            Character.MainRace tempRace = new Character.MainRace();
            tempRace.Race = myRaceName[raceSelection];
            double percentage = ((double)raceSelection / (double)myRaceName.Count) * 100.0;
            Thread.Sleep(10);
            worker.ReportProgress((int)percentage, String.Format("Processing " + myRaceName[raceSelection]));
            tempRace.ShortDescription = myRaceShortDescription[raceSelection];
            listRaces.Add(tempRace);
        }
        worker.ReportProgress(100, "Done Processing");
    }

    private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("All Done!");
        progressBarTest.Value = 0;
        textBlockTest.Text = "";
    }
}

There's really two places where I need the UI to update.确实有两个地方我需要更新 UI。 Each time I'm loading a new set of data for each loader like Human, elf, etc for race and fighter, paladin, thief, etc for class.每次我为每个加载器加载一组新数据时,如人类、精灵等,用于种族和战士、圣骑士、小偷等。 As well as when I move on to the next set of data like Classes Race, Feats, etc. (though to keep this a bit shorter I only included showing just 1 for race.以及当我继续处理下一组数据时,例如 Classes Race、Feats 等(虽然为了保持这个更短一些,我只包括仅显示 1 个种族。

I'm pretty new to coding in general especially wpf.我对一般编码很陌生,尤其是 wpf。 I've been going through bits a pieces of tutorials.我一直在经历一些教程。 I've tried using INotifyproperty,background worker, dispatcher, binding and not binding the content.我试过使用 INotifyproperty、后台工作人员、调度员、绑定和不绑定内容。 I'm just not getting it.我只是不明白。 Everything works separately but not together.一切都单独工作,但不能一起工作。 From what I understand, it's because the UI thread which is the main thread getting held up because of my long process.据我了解,这是因为作为主线程的 UI 线程由于我的漫长过程而被阻塞。

As Jeroen mentioned, we need an example.正如 Jeroen 提到的,我们需要一个例子。 You're trying to update a WPF UI based on the contents of [ cringe ] hard-coded Excel ranges.您正在尝试根据 [ cringe ] 硬编码 Excel 范围的内容更新 WPF UI。 I'm guessing the workbook import part works, but the UI is freezing?我猜工作簿导入部分有效,但用户界面冻结了? or not updating?还是不更新?

First, try using a DataGrid to display your data on the UI.首先,尝试使用DataGrid在 UI 上显示您的数据。 Bind its ItemsSource to a List (or ObservableCollection ) of a class that holds all your pertinent data nicely.将其ItemsSource绑定到一个类的List (或ObservableCollection ),该类可以很好地保存所有相关数据。 The DataGrid should do the rest as the AutoGenerateColumns is true by default. DataGrid应该完成剩下的工作,因为AutoGenerateColumns默认为 true。

Then, for further help, you'll have to attempt something and provide a reproducible example of what's failing.然后,为了获得进一步的帮助,您必须尝试一些东西并提供一个可重现的失败示例。 Showing the implementation of your BackgroundWorker would provide some clues on holding the main UI thread.显示您的BackgroundWorker的实现将提供一些关于保持主 UI 线程的线索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM