Silverlight中许多操作都要去异步完成,但有时候会碰到多个异步操作需要按照一定顺序完成,事件是把异步操作转换成同步操作的一种方法。

lambda是操作事件的简洁方式,事件、lambda、同步组合在一起,就会产生事件处理程序不能移除的状况。看下面的代码:


public void LoadChapter(string uri)
      {
          
//两个异步操作需要协调

          EventHandler handler 
= (s, e) =>
          {
              
if (this.PagedContentListView == null)
              {
                  BookContentService contentService 
= new BookContentService();
                  EventHandler loadContentHandler 
= (ss, args) =>
                  {
                     
//必须在外层执行完后执行

                  };
                  contentService.Loaded 
-= loadContentHandler;
                  contentService.Loaded 
+= loadContentHandler;
                  contentService.Load(UriGenratorFaced.GeneratBookContentUri(uri));
              }

              Chapter 
= service.BookChapter;
                        };
          service.Loaded 
-= handler;
          service.Loaded 
+= handler;       } 

相关文章:

  • 2021-10-06
  • 2022-12-23
  • 2021-09-08
  • 2021-11-23
  • 2021-08-20
  • 2022-01-03
猜你喜欢
  • 2021-11-23
  • 2021-11-21
  • 2021-10-19
  • 2021-10-02
  • 2021-10-28
  • 2021-06-13
相关资源
相似解决方案