简体   繁体   中英

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. 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. 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.

I'm pretty new to coding in general especially 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. 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.

As Jeroen mentioned, we need an example. You're trying to update a WPF UI based on the contents of [ cringe ] hard-coded Excel ranges. 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. Bind its ItemsSource to a List (or ObservableCollection ) of a class that holds all your pertinent data nicely. The DataGrid should do the rest as the AutoGenerateColumns is true by default.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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