c# - 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"
/>
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");
}
}
}
解决方案
文档有一个明确的例子。如果要使用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"));
推荐阅读
- python - 在 Python 中过滤两个具有相同道具的对象列表
- typescript - 来自 typeof 对象的打字稿接口
- android - 在新的android版本中对从前台服务启动活动的限制是否适用于通过单击通知启动活动?
- laravel - 404 添加到购物车 laravel
- php - 使用来自任何类型图像标签的 php 从 html 获取图像路径
- laravel - Laravel - 未定义的索引:嵌套 foreach 中的 current_timestamp
- algorithm - 如何组合旋转数据的属性?
- typescript - Typescript:如何在实例成员属性(mixin)中使用方法装饰器?
- javascript - 如何在 JS 中递归重复一个字符串 n 次?
- c - 哪些运算符可用于限制整数除法?