2012-02-22 32 views
0

我的代碼存在一些問題。我有Silverlight的按鈕後,我按下按鈕,我想加載文檔庫,並得到有多少項目是在這個圖書館共同我的代碼信息是:SilverLight與SharePoint中的異步代碼

var web = context.Web; 
List sharedDocumentsList = context.Web.Lists.GetByTitle("dokumenty"); 

int i = sharedDocumentsList.ItemCount; 

context.Load(sharedDocumentsList); 
context.ExecuteQueryAsync(OnFileWriteSucceeded, OnFileWriteFailed); 

但我還是同樣的問題。

集合尚未初始化。它沒有被請求或者請求沒有被執行。它可能需要爆炸性地請求

我如何獲得ItemCount。這只是一個簡單的例子,我需要在循環中使用這個列表等等。但我需要解決這個主要問題。如何使用按鈕點擊方法直接使用此文檔列表。

謝謝。

+0

當我試着使用 context.ExecuteQuery(),使這個; 我得到這個錯誤: 「被調用的方法或屬性可能會阻塞UI線程並且它是不允許的,請使用後臺線程來調用方法或屬性,例如,使用System.Threading.ThreadPool.QueueUserWorkItem方法來調用方法或屬性在我的按鈕單擊方法 所以我不能夠與 context.ExecuteQuery(),使這個; 而且我無法得到結果當我做這個異步 謝謝 – user1223484 2012-02-22 13:28:10

回答

0

這個工作對我來說:

private List docList; 
private void GetList() 
{ 
    var context = new ClientContext(ApplicationContext.Current.Url); 
    context.Load(context.Web); 
    docList = context.Web.Lists.GetByTitle("dokumenty"); 
    context.Load(docList); 
    context.ExecuteQueryAsync(GetListSucceded, GetListFailed); 
} 

private void GetListFailed(object sender, ClientRequestFailedEventArgs e) 
{    
    Dispatcher.BeginInvoke(() => MyErrorFunction(); 
} 

private void GetListSucceded(object sender, ClientRequestSucceededEventArgs e) 
{ 
    Dispatcher.BeginInvoke(() => GetItemsCount()); 
} 

private void GetItemsCount() 
{ 
    MessageBox.Show(docList.ItemCount.ToString()); 
}