0

我正在为 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(); 
4

2 回答 2

1

我不熟悉 SharePoint 的 ClientContext 类(甚至是 SharePoint),但我在docs中看不到任何异步方法。如果此调用成本很高,您可以将调用包装在BackgroundWorker中。BackgroundWorker 将执行查询,您可以返回结果。您将无法按照您的描述分配 Source,而是需要在工作人员完成时设置 Source。

    private void SetDataGridItemSource()
    {
        BackgroundWorker worker = new BackgroundWorker();
        worker.DoWork += WorkerOnDoWork;
        worker.RunWorkerCompleted += WorkerOnRunWorkerCompleted;
        worker.RunWorkerAsync("name");       
    }

    private void WorkerOnDoWork(object sender, DoWorkEventArgs args)
    {
        if(args.Argument != null)
        {
            string name = args.Argument.ToString();
            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();

            args.Result = collListItems;
        }
    }

    private void WorkerOnRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs args)
    {
        dataGrid.Source = args.Result as ListItemCollection;
    }
于 2012-08-13T16:33:36.350 回答
0

像这样的东西?

public delegate void ItemsLoadedCallback(IEnumerable<object> Entities);
public static void GetItemsFromList(string name, ItemsLoadedCallback Callback)
{
    // codesnip

    // Do async thing, on return call:
    if (Callback != null)
    {
        Callback(collListItems);
    }
}


private void SetDataGridItemSource()
{
    GetItemsFromList("name", (items) => dataGrid.Source = items);
}
于 2012-08-13T16:33:06.343 回答