【问题标题】:Xamarin.Forms show a different datatemplate for a specific item in listviewXamarin.Forms 为列表视图中的特定项目显示不同的数据模板
【发布时间】:2018-03-18 14:13:52
【问题描述】:

我目前正在开发一个使用 Xamarin.Forms 和 .Net Standard 的动态应用程序。 我正在使用 MVVM 作为代码模式。视图后面没有代码。

视图/页面的内容是绑定到TemplateItem 对象列表的列表视图。每个列表视图项目TemplateItem 应该看起来相同(作为文章)。但是当TemplateItem 的属性BlockTypeslideshow 时,使用另一个数据模板,列表视图必须看起来不同。

当对象的属性不同时,如何为列表视图项使用另一个数据模板?

这是我的 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>
</ContentPage>

这是模型类:

public class TemplateItem
{
        public int Id { get; set; }
        public String BlockType { get; set; }

        public String Title { get; set; }
        public String ArticleDescription { get; set; }
        public List<String> LstImagePathsForSlideshow { get; set; }
}

这是一个线框图,向您展示我想要完成的工作:

WireFrame of what I am trying to accomplish

【问题讨论】:

标签: c# xamarin xamarin.forms datatemplate .net-standard


【解决方案1】:

尝试使用 DataTemplateSelector,而不是使用 DataTemplate。这样您就可以为不同的对象设置不同的模板。 Reference Link :https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/data-templates/selector

【讨论】:

  • 鼓励链接到外部资源,但请在链接周围添加上下文,以便您的其他用户了解它是什么以及为什么存在。始终引用重要链接中最相关的部分,以防目标站点无法访问或永久离线。
  • 照@Baduker 所说的去做,我会保证你的回答+1 =)
猜你喜欢
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
  • 2015-12-15
  • 2012-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多