繁体   English   中英

CarouselPage Xamarin

[英]CarouselPage Xamarin

我不确定我在这里做错了什么。

我有一个 CarouselPage 和一个 ContentPage 我想以编程方式将内容页面添加到 CarouselPage 但是当我这样做时,应用程序中没有任何反应。

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

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

主页.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"
/>

主页.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;

        }

    }
}

故事页面.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>

故事页面.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");
        }

    }
}

文档有一个明确的例子来说明这一点。 如果要使用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"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM