微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CarouselPage Xamarin

如何解决CarouselPage Xamarin

我不确定我在做什么错。

我有一个CarouselPage和一个ContentPage,我想以编程方式将内容页添加到CarouselPage中,但是当我这样做时,应用程序中什么都没有发生。

我可以看到正在添加到列表中的项目,但是当我在下面运行代码时没有显示页面

任何对我做错事的帮助都非常感谢:)

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="DemoApp2.MainPage"
/>

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace DemoApp2
{
    public partial class MainPage : CarouselPage
    {
        public MainPage()
        {
            InitializeComponent();

            ObservableCollection<ContentPage> pages = new ObservableCollection<ContentPage>();

            pages.Add(new StoryPage("This is page 1"));
            pages.Add(new StoryPage("This is page 2"));
            pages.Add(new StoryPage("This is page 3"));
            pages.Add(new StoryPage("This is page 4"));
            pages.Add(new StoryPage("This is page 5"));

            this.ItemsSource = pages;

        }

    }
}

StoryPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="DemoApp2.StoryPage">
    <ContentPage.Content>
        <StackLayout>
            <Label x:Name="YourLableName"
                   Text="Not Set"
                   TextColor="Black"
                VerticalOptions="CenterandExpand" 
                HorizontalOptions="CenterandExpand" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

StoryPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace DemoApp2
{
    [XamlCompilation(XamlCompilationoptions.Compile)]
    public partial class StoryPage : ContentPage
    {


        public string PageText = "";

        public StoryPage()
        {
            InitializeComponent();

            PageText = "";

        }


        public StoryPage(string pageTextIn)
        {
            InitializeComponent();

            YourLableName.TextColor = Color.Black;
            YourLableName.Text = pageTextIn;

        }

        protected override void OnAppearing()
        {
            Debug.WriteLine("OnAppearing");
        }

        protected override void Ondisappearing()
        {
            Debug.WriteLine("Ondisappearing");
        }

    }
}

解决方法

docs有一个清晰的示例。如果要使用ItemsSource,则必须提供DataTemplate。否则,您将修改页面的Children

Children.Add(new StoryPage("This is page 1"));
Children.Add(new StoryPage("This is page 2"));
Children.Add(new StoryPage("This is page 3"));
Children.Add(new StoryPage("This is page 4"));
Children.Add(new StoryPage("This is page 5"));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。