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的客户端缓存就可以。

 

 

源码:

001public partial class MainPage : UserControl
002    {
003        public class Project
004        {
005            public string Title
006            {
007                get;
008                set;
009            }
010  
011            public DateTime DueDate
012            {
013                get;
014                set;
015            }
016  
017            public string Description
018            {
019                get;
020                set;
021            }
022        }
023        private ListItemCollection _projects;
024  
025        public MainPage()
026        {
027            InitializeComponent();
028            ClientContext context = new ClientContext(ApplicationContext.Current.Url); 
029            //System.Threading.SynchronizationContext thread = System.Threading.SynchronizationContext.Current; if (thread == null)
030                //thread = new System.Threading.SynchronizationContext();
031            //ClientContext context = new ClientContext("http://localhost");
032            //context.AuthenticationMode = Microsoft.SharePoint.Client.ClientAuthenticationMode.Default;
033            context.Load(context.Web);
034              
035            List Projects = context.Web.Lists.GetByTitle("Tasks");
036            context.Load(Projects);
037  
038            CamlQuery query = new Microsoft.SharePoint.Client.CamlQuery();
039            string camlQueryXml = "<View><Query><Where><Gt>" +
040                "<FieldRef Name='DueDate' />" +
041                "<Value Type='DateTime'>2008-01-1T00:00:00Z</Value>" +
042                "</Gt></Where></Query><ViewFields>" +
043                "<FieldRef Name=/"Title/" /><FieldRef Name=/"Body/" />" +
044                "<FieldRef Name=/"DueDate/" />" +
045                "</ViewFields></View>";
046  
047            query.ViewXml = camlQueryXml;
048            _projects = Projects.GetItems(query);
049            context.Load(_projects);
050            //MessageBox.Show("Test!");
051            //context.ExecuteQuery();
052            //System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ThreadCallback), context);
053            context.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), new ClientRequestFailedEventHandler(OnRequestFailed));
054              
055        }
056  
057        /*private void ThreadCallback(object s)
058        {
059           // var context = (ClientContext)s;
060           // context.ExecuteQuery();
061           this.Dispatcher.BeginInvoke(BindData);
062        }*/
063  
064        private void OnRequestFailed(Object sender, ClientRequestFailedEventArgs args)
065        {
066            // this is not called on the UI thread 
067            //MessageBox.Show("inside OnRequestFailed Method");
068            this.Dispatcher.BeginInvoke(delegate()
069            {
070                MessageBox.Show("inside OnRequestSucceeded Method" + args.StackTrace.ToString() + args.ErrorDetails);
071            });
072        }
073  
074         
075  
076        private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args) 
077        
078            // this is not called on the UI thread 
079            //MessageBox.Show("inside OnRequestSucceeded Method");
080            this.Dispatcher.BeginInvoke(BindData); 
081        }
082  
083        private void Failed(ClientRequestFailedEventArgs args)
084        {
085            MessageBox.Show("inside OnRequestFailed Method" + args);
086        }
087          
088        private void BindData() 
089        
090            List<Project> projects = new List<Project>();
091           foreach (ListItem li in _projects) 
092            
093                projects.Add(new Project() 
094                
095                    Title = li["Title"].ToString(), 
096                    DueDate = Convert.ToDateTime(li["DueDate"].ToString()), 
097                    Description = li["Body"].ToString() 
098                }); 
099            }
100            dataGrid1.ItemsSource = projects; 
101        }
102    }

 

http://www.cnblogs.com/johnsonwong/archive/2011/05/08/2040266.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值