新时尚Windows8开发(20):FlipView控件

转自:http://blog.csdn.net/tcjiaan/article/details/8063301

FlipView控件和我们常见到的ListView、ListBox控件很像,今天拿它来吹一吹,是因为这个控件还挺新鲜的。

要说用文字来介绍这个控件,还真不够生动也欠缺活泼,不过,其实这个控件咱们还是见得不少的,如果你经常到应用商店下载应用的话,你肯定见过。就是这个:

就是这玩意儿,左右各有一个按钮用来导航视图,每次只能查看一个视图,比较适合用于图片浏览。

废话少说,我们来做两个实例,第一个是手动添加项,第二个是数据绑定的。

好的,先做第一个,很简单,我们在FlipView中放三个项,每个项的内容就是一个Image,看看下面的XAML就会明白了。

[html] view plaincopyprint?

  1. <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">  
  2.     <FlipView Margin="300,150">  
  3.         <FlipViewItem>  
  4.             <Image  
  5.                 Stretch="Uniform"  
  6.                 Source="http://pica.nipic.com/2008-01-17/2008117205617816_2.jpg"/>  
  7.         </FlipViewItem>  
  8.         <FlipViewItem>  
  9.             <Image  
  10.                 Stretch="Uniform"  
  11.                 Source="http://pic4.nipic.com/20090914/1593169_000535237178_2.jpg"/>  
  12.         </FlipViewItem>  
  13.         <FlipViewItem>  
  14.             <Image  
  15.                 Stretch="Uniform"  
  16.                 Source="http://www.zwtuu.com.cn/upload/2008_10/08102721166304.jpg"/>  
  17.         </FlipViewItem>  
  18.     </FlipView>  
  19. </Grid>  


然后,我们运行一下。

好的,现在来看看第二个例子,数据绑定的。

既然要数据绑定了,肯定要数据源的了。因此,我们先伪造一些数据,注意,这数据是伪造的,如有雷同,纯属自然造化。

[csharp] view plaincopyprint?

  1. public class Student  
  2. {  
  3.     public string Name { getset; }  
  4.     public int Age { getset; }  
  5.     public string Sex { getset; }  
  6.     public string Email { getset; }  
  7.     public string Remark { getset; }  
  8. }  
  9.   
  10. public class TestDataSource : System.Collections.ObjectModel.ObservableCollection<Student>  
  11. {  
  12.     public TestDataSource()  
  13.     {  
  14.   
  15.         this.Add(new Student  
  16.         {  
  17.             Name = "小赵",  
  18.             Age = 20,  
  19.             Sex = "男",  
  20.             Email = "[email protected]",  
  21.             Remark = "此人人品低下,经常在公共场所撒尿。"  
  22.         });  
  23.   
  24.         this.Add(new Student  
  25.         {  
  26.             Name = "小王",  
  27.             Age = 18,  
  28.             Email = "[email protected]",  
  29.             Sex = "女",  
  30.             Remark = "胆大心细脸皮厚。"  
  31.         });  
  32.   
  33.         this.Add(new Student  
  34.         {  
  35.             Name = "小刘",  
  36.             Age = 21,  
  37.             Email = "[email protected]",  
  38.             Sex = "男",  
  39.             Remark = "这厮是牛人啊。"  
  40.         });  
  41.     }  
  42. }  


然后,在XAML中进行绑定UI。

[html] view plaincopyprint?

  1. <Page  
  2.     x:Class="FlipViewExample2.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:local="using:FlipViewExample2"  
  6.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  7.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  8.     mc:Ignorable="d">  
  9.       
  10.     <Page.Resources>  
  11.         <Style x:Key="t1" TargetType="TextBlock">  
  12.             <Setter Property="FontSize" Value="20"/>  
  13.             <Setter Property="FontWeight" Value="Bold"/>  
  14.             <Setter Property="VerticalAlignment" Value="Top"/>  
  15.             <Setter Property="Margin" Value="3,2,6,2"/>  
  16.         </Style>  
  17.         <Style x:Key="t2" TargetType="TextBlock">  
  18.             <Setter Property="FontSize" Value="19"/>  
  19.             <Setter Property="Margin" Value="3.2,2,3,2"/>  
  20.             <Setter Property="TextWrapping" Value="Wrap"/>  
  21.         </Style>  
  22.         <Style x:Key="jlipviewItemStyle" TargetType="FlipViewItem">  
  23.             <Setter Property="HorizontalContentAlignment" Value="Center"/>  
  24.             <Setter Property="VerticalContentAlignment" Value="Center"/>  
  25.         </Style>  
  26.     </Page.Resources>  
  27.   
  28.     <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">  
  29.         <FlipView x:Name="fv" Width="500" Height="150" VerticalAlignment="Center" HorizontalAlignment="Center" ItemContainerStyle="{StaticResource jlipviewItemStyle}">  
  30.             <FlipView.ItemTemplate>  
  31.                 <DataTemplate>  
  32.                     <Grid>  
  33.                         <Grid.ColumnDefinitions>  
  34.                             <ColumnDefinition Width="auto"/>  
  35.                             <ColumnDefinition Width="*"/>  
  36.                         </Grid.ColumnDefinitions>  
  37.                         <Grid.RowDefinitions>  
  38.                             <RowDefinition Height="auto"/>  
  39.                             <RowDefinition Height="auto"/>  
  40.                             <RowDefinition Height="auto"/>  
  41.                             <RowDefinition Height="auto"/>  
  42.                             <RowDefinition Height="auto"/>  
  43.                         </Grid.RowDefinitions>  
  44.                         <TextBlock Style="{StaticResource t1}" Grid.Column="0" Grid.Row="0" Text="姓名:"/>  
  45.                         <TextBlock Style="{StaticResource t2}" Grid.Column="1" Grid.Row="0" Text="{Binding Name}"/>  
  46.                         <TextBlock Style="{StaticResource t1}" Grid.Column="0" Grid.Row="1" Text="年龄:"/>  
  47.                         <TextBlock Style="{StaticResource t2}" Grid.Column="1" Grid.Row="1" Text="{Binding Age}"/>  
  48.                         <TextBlock Style="{StaticResource t1}" Grid.Column="0" Grid.Row="2" Text="性别:"/>  
  49.                         <TextBlock Style="{StaticResource t2}" Grid.Column="1" Grid.Row="2" Text="{Binding Sex}"/>  
  50.                         <TextBlock Style="{StaticResource t1}" Grid.Column="0" Grid.Row="3" Text="电邮:"/>  
  51.                         <TextBlock Style="{StaticResource t2}" Grid.Column="1" Grid.Row="3" Text="{Binding Email}"/>  
  52.                         <TextBlock Style="{StaticResource t1}" Grid.Column="0" Grid.Row="4" Text="备注:"/>  
  53.                         <TextBlock Style="{StaticResource t2}" Grid.Column="1" Grid.Row="4" Text="{Binding Remark}"/>  
  54.                     </Grid>  
  55.                 </DataTemplate>  
  56.             </FlipView.ItemTemplate>  
  57.         </FlipView>  
  58.     </Grid>  
  59. </Page>  


切换到代码视图,在MainPage类的构造函数中加入以下代码,设置FlipView的数据源。

[csharp] view plaincopyprint?

  1. public MainPage()  
  2. {  
  3.     this.InitializeComponent();  
  4.     TestDataSource source = new TestDataSource();  
  5.     this.fv.ItemsSource = source;  
  6. }  


如果一切正常,运行后,你应该能看到以下效果。

猜你喜欢

转载自blog.csdn.net/coaxhappy/article/details/8134759