2011-03-04 42 views
1

我正在构建Silverlight Web部件。我只是想在文本块中显示sharepoint列表数据与数据网格,因为我只打算从列表中返回一个项目。我设法得到我想要的结果在一个数据网格,但我不知道如何修改我的代码,所以我可以显示我的数据在文本块。在textblock verses datagrid中显示列表数据,sharepoint webpart

我以为我可以简单的写

texblock1.text = projects; 

但它抛出一个错误。

这里的代码隐藏在我的XAML主页-------------------

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using Microsoft.SharePoint.Client; 

namespace something{   

    public class Project{   
     public string Title {get; set;}   
    }  

    public partial class MainPage : UserControl  
    {   
     public string SiteUrl { get; set; }   

     private ListItemCollection _projects;   

     //private Web _web = null;   
     //private string _lastErrorMessage = null;   

     public MainPage()   
     {    

      InitializeComponent(); 
      ClientContext context = new ClientContext(ApplicationContext.Current.Url); 
      context.Load(context.Web); 
      List Projects = context.Web.Lists.GetByTitle("projects"); 
      context.Load(Projects); 
      CamlQuery query = new Microsoft.SharePoint.Client.CamlQuery(); 

      string camlQueryXml = "<View><Query><Where><Eq><FieldRef Name=\"NameLast\" /><Value Type=\"Boolean\">1</Value></Eq></Where></Query></View>"; 

      query.ViewXml = camlQueryXml; 
      _projects = Projects.GetItems(query); 
      context.Load(_projects);context.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), null);   

     }   

     private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args)   
     {    

      // This is not called on the UI thread.    
      Dispatcher.BeginInvoke(BindData);   
     }   

     private void BindData()   
     {    

      List<Project> projects = new List<Project>();    

      foreach (ListItem li in _projects)    

      {     

       projects.Add(new Project()     

       {      
        Title = li["Title"].ToString(), 
       });    

      }    

      dataGrid1.ItemsSource = projects; // must be on UI thread   
     }  
    } 
} 

回答

1

要在UI线程使用的代码运行代码如下:

Dispatcher.BeginInvoke(() => { 
    //add code here to which are to be executed on the UI thread 
}); 
+0

多一点指导,将不胜感激!感谢您的输入。 – 2011-03-06 22:08:08

相关问题