2017-01-16 1 views
1

すべてのスライドに特定のテンプレートがあるXamarinフォームでCarouselViewを作成する必要があります。 は現在、私はそうしていますXamarinフォームの各スライドのテンプレートが異なるCarouselView

XAML

xmlns:control="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView" 
....... 

<ContentView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"> 
      <control:CarouselView x:Name="carouselView"> 
      <control:CarouselView.ItemTemplate> 
       <DataTemplate> 
       <Label Text="{Binding Testo}" /> 
       </DataTemplate> 
      </control:CarouselView.ItemTemplate> 
      </control:CarouselView> 
     </ContentView> 

分離コード

List<CustomCell> myCarousel = new List<CustomCell>(); 
myCarousel.Add(new CustomCell { Testo = "ciao" }); 
myCarousel.Add(new CustomCell { Testo = "ciao due" }); 

carouselView.ItemsSource = myCarousel; 

CustomCell

public class CustomCell 
{ 
    public string Testo { get; set; } 
} 

私の問題は、スライドごとに異なるテンプレートがあることです。例えば、グリッドごとにグラフィックスごとに異なるグリッドがあります。これは、グラフィカルに違ったデータを表示する必要があるためです。 ソリューションをお勧めしますか?ありがとう

答えて

2

data template selectorを使用して、CarouselViewのさまざまな項目の外観をカスタマイズできます。簡単な例:

MyDataTemplateSelector.cs

public class MyDataTemplateSelector : DataTemplateSelector 
{ 
    public DataTemplate SimpleTemplate { get; set; } 
    public DataTemplate ComplexTemplate { get; set; } 

    public MyDataTemplateSelector() 
    { 
     SimpleTemplate = new DataTemplate(typeof(SimpleView)); 
     ComplexTemplate = new DataTemplate(typeof(ComplexView)); 
    } 

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container) 
    { 
     CustomCell cell = (CustomCell)item; 

     if (cell.Testo.Length > 5) { 
      return ComplexTemplate; 
     } else { 
      return SimpleTemplate; 
     } 
    } 
} 

SimpleView.xaml

<ContentView> 
    <StackLayout BackgroundColor="Red"> 
     <Label Text="{Binding Testo}" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" /> 
    </StackLayout> 
</ContentView> 

ComplexView.xaml

<ContentView> 
    <StackLayout BackgroundColor="Yellow" > 
     <Label Text="{Binding Testo}" VerticalOptions="CenterAndExpand" HorizontalOptions="Center" /> 
     <Label Text="I lied about this being complex" /> 
    </StackLayout> 
</ContentView> 

そして、あなたのCarouselViewがあるページで:

<ContentPage.Resources> 
    <ResourceDictionary> 
    <local:MyDataTemplateSelector x:Key="templateSelector"></local:MyDataTemplateSelector> 
    </ResourceDictionary> 
</ContentPage.Resources> 

.... 

<control:CarouselView x:Name="carouselView" ItemTemplate="{StaticResource templateSelector}" /> 
関連する問題