我只是在WPF中使用ProgressBar进行新的woking。我有这样的用户控件:
public partial class Import : UserControl
{
public Import()
{
InitializeComponent();
}
private void filePickerButton_Click(object sender, RoutedEventArgs e)
{
// Create the OpenFIleDialog object
Microsoft.Win32.OpenFileDialog openPicker = new Microsoft.Win32.OpenFileDialog();
// Add file filters
// We are using excel files in this example
openPicker.DefaultExt = ".xslt";
openPicker.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
// Display the OpenFileDialog by calling ShowDialog method
Nullable<bool> result = openPicker.ShowDialog();
// Check to see if we have a result
if (result == true)
{
// Application now has read/write access to the picked file
filePathTextBox.Text = openPicker.FileName.ToString();
}
}
private void btn_Import_Click(object sender, RoutedEventArgs e)
{
//import all data from excel file to datatable
Workbook wb = new Workbook(filePathTextBox.Text);
// Accessing the worksheet in the Excel file
Worksheet worksheetPro = wb.Worksheets[1];
Worksheet worksheetCat = wb.Worksheets[0];
// Exporting all data by ExportDataTable
DataTable dataTablePro = worksheetPro.Cells
.ExportDataTable(1, 0, worksheetPro.Cells.Rows.Count - 1, worksheetPro.Cells.Columns.Count, false);
DataTable dataTableCat = worksheetCat.Cells
.ExportDataTable(1, 0, worksheetCat.Cells.Rows.Count - 1, worksheetCat.Cells.Columns.Count, false);
//dump data from datatable to SQL server
string connectionString = @"Data Source=DESKTOP-L6OBVA4\SQLEXPRESS;Initial Catalog=QLDB;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
{
//mapping columns of Datatable with the name of DB
bulkCopy.ColumnMappings.Add(dataTablePro.Columns[0].ColumnName, "Tên");
bulkCopy.ColumnMappings.Add(dataTablePro.Columns[1].ColumnName, "Giá");
bulkCopy.ColumnMappings.Add(dataTablePro.Columns[2].ColumnName, "Số Lượng");
bulkCopy.ColumnMappings.Add(dataTablePro.Columns[3].ColumnName, "Miêu Tả");
//set the destination table name in DB will be affected
bulkCopy.DestinationTableName = "dbo.Products";
try
{
//coppy all rows from nominated datatable and dump it to DB
bulkCopy.WriteToServer(dataTablePro);
dataTablePro.Clear();
using (SqlBulkCopy bulkCopyCat = new SqlBulkCopy(connection))
{
bulkCopyCat.ColumnMappings.Add(dataTableCat.Columns[0].ColumnName, "Loại");
bulkCopyCat.DestinationTableName = "dbo.Categories";
bulkCopyCat.WriteToServer(dataTableCat);
dataTableCat.Clear();
MessageBox.Show("Success!!!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}
此用户控件的用途是:选择一个excel文件,然后将所有数据导入datatable,最后将所有数据转储到SQL Server。我需要为从数据表到SQL服务器的转储操作创建一个处理栏,因为我认为这需要很长时间。接下来,我创建了一个Process bar窗口:
public partial class ProgressBar : Window
{
public ProgressBar()
{
InitializeComponent();
}
private void Window_ContentRendered(object sender, EventArgs e)
{
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += worker_DoWork;
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerAsync();
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 100; i++)
{
(sender as BackgroundWorker).ReportProgress(i);
//do my operation here
}
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pbStatus.Value = e.ProgressPercentage;
}
}
你可以看到上面的代码段,我有一个名为的方法:
worker_DoWork()
这是我计划进行操作的地方。最后,我想从用户控件中获取这些代码行,然后将其放入worker_DoWork()中,因为我认为这些行需要时间来处理:
try
{
//coppy all rows from nominated datatable and dump it to DB
bulkCopy.WriteToServer(dataTablePro);
dataTablePro.Clear();
using (SqlBulkCopy bulkCopyCat = new SqlBulkCopy(connection))
{
bulkCopyCat.ColumnMappings.Add(dataTableCat.Columns[0].ColumnName, "Loại");
bulkCopyCat.DestinationTableName = "dbo.Categories";
bulkCopyCat.WriteToServer(dataTableCat);
dataTableCat.Clear();
MessageBox.Show("Success!!!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
我该怎么做才能得到它?我听说过代表和事件可以给我一个选择,但我自己找不到它。谢谢!
答案 0 :(得分:0)
如果您不关心进度对话框的多个实例。然后尝试这样的事情:
<强> Import.xaml:强>
<Grid>
<Button x:Name="filePicker" Content="ClickMe" Click="filePicker_Click" DockPanel.Dock="Top"/>
<ProgressBar x:Name="progressBar" Minimum="0" Maximum="100" Value="75" DockPanel.Dock="Top" Visibility="Collapsed" />
</Grid>
<强> Import.xaml.cs 强>:
private void filePicker_Click(object sender, RoutedEventArgs e)
{
progressBar.Visibility = Visibility.Visible;
// here you call your batch code
var task = new Task(() => Thread.Sleep(3000));
// after task is done hide progress dialog
task.ContinueWith(x => Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => this.progressBar.Visibility = Visibility.Collapsed)));
task.Start();
}
(我没有更新状态。它总是75%。)。