Xaml 重用listview模板
我正在做一个Xamarin.Forms项目。它有多个包含ListView的页面,这些页面显示需要模板的内容。我如何能够轻松地在整个项目中重用一个模板,因为现在每次我在一个模板中更改某些内容时,都必须更新每个页面上的模板 我已经在每个页面的xaml中创建了这样一个模板Xaml 重用listview模板,xaml,xamarin,xamarin.forms,Xaml,Xamarin,Xamarin.forms,我正在做一个Xamarin.Forms项目。它有多个包含ListView的页面,这些页面显示需要模板的内容。我如何能够轻松地在整个项目中重用一个模板,因为现在每次我在一个模板中更改某些内容时,都必须更新每个页面上的模板 我已经在每个页面的xaml中创建了这样一个模板 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:*snip*.Renderers;assembly=*snip*"
x:Class="*snip*.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<DataTemplate x:Key="template1">
<local:PostViewCell>
<StackLayout BackgroundColor="White" Margin="10, 10, 10, 10" Padding="10, 10, 10, 10">
. . .
</StackLayout>
</local:PostViewCell>
</DataTemplate>
<DataTemplate x:Key="template2">
<local:PostViewCell>
<StackLayout BackgroundColor="White" Margin="10, 10, 10, 10" Padding="10, 10, 10, 10">
. . .
</StackLayout>
</local:PostViewCell>
</DataTemplate>
<local:PostTemplateSelector x:Key="templateSelector"
Template1="{StaticResource template1}"
Template2="{StaticResource template2}" ></local:PostTemplateSelector>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<local:PostListView x:Name="PostListView" ItemTemplate="{StaticResource templateSelector}"/>
</StackLayout>
</ContentPage.Content>
. . .
. . .
您可以在
App.xaml
中将数据模板定义为一个资源,并从各个xaml页面中引用它在App.xaml中使用唯一键定义您的数据模板
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="YourAppName.App">
<Application.Resources>
<ResourceDictionary>
<DataTemplate x:Key="lstTemplate">
<ViewCell>
<------Your Design here------->
</ViewCell>
</DataTemplate>
</ResourceDictionary>
</Application.Resources>
</Application>
在Listview中使用lstTemplate键
<ListView x:Name=lst"
ItemsSource="{Binding Name}"
SeparatorColor="#0094FF" ItemTemplate="{StaticResource lstTemplate}">
官方文档,总是从这里开始:我如何从每个XAML页面引用它?我有…
,但它一直说找不到TextPostTemplate