【问题标题】:Xamarin forms: CarouselView for static contentXamarin 表单:用于静态内容的 CarouselView
【发布时间】:2018-02-08 10:00:14
【问题描述】:

我正在寻找一种将 CarouselView 用于仅具有静态内容的 Xamarin 表单的方法。这意味着,我不打算使用任何 Itemsource 或绑定或其他任何东西。

假设我有三个独立的 Stacklayout(每个 Stacklayout 都包含几个文本标签和按钮)并且我希望能够水平滑动它们。像这样的东西(伪代码):

<cv:CarouselView>
   <cv:CarouselView.Items>
      <CarouselItem>
         <StackLayout>
            <Label Text="This is step1" />
         </StackLayout>
      </CarouselItem >
      <CarouselItem>
         <StackLayout>
            <Label Text="This is step2" />
         </StackLayout>
      </CarouselItem >
      <CarouselItem>
         <StackLayout>
            <Label Text="This is step3" />
         </StackLayout>
      </CarouselItem >
   </cv:CarouselView.Items>
</cv:CarouselView>

如果我可以“按原样”放置我的内容而不使用任何动态项目源,那就太好了。有什么想法吗?

【问题讨论】:

    标签: xaml xamarin.forms


    【解决方案1】:

    您可以使用data template selectors 选择要显示的视图。

    首先,创建具有所需内容的内容视图:

    <ContentView.Content>
        <StackLayout>
            <Label Text="Hello Xamarin.Forms!" />
        </StackLayout>
    </ContentView.Content>
    

    然后创建一个数据模板选择器并在其中决定应该调用哪个模板:

    public DataTemplate Step1 { get; set; }
        public DataTemplate Step2 { get; set; }
        public DataTemplate Step3 { get; set; }
    
        public HelpPageDataTemplateSelector()
        {
            Step1 = new DataTemplate(typeof(ContentViewStep1));
            Step2 = new DataTemplate(typeof(ContentViewStep2));
            Step3 = new DataTemplate(typeof(ContentViewStep3));
        }
    
        protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
        {
            switch ((int)item)
            {
                case 2: return Step2;
                case 3: return Step3;
                default: return Step1;
            }
        }
    

    然后在轮播视图中,设置数据模板选择器并将步骤绑定为项目源(模型中的步骤只是整数数组 - 例如 1,2,3):

    <ContentPage.Resources>
        <ResourceDictionary>
            <Selector:TemplateSelector x:Key="TemplateSelector"></Selector:HelpPageDataTemplateSelector>
        </ResourceDictionary>
    </ContentPage.Resources>
    
    <ContentPage.Content>
        <Forms:CarouselView ItemTemplate="{StaticResource TemplateSelector}" ItemsSource="{Binding Steps}" />
    </ContentPage.Content>
    

    【讨论】:

      【解决方案2】:

      不要使用动态的,只需在模型或页面构造函数中手动创建一个 List 并将其设置为您的 ItemsSource。

      1. 使用x:name="MyCarousel"为 xaml 中的 Carousel 命名

      2. 声明你的幻灯片项目类:

        public class MyItem
        {
            public string Desc { get; set; }
            //put more stufff here
        }       
        
      3. 在后面的代码中发送你所谓的静态数据:

        var MyItems = new List<MyItem>
        {
            new MyItem
            {
                Desc = "This is step1",
            },
            new MyItem
            {
                Desc = "This is step2",
            },
            new MyItem
            {
                Desc = "This is step3",
            },
        };
        MyCarousel.ItemsSource = MyItems;
        

      【讨论】:

      • 好的,谢谢,但我想坚持使用 XAML 代码。也许我的要求太模糊了:我不想写一个列表,因为每个 CarouselItems 或幻灯片都包含不同的元素。所以我必须为每张幻灯片编写一个不同的类,所以为什么要麻烦而不是使用纯 XAML 声明所有内容?!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      • 2013-11-11
      • 2019-10-07
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      • 1970-01-01
      相关资源
      最近更新 更多