SliverLight Web part
做了一个简单的SilverLight WebPart,但第一次做起来并不轻松,除了各种各样的Issue,譬如OnRequestSucceeded没有被触发是因为没有注册OnRequestFailed而执行Fail的原因,但是在OnRequestFailed里面的代码(简单的弹出消息)失败的原因应该与线程有关,总结了各种经验:
第一,当我们使用context.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), new ClientRequestFailedEventHandler(OnRequestFailed));来注册事件的时候,不要在OnRequestSucceeded或者OnRequestFailed事件里直接写代码,这个会报线程冲突的错误。应该使用代理。
Dispatcher.BeginInvoke(delegate()
{
MessageBox.Show("inside OnRequestSucceeded Method" + args.StackTrace.ToString() + args.ErrorDetails);
});
第二个,context的参数指定一定要使用ApplicationContext.Current.Url,否则在运行的时候会报Security的错误(太搞了,我直接使用ClientContext(“http://localhost”)一直报这个错误,SharePoint的设计也太次了,难道是因为不确认是否在本站运行?有些网站里面提到要部署ClientAccessPolicy.xml在IIS根站点目录下,至少对SilverLight WebPart是没有意义的。)
ClientContext context = new ClientContext(ApplicationContext.Current.Url);
第三个,总结学到的SilverLight里两种异步执行的方式
1)clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed),注册成功执行后的时间以及失败后的事件
2)通过多线程调用System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ThreadCallback), context);
一个小技巧点:
Silverlight重新部署后不需要重启IIS或者重置Application Pool,删除IE的客户端缓存就可以。
源码:
public partial class MainPage : UserControl { public class Project { public string Title { get; set; } public DateTime DueDate { get; set; } public string Description { get; set; } } private ListItemCollection _projects; public MainPage() { InitializeComponent(); ClientContext context = new ClientContext(ApplicationContext.Current.Url); //System.Threading.SynchronizationContext thread = System.Threading.SynchronizationContext.Current; if (thread == null) //thread = new System.Threading.SynchronizationContext(); //ClientContext context = new ClientContext("http://localhost"); //context.AuthenticationMode = Microsoft.SharePoint.Client.ClientAuthenticationMode.Default; context.Load(context.Web); List Projects = context.Web.Lists.GetByTitle("Tasks"); context.Load(Projects); CamlQuery query = new Microsoft.SharePoint.Client.CamlQuery(); string camlQueryXml = "<View><Query><Where><Gt>" + "<FieldRef Name='DueDate' />" + "<Value Type='DateTime'>2008-01-1T00:00:00Z</Value>" + "</Gt></Where></Query><ViewFields>" + "<FieldRef Name=\"Title\" /><FieldRef Name=\"Body\" />" + "<FieldRef Name=\"DueDate\" />" + "</ViewFields></View>"; query.ViewXml = camlQueryXml; _projects = Projects.GetItems(query); context.Load(_projects); //MessageBox.Show("Test!"); //context.ExecuteQuery(); //System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ThreadCallback), context); context.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), new ClientRequestFailedEventHandler(OnRequestFailed)); } /*private void ThreadCallback(object s) { // var context = (ClientContext)s; // context.ExecuteQuery(); this.Dispatcher.BeginInvoke(BindData); }*/ private void OnRequestFailed(Object sender, ClientRequestFailedEventArgs args) { // this is not called on the UI thread //MessageBox.Show("inside OnRequestFailed Method"); this.Dispatcher.BeginInvoke(delegate() { MessageBox.Show("inside OnRequestSucceeded Method" + args.StackTrace.ToString() + args.ErrorDetails); }); } private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args) { // this is not called on the UI thread //MessageBox.Show("inside OnRequestSucceeded Method"); this.Dispatcher.BeginInvoke(BindData); } private void Failed(ClientRequestFailedEventArgs args) { MessageBox.Show("inside OnRequestFailed Method" + args); } private void BindData() { List<Project> projects = new List<Project>(); foreach (ListItem li in _projects) { projects.Add(new Project() { Title = li["Title"].ToString(), DueDate = Convert.ToDateTime(li["DueDate"].ToString()), Description = li["Body"].ToString() }); } dataGrid1.ItemsSource = projects; } }