我想在Xamarin Forms中使用CarouselPage .
<?xml version="1.0" encoding="utf-8" ?>
<CarouselPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
xmlns:views="clr-namespace:TestForms.Views;assembly=TestForms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="TestForms.Views.Photos" ItemsSource="{Binding Pages}">
<CarouselPage.ItemTemplate>
<DataTemplate>
<ContentPage >
<StackLayout VerticalOptions="StartAndExpand" Padding="50">
<Label Text="ContentPage"></Label>
<Label Text="{Binding Title}"></Label>
<Label Text="{Binding Description}"></Label>
</StackLayout>
</ContentPage>
</DataTemplate>
</CarouselPage.ItemTemplate>
</CarouselPage>
在视图模型中我有
List<ContentPage> ContentPages = new List<ContentPage>();
foreach (var photo in Photos)
{
var page = new ContentPage();
page.BindingContext = new PhotoDetailViewModel(photo);
ContentPages.Add(page);
}
Pages = new ObservableCollection<ContentPage>(ContentPages);
当我渲染这个时,我会得到所有照片的页面列表 . 但我似乎无法绑定单个页面中的 Headers 或描述 . 我在这里错过了什么?
2 回答
您已正确连接
CarouselPage
只需稍微改变你的视图模型 .
我假设你的
Title
和Description
属性在你的PhotoDetailViewModel
?如果是这样,你在
CarouselPage
中创建的绑定不起作用,因为它绑定到ContentPage
的列表,该列表没有"Title"和"Description"属性在您的
CarouselPage
中,您已经设置了ItemsSource
绑定,该绑定应自动在您的CarouselPage
中设置子页面的BindingContext
. 所以你不需要手动完成 .因此,在ViewModel中创建
ObservableCollection
PhotoDetailViewModel
并将CarouselPage
的ItemsSource
绑定到该CarouselPage
.删除:
并添加:
确保将
Pages
的属性类型更改为ObservableCollection<PhotoDetailViewModel>
这应该是你需要改变的全部内容
正如我所说,你应该使用ContentView而不是ContentPage . 以下是工作示例
第xml页