概述
Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic,Visual C#,IronRuby,Ironpython,对JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步学Silverlight 2系列》文章将从Silverlight 2基础知识、数据与通信、自定义控件、动画、图形图像等几个方面带您快速进入Silverlight 2开发
界面布局
我们最终完成的RSS阅读器界面如下:
<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>
设计顶部输入区域,对Grid第一行做合并,并且放置一个StackPanel:
<StackPanel x:Name="Header" Orientation="Horizontal" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"> <Image Source="RSS.png" Width="32" Height="32" Margin="10 0 10 0"></Image> <Border Style="{StaticResource titleBorder}"> <TextBlock Text="基于Silverlight的RSS阅读器" Foreground="#FFFFFF" VerticalAlignment="Center" Margin="12 0 0 0"></TextBlock> </Border> <WatermarkedTextBox x:Name="FeedAddress" Width="300" Height="35" FontSize="16" Margin="10 0 10 0"> <WatermarkedTextBox.Watermark> <TextBlock Text="请输入有效的RSS地址" VerticalAlignment="Center" Foreground="#FBA430" FontSize="16"></TextBlock> </WatermarkedTextBox.Watermark> </WatermarkedTextBox> <Button x:Name="displayButton" Style="{StaticResource button}" Content="显 示" Click="displayButton_Click"></Button> <Button x:Name="fullScreenButton" Style="{StaticResource button}" Content="全 屏" Click="fullScreenButton_Click"></Button> </StackPanel>
鉴于两个按钮的风格一致,在App.xaml中定义一个button样式:
<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> <LinearGradientBrush StartPoint="0,0"> <GradientStop Color="#FBA430" Offset="0.0" /> <GradientStop Color="#FEF4E7" Offset="0.5" /> <GradientStop Color="#FBA430" Offset="1.0" /> </LinearGradientBrush> </Setter.Value> </Setter> </Style>定义分割线,用Rectangle来表示:
<StackPanel Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" VerticalAlignment="Center"> <Rectangle Style="{StaticResource rectangle}"/> </StackPanel>
为了显示出渐变的样式,我们定义样式如下:
<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> <LinearGradientBrush StartPoint="0,0"> <GradientStop Color="#FEF4E7" Offset="0.0" /> <GradientStop Color="#FBA430" Offset="1.0" /> </LinearGradientBrush> </Setter.Value> </Setter> </Style>定义左边的列表区,用ListBox来显示,并且定义ItemTemplate:
<ListBox x:Name="PostsList" Grid.Column="0" Grid.Row="2" Margin="10 5 5 10" SelectionChanged="PostsList_SelectionChanged"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Title.Text}" textwrapping="Wrap" Width="200"/> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
最后定义右边的详细信息区域,在StackPanel中垂直放置三个Border:
<StackPanel x:Name="Detail" Grid.Column="1" Grid.Row="2"> <Border CornerRadius="10" Background="#CDFCAE" Margin="10 5 10 10" Width="540" Height="40"> <TextBlock Text="{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="{Binding Summary.Text}" textwrapping="Wrap"/> </Border> <Border CornerRadius="10" Background="#CDFCAE" Margin="10 5 10 10" Width="540" Height="40"> <StackPanel Orientation="Horizontal"> <TextBlock Text="评论日期:" textwrapping="Wrap" Foreground="Red" VerticalAlignment="Center"/> <TextBlock Text="{Binding PublishDate}" textwrapping="Wrap" Foreground="Red" VerticalAlignment="Center"/> </StackPanel> </Border> </StackPanel>
界面布局到此大功告成。
实现功能
下面实现数据的获取,采用WebRequest来实现,也可以使用其他方式。
/// <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 PostsList_SelectionChanged(object sender,SelectionChangedEventArgs e) { SyndicationItem item = PostsList.SelectedItem as SyndicationItem; Detail.DataContext = item; }
实现全屏按钮的代码:
/// <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; }
运行效果
运行后界面如下:
输入豆瓣的最新影评Feed:
选择其中一项后,将显示出详细信息:
结束语
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。