我收到错误 - " Carousel没有孩子"。
我使用了Xamarin的旋转木马样本。如果页面相同,则轮播工作。我的要求是我的页面不同但我希望它能在旋转木马中工作。
MainPage.xaml中
<?xml version="1.0" encoding="utf-8" ?>
<CarouselPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CarouselApp.MainPage">
<CarouselPage.ItemTemplate>
<DataTemplate>
<ContentPage>
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness" iOS="0,40,0,0" Android="0,40,0,0">
</ContentPage.Padding>
<StackLayout>
<ContentView BindingContext="PageName"></ContentView>
</StackLayout>
</ContentPage>
</DataTemplate>
</CarouselPage.ItemTemplate>
</CarouselPage>
MainPage.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace CarouselApp
{
public partial class MainPage : CarouselPage
{
public MainPage()
{
InitializeComponent();
ItemsSource = CarouselAppDataModel.All;
}
}
}
的DataModel
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace CarouselApp
{
public class CarouselDataModel : ContentView
{
public ContentView PageName { get; set; }
public static IList<CarouselDatModel> All { get; set; }
private Carousel03 fl3 as new Carousel03;
private Carousel04 fl4 as new Carousel04;
public CarouselDataDataModel()
{
All = new ObservableCollection<CarouselDataModel>
{
new CarouselDataModel
{
PageName = fl3
},
new CarouselDataModel
{
PageName = fl4
}
};
}
}
}
答案 0 :(得分:0)
由于您的网页完全不同,并且您正在使用XAML ...而不是使用数据模板,只需在XAML中创建三个连续的内容页面,如下所示
<CarouselPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CarouselPageNavigation.MainPage">
<ContentPage>
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness" iOS="0,40,0,0" Android="0,40,0,0" />
</ContentPage.Padding>
<StackLayout>
<Label Text="Red" FontSize="Medium" HorizontalOptions="Center" />
<BoxView Color="Red" WidthRequest="200" HeightRequest="200" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage>
<ContentPage>
...
</ContentPage>
<ContentPage>
...
</ContentPage>
</CarouselPage>