Tôi hiện đang viết một ứng dụng WPF 3.5 đơn giản sử dụng SharePoint COM để thực hiện cuộc gọi đến các trang SharePoint và tạo thông tin Nhóm và Người dùng. Vì quá trình này mất một lúc, tôi muốn hiển thị ProgressBar trong khi các nhóm đang được tạo. Quy trình mong muốn như sau:Cập nhật giao diện người dùng với BackgroundWorker trong WPF
- Người dùng nhập url và nút nhấp để tìm nạp dữ liệu trang web.
- ProgressBar bắt đầu hoạt hình
- Các nhóm được tạo ra và tên được thêm vào một ListView
- Khi hoạt hình hoàn ProgressBar kết thúc
Vấn đề tôi đang chạy vào đó là giao diện người dùng không bao giờ được cập nhật. Cả ProgressBar hoặc ListView đều không thực hiện bất kỳ thay đổi nào. Nếu bất cứ ai có bất kỳ ý tưởng để giúp với các mã dưới đây nó sẽ được đánh giá rất nhiều.
private void GetGroupsAndUsersButton_Click(object sender, RoutedEventArgs e)
{
siteUrl = "";
if (SiteURLTextBox.Text.Length > 0)
{
FetchDataProgressBar.IsIndeterminate = true;
mWorker = new BackgroundWorker();
mWorker.DoWork += new DoWorkEventHandler(worker_DoWork);
mWorker.WorkerSupportsCancellation = true;
mWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
mWorker.RunWorkerAsync();
}
else
{
System.Windows.MessageBox.Show("Please enter a URL for the SharePoint site you wish to retrieve data");
}
}
private void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
siteUrl = SiteURLTextBox.Text;
GroupListView.ItemsSource = null;
try
{
using (SPSite site = new SPSite(siteUrl))
{
SPWeb web = site.OpenWeb();
SPGroupCollection collGroups = web.SiteGroups;
if (GroupNames == null)
GroupNames = new List<string>();
foreach (SPGroup oGroup in collGroups)
{
GroupListView.Items.Add(new ListViewItem() { Content = oGroup.Name });
}
foreach (ListViewItem item in GroupListView.Items)
{
item.MouseLeftButtonUp += item_MouseLeftButtonUp;
}
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show("Unable to locate a SharePoint site at: " + siteUrl);
}
}
private void worker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
FetchDataProgressBar.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
new Action(
delegate()
{
FetchDataProgressBar.IsIndeterminate = false;
}
));
}
Cảm ơn bạn đã trả lời. Đáng buồn thay, không có bản cập nhật GUI nào. Ngoài ra, trong khi gỡ lỗi, sự kiện thay đổi tiến trình không bao giờ bị tấn công. Nó ngay lập tức phá vỡ tại worker_RunWorkerCompleted mặc dù (???) – pstricker
bạn đã bao giờ đạt được vòng lặp vào try/catch? – Kooki
Sau một chút tinkering tôi đã có thể đạt được thử/bắt bằng cách di chuyển mã tham chiếu đến các yếu tố giao diện người dùng (GroupListView và SiteUrlTextBox) đến sự kiện nhấn nút tuy nhiên bây giờ nó là một ngoại lệ khi khởi tạo ListViewItem: "Chuỗi gọi phải là STA, vì nhiều thành phần giao diện người dùng yêu cầu điều này ". – pstricker