我正在为 SharePoint 开发 Silverlight 应用程序,并希望从列表中获取 ListItems。我知道我需要异步查询以避免阻塞 UI 线程。通常我使用 ExecuteQueryAsync 但这不起作用,因为我喜欢将结果设置为 DataGrid 源。
如何异步调用 GetItemsFromList 方法并将结果设置为 DataGrid 源而不产生太多代码开销?(拉姆达?)
SharePointHelper 类:
public static ListItemCollection GetItemsFromList(string name)
{
var context = ClientContext.Current;
var targetList = context.Web.Lists.GetByTitle("ListName");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = string.Format("<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>{0}</Value></Eq></Where></Query>RowLimit>4</RowLimit></View>",
name);
ListItemCollection collListItems = targetList.GetItems(camlQuery);
context.ExecuteQuery();
return collListItems;
}
用户界面类:
private void SetDataGridItemSource()
{
dataGrid.Source = GetItemsFromList("name");
}
我现在已经实现了 Shawn Kendrot 的解决方案:
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (sender, args) =>
{
args.Result = SharePointHelpers.GetItemsFromList("name");
};
worker.RunWorkerCompleted += (s, e) => dataSource.Source = e.Result as ListItemCollection;
worker.RunWorkerAsync();