对于Json结构体items不完全一致的情况下,在UWP平台是如何处理数据,并通过不同的模板选择,进行显示呢?
【嵌套Json分析】
[结构]
通过抓取index的API(点击请求API)获取到的json,来看主页的数据是嵌套结构的Json,并且在Recs结构体中的items具有不完全一致性,结构如下图:
如上图所示,recs有若干子项,但是展开子项的items即可发现
还有其他就不依依列举图片出来了。
为了让开发工作更加简便,我们可以新建两个类:Hanju,CommonVideo,设置好各类别的各个字段
【DataTemplate嵌套】
[最低层次]
(韩剧,直播,视频等最外层的数据)
韩剧模板(图中那些转换器,宽度不必在意,后续文章会展开来讲)
1 <DataTemplate x:Key="seriesTemplate"> 2 <Border Margin="2" Width="{Binding ElementName=hanjuThumb_width, Path=Width}"> 3 <Grid> 4 <Grid.RowDefinitions> 5 <RowDefinition/> 6 <RowDefinition Height="20"/> 7 Grid.RowDefinitions> 8 <RelativePanel CornerRadius="10"> 9 <Image x:Name="thumb" Source="{Binding thumb}" HorizontalAlignment="Center" Stretch="UniformToFill"/> 10 <Image Source="/Assets/Icons/series_preview_icon.png" Visibility="{Binding isPreview,Converter={StaticResource BoolToVisibilityConverter}}" Height="40" RelativePanel.AlignRightWithPanel="True"/> 11 <Image Source="/Assets/Icons/new_series.png" Visibility="{Binding updateTime,Converter={StaticResource IsNewUpdateConverter}}" Height="40" RelativePanel.AlignRightWithPanel="True"/> 12 <Image Source="/Assets/Icons/live_ing.png" Visibility="{Binding living,Converter={StaticResource SeriesLivingImgShowConverter}}" RelativePanel.AlignRightWithPanel="True" Height="30"/> 13 <TextBlock Text="{Binding count}" Foreground="White" RelativePanel.AlignBottomWithPanel="True" RelativePanel.AlignLeftWithPanel="True"/> 14 <TextBlock Text="{Binding rank}" Foreground="White" RelativePanel.AlignBottomWithPanel="True" RelativePanel.AlignRightWithPanel="True"/> 15 RelativePanel> 16 <TextBlock Grid.Row="2" Text="{Binding name}" HorizontalAlignment="Center" TextWrapping="Wrap" Foreground="Black" FontSize="15"/> 17 Grid> 18 Border> 19 DataTemplate>
视频模板
<DataTemplate x:Key="videoTemplate"> <Border Margin="2" Width="{Binding ElementName=videoThumb_width,Path=Width}"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="40"/> Grid.RowDefinitions> <Image Source="{Binding thumb}" Stretch="UniformToFill"/> <TextBlock Grid.Row="2" Text="{Binding title}" TextTrimming="WordEllipsis" Foreground="Black" FontSize="15" TextWrapping="Wrap"/> Grid> Border> DataTemplate>
直播模板
<DataTemplate x:Key="liveTemplate"> <RelativePanel Width="{Binding ElementName=videoThumb_width,Path=Width}"> <Image Source="{Binding thumb}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/> <Image Source="/Assets/Icons/star_living.png" Width="50" Margin="5" RelativePanel.AlignRightWithPanel="True"/> <TextBlock Text="{Binding longTitle}" Foreground="White" RelativePanel.AlignLeftWithPanel="True" RelativePanel.AlignBottomWithPanel="True"/> <TextBlock Text="{Binding online}" Foreground="White" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignBottomWithPanel="True"/> RelativePanel> DataTemplate>
[第二层次]
(是高一层次的模板,上述模板是下列这些的子项,整个韩剧集合,整个视频分类集合,GridView从左往右放单个视频)
<DataTemplate x:Key="DataTemplate1"> <GridView IsItemClickEnabled="True" ItemClick="Hanju_ItemClick" Header="{Binding title,Converter={StaticResource StringFormatConverter},ConverterParameter='{0}>>'}" ItemsSource="{Binding items}" ItemTemplate="{StaticResource seriesTemplate}"/> DataTemplate> <DataTemplate x:Key="DataTemplate2"> <GridView IsItemClickEnabled="True" ItemClick="Video_ItemClick" Header="{Binding title,Converter={StaticResource StringFormatConverter},ConverterParameter='{0}>>'}" ItemsSource="{Binding items}" ItemTemplate="{StaticResource videoTemplate}"/> DataTemplate> <DataTemplate x:Key="DataTemplate3"> <GridView Header="{Binding title,Converter={StaticResource StringFormatConverter},ConverterParameter='{0}>>'}" ItemsSource="{Binding items}" ItemTemplate="{StaticResource videoTemplate}"/> DataTemplate> <DataTemplate x:Key="DataTemplate4"> <GridView Visibility="{Binding extItems,Converter={StaticResource VisibilityConverter}}" Header="{Binding title,Converter={StaticResource StringFormatConverter},ConverterParameter='{0}>>'}" ItemsSource="{Binding extItems}" ItemTemplate="{StaticResource liveTemplate}"/> DataTemplate> <DataTemplate x:Key="DataTemplate5"> <GridView Header="{Binding title,Converter={StaticResource StringFormatConverter},ConverterParameter='{0}>>'}" ItemsSource="{Binding items}" ItemTemplate="{StaticResource videoTemplate}"/> DataTemplate>
[最高层次]
<ListView HorizontalAlignment="Center" SelectionMode="None" ItemsSource="recs" ItemTemplateSelector="{StaticResource Selector}" x:Name="lvRecs"/>
【模板选择器】
<Tools:MyTemplateSelector x:Key="Selector" x:Name="MyTemplateSelector" DataTemplate1="{StaticResource DataTemplate1}" DataTemplate2="{StaticResource DataTemplate2}" DataTemplate3="{StaticResource DataTemplate3}" DataTemplate4="{StaticResource DataTemplate4}" DataTemplate5="{StaticResource DataTemplate5}"/>
注意:在前台要引入自定义模板选择器的命名空间
xmlns:Tools="using:韩剧TV.Tools"
然后在listview里面加入ItemTemplateSelector="{StaticResource Selector}"
(见:2.3)
在json分析中可以看到,每一个视频分类都有一个type字段,应该也是安卓控制模板选择的标志。我们可以根据这个type来选择不同的模板进行适配items项,那么Selector的后台代码如下:
namespace 韩剧TV.Tools { class MyTemplateSelector:DataTemplateSelector { public DataTemplate DataTemplate1 { get; set; } public DataTemplate DataTemplate2 { get; set; } public DataTemplate DataTemplate3 { get; set; } public DataTemplate DataTemplate4 { get; set; } public DataTemplate DataTemplate5 { get; set; } protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) { Recs rec = item as Recs; try { switch (rec.type) { case 1: return DataTemplate1; case 2: return DataTemplate2; case 3: return DataTemplate3; case 4: return DataTemplate4; case 5: return DataTemplate5; default: break; } } catch { return null; } return base.SelectTemplate(item, container); } } }
可以看到自定义模板选择器继承于DataTemplateSelector,同样是WPF也存在的模板选择器
根据传入的item的type来返回对应的模板
【效果图】