WCF+Silverlight一个简单的RSS阅读器(二)

  1. 嘿嘿,坚持不懈,继续我的Silverlight之旅,创建了WCF服务,我们需要的是通过Silverlight能够应用这个服务,

         比起2005,2008的服务引用做的好多了,上图:

        image

   通过右边的Discover按钮,可以搜索系统中的WebService服务,我们的RSSService当然也在此列之中,选中起个名字RSSService吧。

2.有个可以使用的服务,下面就是要提供界面了,遵循传统的方式,选用LisbBox来列出前20项最新的标题,Expression Blend2+sp1可是要必备,

   这东西设计起来比VS2008要好多了,不过BUG也很多,量大的时候容易死机。上图:

image

为了能放置在公告栏上,特意设计成200*300的大小,此时布局带来了大问题,昨天晚上搞了一晚上都没有办法是控件位置移动到最左端,

早上尝试了一下,一定要Rebuid才行,这个Build是无法将修改的布局应用上去的。不过放到博客上去,他还是往中间移。头大了。

3.最关键的东西来了,就是怎么显示内容了,还是传统写法获得数据填充一下:

  a)WebService的调用,

RssService.RssServiceClient rssClient = new SilverlightApplication2.RssService.RssServiceClient();
  rssClient.GetRssItemsCompleted +=new EventHandler<SilverlightApplication2.RssService.GetRssItemsCompletedEventArgs>(rssClient_GetRssItemsCompleted);
          rssClient.GetRssItemsAsync("http://www.codeproject.com/webservices/articlerss.aspx?cat=1");
    

注意:千万别忘了最后一句rssClient.GetRssItemAsync().

  b)对事件的响应:

void rssClient_GetRssItemsCompleted(object sender, SilverlightApplication2.RssService.GetRssItemsCompletedEventArgs e)
        {

try
           {
               ObservableCollection<RssItem> rssItems = e.Result;
               for (int i = 0; i < rssItems.Count; i++)
               {
                   ListBoxItem item = new ListBoxItem();
                   item.Style = (Style)Application.Current.Resources["AppRssItemStyle"];
                    item.Content = rssItems[i].Title;
                   item.Tag = rssItems[i];
                   item.MouseLeftButtonUp += new MouseButtonEventHandler(item_MouseLeftButtonUp);
                    this.lstItems.Items.Add(item);
                 }
           }
           catch (Exception ex)
           {
               ListBoxItem item = new ListBoxItem();
               item.Style = (Style)Application.Current.Resources["AppRssItemStyle"];
               item.Content = ex.Message;
               this.lstItems.Items.Add(item);
           }

}

这里和传统变程的区别是,首先要自定义ListBoxItem类,建立他的样式,建立他的事件,然后添加到listbox中去。

c)Item对事件的响应

void item_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
       {
           ListBoxItem item = (ListBoxItem)sender;
           RssItem rssItem =(RssItem) item.Tag;
           if (rssItem != null)
           {
               item.Content = rssItem.Link;
               if (HtmlPage.IsPopupWindowAllowed)
               {
                   HtmlPage.PopupWindow(new Uri(rssItem.Link), null, null);
               }
               else
               {
                   HtmlPage.Window.Navigate(new Uri(rssItem.Link));
               }

           }
           item.Background.SetValue(Brush.OpacityProperty, 50.0);
       }

  item.Background.SetValue(Brush.OpacityProperty, 50.0);将背景的透明度设置为50,和传统的方法不同在Silverlight中设置

值一般都需要SetValue的方法,同时需要知道DependancyProperty,这个需要对各个属性有一定的了解。

 

  1. 明天我会继续写如何设置样式,使用State,和动画。
posted @ 2008-12-18 06:29  wanghualiang  阅读(1779)  评论(1编辑  收藏  举报