vs2010 学习Silverlight学习笔记(16):RssRead

概要:

       Sl已经学习有一段时间了,但总是怀疑已学的东西到底能不能做出一些东西。今天照着例子动手写一下,验证一下。

内容:

       首先还是xaml的布局。都是一些控件的使用,但是让我来做肯定丑的自己都看不下去。以前最头疼的就是做页面,是没这方面细胞,还是根本就没好好练过?

MainPage.xaml:

<UserControlx:Class="SLDemo18RssRead.MainPage"

   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

   xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

   mc:Ignorable="d"

   d:DesignHeight="500" d:DesignWidth="800">

 

   <Grid x:Name="LayoutRoot"Background="#fef4e7">

       <Grid.RowDefinitions>

           <RowDefinition Height="50"></RowDefinition>

           <RowDefinition Height="20"></RowDefinition>

           <RowDefinition Height="*"></RowDefinition>

       </Grid.RowDefinitions>

       <Grid.ColumnDefinitions>

           <ColumnDefinition Width="240"></ColumnDefinition>

           <ColumnDefinition Width="*"></ColumnDefinition>

       </Grid.ColumnDefinitions>

       <StackPanel x:Name="Header"Orientation="Horizontal"

   Grid.Row="0" Grid.Column="0"Grid.ColumnSpan="2">

           <Image  Width="32"Height="32" Margin="10 0 10 0"Source="/SLDemo18RssRead;component/Rss.png"></Image>

           <Border Style="{StaticResource titleBorder}">

                <TextBlock Text="基于SL的RSS阅读器"Foreground="#ffffff"

                          VerticalAlignment="Center" Margin="12 0 00"></TextBlock>

           </Border>

 

           <TextBox x:Name="feedAddress" Width="300" Height="35"

                     FontSize="16"Margin="10 0 10 0"></TextBox>

 

           <Button x:Name="displayButton" Style="{StaticResourcebutton}"

                    Content="显 示" Click="displayButton_Click"></Button>

           <Button x:Name="fullScreenButton"Style="{StaticResource button}"

                    Content="全 屏" Click="fullScreenButton_Click"></Button>

       </StackPanel>

       <StackPanel Grid.Row="1" Grid.Column="0"Grid.ColumnSpan="2" VerticalAlignment="Center">

           <RectangleStyle="{StaticResource rectangle}"/>

       </StackPanel>

       <ListBox x:Name="PostsList" Grid.Column="0"Grid.Row="2"

                 Margin="10 5 5 10"SelectionChanged="PostsList_SelectionChanged">

           <ListBox.ItemTemplate>

                <DataTemplate>

                    <TextBlockText="{Binding Title.Text}"

                                  TextWrapping="Wrap" Width="200"/>

                </DataTemplate>

           </ListBox.ItemTemplate>

       </ListBox>

       <StackPanelx:Name="Detail" Grid.Column="1" Grid.Row="2">

           <Border CornerRadius="10" Background="#CDFCAE"Margin="10 5 10 10"

                    Width="540"Height="40">

                <TextBlockText="{Binding Title.Text}" TextWrapping="Wrap"

                          VerticalAlignment="Center" Foreground="Red"/>

           </Border>

           <Border CornerRadius="10" Background="#CDFCAE"Margin="10 5 10 10"

                    Width="540"Height="300">

                <TextBlock Text="{BindingSummary.Text}" TextWrapping="Wrap"/>

           </Border>

           <Border CornerRadius="10" Background="#CDFCAE"Margin="10 5 10 10"

                    Width="540"Height="40">

                <StackPanelOrientation="Horizontal">

                   <TextBlockText="评论日期:"  TextWrapping="Wrap"

                              Foreground="Red" VerticalAlignment="Center"/>

                    <TextBlockText="{Binding PublishDate}" TextWrapping="Wrap"

                              Foreground="Red" VerticalAlignment="Center"/>

                </StackPanel>

           </Border>

       </StackPanel>

   </Grid>

</UserControl>

 

样式部分代码,AppPage.xaml:

<Applicationxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

            x:Class="SLDemo18RssRead.App"

            >

   <Application.Resources>

       <Style x:Key="button" TargetType="Button">

           <Setter Property="Width"Value="100"></Setter>

           <Setter Property="Height" Value="35"></Setter>

           <Setter Property="Background"Value="#FBA430"></Setter>

           <Setter Property="Foreground"Value="#FBA430"></Setter>

           <Setter Property="FontSize"Value="16"></Setter>

       </Style>

       <Style x:Key="titleBorder"TargetType="Border">

           <Setter Property="CornerRadius"Value="10"></Setter>

           <Setter Property="Width"Value="220"></Setter>

           <Setter Property="Height"Value="40"></Setter>

           <Setter Property="Background">

                <Setter.Value>

                    <LinearGradientBrushStartPoint="0,0">

                        <GradientStopColor="#FBA430" Offset="0.0" />

                        <GradientStopColor="#FEF4E7" Offset="0.5" />

                        <GradientStop Color="#FBA430"Offset="1.0" />

                   </LinearGradientBrush>

                </Setter.Value>

           </Setter>

       </Style>

       <Style x:Key="rectangle"TargetType="Rectangle">

           <Setter Property="Width"Value="780"></Setter>

           <Setter Property="Height"Value="5"></Setter>

           <Setter Property="RadiusX"Value="3"></Setter>

           <Setter Property="RadiusY"Value="3"></Setter>

           <Setter Property="Fill">

                <Setter.Value>

                    <LinearGradientBrushStartPoint="0,0">

                        <GradientStopColor="#FEF4E7" Offset="0.0" />

                        <GradientStopColor="#FBA430" Offset="1.0" />

                   </LinearGradientBrush>

                </Setter.Value>

           </Setter>

       </Style>

   </Application.Resources>

</Application>

 

 

代码:

/// <summary>

       /// 显示列表

       /// </summary>

       /// <param name="sender"></param>

       /// <param name="e"></param>

       private void displayButton_Click(object sender, RoutedEventArgs e)

       {

           Uri uri = new Uri(feedAddress.Text);

           WebRequest request = (WebRequest)WebRequest.Create(uri);

           request.BeginGetResponse(new AsyncCallback(responseReady), request);

       }

 

       void responseReady(IAsyncResult asyncResult)

       {

           WebRequest request = (WebRequest)asyncResult.AsyncState;

           WebResponse response = (WebResponse)request.EndGetResponse(asyncResult);

 

           XmlReader reader = XmlReader.Create(response.GetResponseStream());

           SyndicationFeed feed = SyndicationFeed.Load(reader);

 

           PostsList.ItemsSource = feed.Items;

       }

 

       /// <summary>

       /// 全屏显示

       /// </summary>

       /// <param name="sender"></param>

       /// <param name="e"></param>

       private void fullScreenButton_Click(object sender, RoutedEventArgs e)

       {

           Content contentObject = Application.Current.Host.Content;

           contentObject.IsFullScreen = !contentObject.IsFullScreen;

       }

 

       /// <summary>

       /// 查看详细信息

        /// </summary>

       /// <param name="sender"></param>

       /// <param name="e"></param>

       private void PostsList_SelectionChanged(object sender,SelectionChangedEventArgs e)

       {

           SyndicationItem item = PostsList.SelectedItem as SyndicationItem;

 

           Detail.DataContext = item;

       }

 

 

注:

代码所需的引用:

       System.Xml.LinQ;

System.ServiceModel;

       System.ServiceModel.Syndication;

引入的命名空间总共有:

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 System.Xml;

using System.Xml.Linq;

using System.Windows.Interop;

using System.ServiceModel.Syndication;

 

 

 

总结:

       做这个例子可能遇到的困难首先是引入命名空间问题,在什么情况下需要引入什么命名空间,这个我只是照着例子试试看。我想这个也没什么取巧的方法,只能靠积累,总结。

还有一个可能只是对我来说是个困难,就是如何才能有一个漂亮的布局?以前做web我主要做逻辑,实体等部分,页面部分有别的人来做的。但这个SL主要还是用来显示的,所以我总觉得SL在我手上是被糟蹋的感觉。。。

posted @ 2010-09-09 20:48  耀哥  阅读(629)  评论(0编辑  收藏  举报