在WPF中使用ProgressBar,简单应用程序

时间:2013-08-13 13:08:56

标签: c# wpf progress-bar

我一直试图理解这篇文章http://bensprogrammingwork.blogspot.com/2012/01/progressbar-in-wpf.html

我的Button_Click事件中包含以下代码:

FileInfo existingFile = new FileInfo("C:\\Users\\cle1394\\Desktop\\Apple Foreign Tax Payment Sample Layout Proposed - Sample Data.xlsx");

ConsoleApplication2.Program.ExcelData data = ConsoleApplication2.Program.GetExcelData(existingFile);

GetExcelData()方法需要几分钟才能完成,所以我想显示一个进度条,指出预计完成时间。

但是,我不知道如何将上述教程中的方法应用于我的GetExcelData()代码:

public static ExcelData GetExcelData(FileInfo file)
{
    ExcelData data = new ExcelData();
    data.Countries = new List<Country>();

    using (ExcelPackage xlPackage = new ExcelPackage(file))
    {
        // get the first worksheet in the workbook
        ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets[1];

        List<string> countryNames = new List<string> 
        { 
            "Australia"/*,
            "China - Beijing",
            "China - Shanghai",
            "Hong Kong",
            "Hungary",
            "Ireland",
            "Spain",
            "United Kingdom"*/
        };

        List<string> monthNames = new List<string>
        {
            "January",
            "February",
            "March",
            "April",
            "May",
            "June",
            "July",
            "August",
            "September",
            "October",
            "November",
            "December"
        };

        foreach (string name in countryNames)
        {
            Country country = new Country();

            Console.WriteLine(name);

            country.Name = name;
            country.Months = new List<Month>();

            foreach (string _name in monthNames)
            {
                country.Months.Add(GetMonthDataRows(_name, GetMonth(_name, GetCountry(name, worksheet), worksheet), worksheet));
            }

            country.Totals = GetTotals(country);

            data.Countries.Add(country);

            // this is where I would like to update the progressbar

        }

    } // the using statement calls Dispose() which closes the package.

    return data;
}

我想在上面的foreach循环中更新进度条。有人能告诉我一个如何做到这一点的例子吗?

3 个答案:

答案 0 :(得分:1)

我在WPF中做了类似的事情,但我想在List中显示更新消息而不阻塞UI线程see here。您可以使用消息替换此示例中的List。对不起,刚才没有时间写一个简化版本。

答案 1 :(得分:1)

在文章中,您将看到一个BackgroundWorker对象。首先,阅读MSDN上的BackgroundWorker Class

此对象提供了一种在单独的线程中执行长时间运行操作的简便方法。如果您不希望UI在执行操作时锁定,则必须在与UI线程分离的线程上执行长时间运行的操作。您是否使用控制台应用程序无关紧要。

在本文中,您还会看到ProgressChangedEventHandler被设置为构造函数中名为bw_ProgressChanged的方法。这个bw_ProgressChanged方法将在UI线程上调用,因此您可以在此处访问您的UI元素,即ProgressBar对象。

现在查看bw_DoWork方法......这是你在另一个线程(由BackgroundWorker创建)上完成工作(长时间运行的操作)的地方。现在,请注意从BackgroundWorkwer参数创建sender的行...这是必不可少的,因为您需要使用该对象从其他线程调用bw_ProgressChanged方法。这是使用ReportProgress类的BackgroundWorkwer方法完成的。

作为进度值传递的值取决于您,但必须介于您在ProgressBar控件中设置的最小值和最大值之间。我们的想法是,您定期传递一个值,该值会在您每次执行某项工作时增加,并以您将ProgressBar.Maximum设置为的值结束。

因此,上述示例的唯一连接是您在GetExcelData()方法中添加的DoWork方法。

答案 2 :(得分:1)

将以下代码复制到教程中的bw_dowork方法,它应该可以工作。

FileInfo existingFile = new FileInfo("C:\\Users\\cle1394\\Desktop\\Apple Foreign Tax Payment     Sample Layout Proposed - Sample Data.xlsx");

ConsoleApplication2.Program.ExcelData data =  ConsoleApplication2.Program.GetExcelData(existingFile); 

所以,他们正在做的是onStart_click他们启动后台异步任务,这是你需要在onClick方法中编写你当前正在做的事情。

相关问题