我目前正在使用 Xamarin.Forms 和 .Net Standard 开发动态应用程序。我使用 MVVM 作为代码模式。视图背后没有代码。
视图/页面的内容是绑定到TemplateItem对象列表的列表视图。每个列表视图项,TemplateItem,应该看起来一样(作为一篇文章)。但是,当该属性BlockType的TemplateItem是slideshow,列表视图必须考虑使用另一种数据模板不同。
当对象的属性不同时,如何为列表视图项使用另一个数据模板?
这是我的xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App1"
x:Class="App1.MainPage"
NavigationPage.HasBackButton="True"
NavigationPage.HasNavigationBar="True"
Title="Overview">
<StackLayout >
<ActivityIndicator IsRunning="{Binding IsBusy}"
HorizontalOptions="CenterAndExpand" IsVisible="{Binding IsBusy}"/>
<ScrollView>
<StackLayout>
<ListView ItemsSource="{Binding LstTemplateList}" SeparatorVisibility="Default" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate x:Name="DTArticle">
<ViewCell>
<StackLayout>
<Label Text="{Binding Title}" FontSize="Large" />
<TextCell Text="ArticleDescription"/>
</StackLayout>
</ViewCell>
</DataTemplate>
<DataTemplate x:Name="DTSlideShow">
<ViewCell>
<!-- another DT for a slideshow -->
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ScrollView>
</StackLayout> …Run Code Online (Sandbox Code Playgroud)